|
http://www.hackerschool.org/HS_Boards/zboard.php?AllArticle=true&no=3033 [복사]
import java.awt.event.*;
import javax.swing.*;
class CalculatorTest extends JFrame{
private JButton button1, button2,button3,button4,button5;
private JButton button6,button7,button8,button9,button10;
private JButton button11,button12,button13,button14,button15;
private JTextField t,t1;
public CalculatorTest(){
this.setSize(500,500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("계산기");
JPanel panel = new JPanel();
JLabel label = new JLabel("결과 = ");
t = new JTextField(40);
t1 = new JTextField(30);
button1 = new JButton("C");
button2 = new JButton("1");
button3 = new JButton("2");
button4 = new JButton("3");
button5 = new JButton("4");
button6 = new JButton("5");
button7 = new JButton("6");
button8 = new JButton("7");
button9 = new JButton("8");
button10 = new JButton("9");
button11 = new JButton("0");
button12 = new JButton("+");
button13 = new JButton("-");
button14 = new JButton("/");
button15 = new JButton("*");
button1.addActionListener(new MyListener());
button2.addActionListener(new MyListener());
button3.addActionListener(new MyListener());
button4.addActionListener(new MyListener());
button5.addActionListener(new MyListener());
button6.addActionListener(new MyListener());
button7.addActionListener(new MyListener());
button8.addActionListener(new MyListener());
button9.addActionListener(new MyListener());
button10.addActionListener(new MyListener());
button11.addActionListener(new MyListener());
button12.addActionListener(new MyListener());
button13.addActionListener(new MyListener());
button14.addActionListener(new MyListener());
button15.addActionListener(new MyListener());
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(button4);
panel.add(button5);
panel.add(button6);
panel.add(button7);
panel.add(button8);
panel.add(button9);
panel.add(button10);
panel.add(button11);
panel.add(button12);
panel.add(button13);
panel.add(button14);
panel.add(button15);
panel.add(t);
panel.add(label);
panel.add(t1);
this.add(panel);
this.setVisible(true);
}
private class MyListener implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource() == button1){
t.setText("취소");
}
else if(e.getSource() == button2){
t.setText("1");
}
else if(e.getSource() == button3){
t.setText("2");
}
else if(e.getSource() == button4){
t.setText("3");
}
else if(e.getSource() == button5){
t.setText("4");
}
if(e.getSource() == button6){
t.setText("5");
}
if(e.getSource() == button7){
t.setText("6");
}
if(e.getSource() == button8){
t.setText("7");
}
if(e.getSource() == button9){
t.setText("8");
}
if(e.getSource() == button10){
t.setText("9");
}
if(e.getSource() == button11){
t.setText("0");
}
if(e.getSource() == button12){
t.setText("+");
}
if(e.getSource() == button13){
t.setText("-");
}
if(e.getSource() == button14){
t.setText("/");
}
if(e.getSource() == button15){
t.setText("*");
}
}
}
}
public class Calculator{
public static void main(String[] args){
CalculatorTest c = new CalculatorTest();
}
}
이 소스처럼 대충 계산기 프로그램은 만들었는데
문제점은....
만약에 숫자 1을 입력한 뒤에 다시 숫자 2를 누르면
12 이런 식으로 나와야 되는데 그렇지 않고
1을 입력하고 2를 누르면 그냥 2만 나옵니다.
어떻게 해야 될지 궁금합니다... |
Hit : 18297 Date : 2011/06/02 03:16
|