JFrame을 생성하여 사용
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
public class BasicOperand1 { public static void main(String[] args) { // TODO Auto-generated method stub JFrame f = new JFrame(); f.setTitle("연산자"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setSize(300, 600); f.setLayout(null); JTextField txtNum1 = new JTextField(20); txtNum1.setBounds(10, 10, 200, 25); f.add(txtNum1); JTextField txtNum2 = new JTextField(20); txtNum2.setBounds(10, 50, 200, 25); f.add(txtNum2); JLabel lblResult = new JLabel("결과출력"); lblResult.setBounds(10, 90, 200, 25); f.add(lblResult); JButton btnAdd = new JButton("더하기"); btnAdd.setBounds(10, 130, 200, 25); f.add(btnAdd); btnAdd.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub double n1 = Double.parseDouble(txtNum1.getText().toString()); double n2 = Double.parseDouble(txtNum2.getText().toString()); double n = n1 + n2; lblResult.setText(n + ""); } }); f.setVisible(true); } } |
JFrame을 상속받아 사용
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
public class BasicOperand2 extends JFrame { public BasicOperand2() { this.setTitle("연산자"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(300, 600); this.setLayout(null); JTextField txtNum1 = new JTextField(20); txtNum1.setBounds(10, 10, 200, 25); this.add(txtNum1); JTextField txtNum2 = new JTextField(20); txtNum2.setBounds(10, 50, 200, 25); this.add(txtNum2); JLabel lblResult = new JLabel("결과출력"); lblResult.setBounds(10, 90, 200, 25); this.add(lblResult); JButton btnAdd = new JButton("더하기"); btnAdd.setBounds(10, 130, 200, 25); this.add(btnAdd); btnAdd.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub double n1 = Double.parseDouble(txtNum1.getText().toString()); double n2 = Double.parseDouble(txtNum2.getText().toString()); double n = n1 + n2; lblResult.setText(n + ""); } }); this.setVisible(true); } public static void main(String[] args) { // TODO Auto-generated method stub new BasicOperand2(); } } |
ActionListener를 별도로 만들어 사용
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
public class BasicOperand3 extends JFrame { JTextField txtNum1; JTextField txtNum2; JLabel lblResult; ActionListener actionListener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub double n1 = Double.parseDouble(txtNum1.getText().toString()); double n2 = Double.parseDouble(txtNum2.getText().toString()); double n = 0; System.out.println(e.getActionCommand()); switch(e.getActionCommand()) { case "더하기" : n = n1 + n2; break; } lblResult.setText(n + ""); } }; public BasicOperand3() { this.setTitle("연산자"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(300, 600); this.setLayout(null); txtNum1 = new JTextField(20); txtNum1.setBounds(10, 10, 200, 25); this.add(txtNum1); txtNum2 = new JTextField(20); txtNum2.setBounds(10, 50, 200, 25); this.add(txtNum2); lblResult = new JLabel("결과출력"); lblResult.setBounds(10, 90, 200, 25); this.add(lblResult); JButton btnAdd = new JButton("더하기"); btnAdd.setBounds(10, 130, 200, 25); this.add(btnAdd); btnAdd.addActionListener(actionListener); this.setVisible(true); } public static void main(String[] args) { // TODO Auto-generated method stub new BasicOperand3(); } } |
ActionListener를 상속받아 사용
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
public class BasicOperand4 extends JFrame implements ActionListener{ JTextField txtNum1; JTextField txtNum2; JLabel lblResult; public BasicOperand4() { this.setTitle("연산자"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(300, 600); this.setLayout(null); txtNum1 = new JTextField(20); txtNum1.setBounds(10, 10, 200, 25); this.add(txtNum1); txtNum2 = new JTextField(20); txtNum2.setBounds(10, 50, 200, 25); this.add(txtNum2); lblResult = new JLabel("결과출력"); lblResult.setBounds(10, 90, 200, 25); this.add(lblResult); JButton btnAdd = new JButton("더하기"); btnAdd.setBounds(10, 130, 200, 25); this.add(btnAdd); btnAdd.addActionListener(this); this.setVisible(true); } public static void main(String[] args) { // TODO Auto-generated method stub new BasicOperand4(); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub double n1 = Double.parseDouble(txtNum1.getText().toString()); double n2 = Double.parseDouble(txtNum2.getText().toString()); double n = 0; System.out.println(e.getActionCommand()); switch(e.getActionCommand()) { case "더하기" : n = n1 + n2; break; } lblResult.setText(n + ""); } } |
반복문 사용해서 버튼 생성 코드 줄이기
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
public class BasicOperand5 extends JFrame { JTextField t1; JTextField t2; JLabel L1; String[] op = new String[] { "+", "-", "*", "/", "%", ">>", "<<", ">>>", "&", "|", "^", "~"}; ActionListener actionListener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int a = Integer.parseInt(t1.getText()); int b = Integer.parseInt(t2.getText()); int c = 0; switch (e.getActionCommand()) { case "+": c = a + b; break; case "-": c = a - b; break; case "*": c = a * b; break; case "/": c = a / b; break; case "%": c = a % b; break; case ">>": c = a >> b; break; case "<<": c = a << b; break; case ">>>": c = a >>> b; break; case "&": c = a & b; break; case "|": c = a | b; break; case "^": c = a ^ b; break; case "~": c = ~a; break; } L1.setText("" + c); } }; public BasicOperand5() { this.setTitle("연산자"); this.setSize(300, 600); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLayout(null); t1 = new JTextField(20); t1.setBounds(10, 10, 200, 25); this.add(t1); t2 = new JTextField(20); t2.setBounds(10, 40, 200, 25); this.add(t2); L1 = new JLabel("0"); L1.setBounds(10, 70, 200, 25); this.add(L1); JButton btns[] = new JButton[op.length]; for (int i = 0; i < op.length; i++) { btns[i] = new JButton(); btns[i].setBounds(10, i*30 + 100, 200, 25); btns[i].setText(op[i]); this.add(btns[i]); btns[i].addActionListener(actionListener); } this.setVisible(true); } public static void main(String[] args) { new BasicOperand5(); } } |