본문 바로가기

Study/Programming

자바 AWT 10진수 16진수 변환기

import java.awt.*;

/**
 * @author ehjeong
 *
 */
public class Hex2Dec extends Frame {

 private static final long serialVersionUID = 1L;
 private TextField textField = null;
 private TextField textField1 = null;
 private Button button = null;
 private CheckboxGroup cg = new CheckboxGroup();
 private Checkbox checkbox = new Checkbox("16 > 10", true, cg);
 private Checkbox checkbox1 = new Checkbox("10 > 16", false, cg);
 /**
  * @throws HeadlessException
  */
 public Hex2Dec() throws HeadlessException {
  // TODO Auto-generated constructor stub
  super();
  initialize();
 }

 /**
  * @param arg0
  */
 public Hex2Dec(GraphicsConfiguration arg0) {
  super(arg0);
  // TODO Auto-generated constructor stub
  initialize();
 }

 /**
  * @param arg0
  * @throws HeadlessException
  */
 public Hex2Dec(String arg0) throws HeadlessException {
  super(arg0);
  // TODO Auto-generated constructor stub
  initialize();
 }

 /**
  * @param arg0
  * @param arg1
  */
 public Hex2Dec(String arg0, GraphicsConfiguration arg1) {
  super(arg0, arg1);
  // TODO Auto-generated constructor stub
  initialize();
 }

 /**
  * This method initializes textField 
  *  
  * @return java.awt.TextField 
  */
 private TextField getTextField() {
  if (textField == null) {
   textField = new TextField();
   textField.setColumns(4);
   textField.addKeyListener(new java.awt.event.KeyListener() {
    public void keyPressed(java.awt.event.KeyEvent e) {
//     System.out.println("keyPressed()"); // TODO Auto-generated Event stub keyPressed()
    }
    public void keyTyped(java.awt.event.KeyEvent e) {
    }
    public void keyReleased(java.awt.event.KeyEvent e) {
     if(e.getKeyCode() == 10){
      event();
     }
    }
   });
  }
  return textField;
 }

 /**
  * This method initializes textField1 
  *  
  * @return java.awt.TextField 
  */
 private TextField getTextField1() {
  if (textField1 == null) {
   textField1 = new TextField();
   textField1.setColumns(4);
   textField1.setEnabled(false);
  }
  return textField1;
 }

 /**
  * This method initializes button 
  *  
  * @return java.awt.Button 
  */
 
 void event(){
  int input = 0;
  String hexstr = "";
  
  if(cg.getSelectedCheckbox() == checkbox){
   input = Integer.parseInt(textField.getText(), 16);
   textField1.setText(Integer.toString(input));   
  }else if(cg.getSelectedCheckbox() == checkbox1){
   hexstr = Integer.toString(Integer.parseInt(textField.getText()), 16);
   textField1.setText(hexstr.toUpperCase());
  }
  
 }
 
 private Button getButton(){
  if (button == null) {
   button = new Button();
   button.setLabel("Convert");
   button.addMouseListener(new java.awt.event.MouseAdapter() {
    public void mouseClicked(java.awt.event.MouseEvent e) {

     
     event();           
     
//     System.out.println("mouseClicked()"); // TODO Auto-generated Event stub mouseClicked()
     
    }
   });
  }
  return button;
 }

 /**
  * This method initializes checkbox 
  *  
  * @return java.awt.Checkbox 
  */
 private Checkbox getCheckbox() {
  if (checkbox == null) {
   checkbox = new Checkbox();
  }
  return checkbox;
 }

 /**
  * This method initializes checkbox1 
  *  
  * @return java.awt.Checkbox 
  */
 private Checkbox getCheckbox1() {
  if (checkbox1 == null) {
   checkbox1 = new Checkbox();
  }
  return checkbox1;
 }

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Hex2Dec hd = new Hex2Dec();


 }

 /**
  * This method initializes this
  *
  * @return void
  */
 private void initialize() {
  FlowLayout flowLayout = new FlowLayout();
  flowLayout.setAlignment(java.awt.FlowLayout.CENTER);
  flowLayout.setVgap(5);
  flowLayout.setHgap(5);
  this.setLayout(flowLayout);
  this.setSize(385, 75);
  this.setTitle("Jeong");
  this.setVisible(true);

  this.add(getCheckbox(), null);
  this.add(getCheckbox1(), null);
  this.add(getTextField(), null);
  this.add(getButton(), null);
  this.add(getTextField1(), null);
  this.addWindowListener(new java.awt.event.WindowAdapter() {
   public void windowClosing(java.awt.event.WindowEvent e) {
//    System.out.println("windowClosing()"); // TODO Auto-generated Event stub windowClosing()
                e.getWindow().setVisible(false);
                e.getWindow().dispose();
    System.exit(0);
   }
  });
 }

}  //  @jve:decl-index=0:visual-constraint="35,11"