package test;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Test6 extends JFrame {
JTextField tf1 = new JTextField(10);
JTextField tf2 = new JTextField(10);
JTextField tfResult = new JTextField(10);
JButton btnAdd = new JButton("덧셈");
JButton btnSubtract = new JButton("뺄셈");
JButton btnMultiply = new JButton("곱셈");
JButton btnDivide = new JButton("나눗셈");
public Test6() {
setTitle("간단한 계산기 연습");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
c.add(new JLabel("숫자1 : "));
c.add(tf1);
c.add(new JLabel("숫자2 : "));
c.add(tf2);
c.add(btnAdd);
c.add(btnSubtract);
c.add(btnMultiply);
c.add(btnDivide);
c.add(tfResult);
btnAdd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String strNum1 = tf1.getText().trim();
String strNum2 = tf2.getText().trim();
int num1 = Integer.parseInt(strNum1);
int num2 = Integer.parseInt(strNum2);
int sum = num1 + num2;
tfResult.setText(Integer.toString(sum));
}
});
btnSubtract.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String strNum1 = tf1.getText().trim();
String strNum2 = tf2.getText().trim();
int num1 = Integer.parseInt(strNum1);
int num2 = Integer.parseInt(strNum2);
int sum = num1 - num2;
tfResult.setText(Integer.toString(sum));
}
});
btnMultiply.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String strNum1 = tf1.getText().trim();
String strNum2 = tf2.getText().trim();
int num1 = Integer.parseInt(strNum1);
int num2 = Integer.parseInt(strNum2);
int sum = num1 * num2;
tfResult.setText(Integer.toString(sum));
}
});
btnDivide.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String strNum1 = tf1.getText().trim();
String strNum2 = tf2.getText().trim();
int num1 = Integer.parseInt(strNum1);
int num2 = Integer.parseInt(strNum2);
int sum = num1 / num2;
tfResult.setText(Integer.toString(sum));
}
});
setSize(220, 250);
setVisible(true);
}
public static void main(String[] args) {
new Test6();
}
}
'IT > Java' 카테고리의 다른 글
Java - JUnit 픽스처, setUp, tearDown, Before, After, BeforeClass, AfterClass (0) | 2018.11.14 |
---|---|
java - JUnit 사용 방법 예제 코드 (0) | 2018.11.14 |
자바 마우스 이벤트, MouseListener, MouseAdapter (0) | 2018.08.07 |
자바 이벤트 ActionListener, 독립클래스, 멤버내부클래스, 지역내부클래스, 익명클래스 (0) | 2018.08.07 |
자바 GUI, JFrame, BorderLayout, GridLayout, NullContainer (0) | 2018.08.06 |