728x90
import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;
import java.awt.event.*;
class BtnDigitListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton)e.getSource();
String digits = NsystemWin.tf.getText();
if (digits.equals("0"))
digits = btn.getText();
else
digits = digits + btn.getText();
NsystemWin.tf.setText(digits);
}
}
class BtnModelListener implements ActionListener {
static int state = 10;
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton)e.getSource();
String value = NsystemWin.tf.getText();
String mode = btn.getText();
switch(mode.toUpperCase()) {
case "BIN":
NsystemWin.tf.setText(convert(value, state, 2));
state = 2;
break;
case "OCT":
NsystemWin.tf.setText(convert(value, state, 8));
state = 8;
break;
case "DEC":
NsystemWin.tf.setText(convert(value, state, 10));
state = 10;
break;
case "HEX":
NsystemWin.tf.setText(convert(value, state, 16));
state = 16;
break;
case "C":
int dec_val = Integer.parseInt(value, state);
dec_val = dec_val/state;
value = convert(Integer.toString(dec_val), 10, state);
NsystemWin.tf.setText(value);
break;
default:
System.out.println("not available");
break;
}
NsystemWin.Label.setText(" "+Integer.toString(state) + "진수");
}
private String convert(String value, int from, int to) {
String convert_val;
int dec_val = Integer.parseInt(value, from);
switch (to) {
case 10:
convert_val = Integer.toString(dec_val);
break;
case 2:
convert_val = Integer.toBinaryString(dec_val);
break;
case 8:
convert_val = Integer.toOctalString(dec_val);
break;
case 16:
convert_val = Integer.toHexString(dec_val);
break;
default:
System.out.println("잘못된 입력");
return value;
}
return convert_val;
}
}
class NsystemWin extends JFrame {
private String[] btnNames = {
"0", "1", "2", "3", "4",
"5", "6", "7", "8", "9",
"Bin", "Oct", "Dec", "Hex", "C"
};
static JLabel Label;
static TextField tf;
public NsystemWin() {
initLayout();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Numeric System");
this.setSize(300, 200);
this.setMinimumSize(new Dimension(300, 200));
this.setVisible(true);
}
private void initLayout() {
this.getContentPane().setLayout(new BorderLayout());
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
panel1.setLayout(new GridLayout(1, 2));
panel2.setLayout(new GridLayout(3, 5));
tf = new TextField();
Font font = new Font("Consolas", Font.PLAIN, 20);
tf.setFont(font);
tf.setText("0");
Label = new JLabel();
Label.setHorizontalAlignment(JLabel.CENTER);
Label.setSize(30, 30);
Label.setText("10진수");
panel1.add(Label);
panel1.add(tf);
BtnDigitListener digitListener = new BtnDigitListener();
BtnModelListener modeListener = new BtnModelListener();
for (int i =0; i<btnNames.length; i++) {
JButton btn = new JButton(btnNames[i]);
if (i<10)
btn.addActionListener(digitListener);
else
btn.addActionListener(modeListener);
panel2.add(btn);
}
this.getContentPane().add(panel1, BorderLayout.NORTH);
this.getContentPane().add(panel2, BorderLayout.CENTER);
}
}
public class Ex extends JFrame {
public static void main(String[] args) {
NsystemWin nsw = new NsystemWin();
}
}
728x90
'프로그래밍 언어 > javastudy' 카테고리의 다른 글
만들기 (0) | 2021.08.09 |
---|---|
만들다 만 계산기 (0) | 2021.07.21 |
명품 Java Essential 8단원 실습문제 1~7 (0) | 2021.07.20 |
간단한 스윙 프로그램 만들기/ 명품 자바 에센셜 8 (0) | 2021.07.19 |
명품 Java Essential 7단원 Bonus 1 (0) | 2021.07.17 |