아래의 그림과 같은 계산기 프로그램을 만들어 보자.
조건
1. 계산 값이 소수점 첫째 자리 까지 표기되어야 한다.
2. 계산 값이 출력되는 부분은 ScrollPane- TextArea를 사용해야 한다.
Main
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Main {
private JFrame frame;
private JTextField txtnum1;
private JTextField txtnum2;
private JTextArea textArea;//버튼에서 액션이 발생했을 때 접근하기 위해 설정
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main window = new Main();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Main() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null); //
// Number 1 number2 레이블을 GUI화면에 출력
int lblWidth = 142;
int lblheight = 38;
Font lblFont = new Font("굴림", Font.BOLD, 20);
JLabel lblNum1 = new JLabel("Number 1");
lblNum1.setBounds(12, 10, lblWidth, lblheight);
lblNum1.setFont(lblFont);
frame.getContentPane().add(lblNum1);
JLabel lblNum2 = new JLabel("Number 2");
lblNum2.setBounds(12, 62, lblWidth, lblheight);
lblNum2.setFont(lblFont);
frame.getContentPane().add(lblNum2);
// 숫자를 입력할 공간 2개
int txtWidth = 256;
int txtHeight = 38;
Font txtFont = new Font("돋움", Font.PLAIN, 15);
txtnum1 = new JTextField();
txtnum1.setBounds(166, 10, txtWidth, txtHeight);
frame.getContentPane().add(txtnum1);
txtnum1.setColumns(10);
txtnum1.setFont(txtFont);
txtnum2 = new JTextField();
txtnum2.setColumns(10);
txtnum2.setBounds(166, 62, txtWidth, txtHeight);
frame.getContentPane().add(txtnum2);
txtnum2.setFont(txtFont);
// 버튼 클릭 시 +, -, *, / 연산이 되도록 한다.
int btnWidth = 87;
int btnHeight = 38;
Font btnFont = new Font("바탕", Font.ITALIC, 20);
JButton btnAdd = new JButton("+");
btnAdd.addActionListener(new ActionListener() { // 버튼에 액션이 발생될 때
public void actionPerformed(ActionEvent e) {
String num1 = txtnum1.getText();
String num2 = txtnum2.getText();
//1. 계산 값이 소수점 첫째 자리 까지 표기되어야 한다.
double num1_d = Double.parseDouble(num1); // 문자를 실수 값으로 변환
double num2_d = Double.parseDouble(num2);
String msg = num1_d + " + " + num2_d + " = " + (num1_d + num2_d) + "\n";
textArea.append(msg); // 문자열이 추가되면서 출력된다.
}
});
btnAdd.setBounds(12, 110, btnWidth, btnHeight);
btnAdd.setFont(btnFont);
frame.getContentPane().add(btnAdd);
JButton btnSub = new JButton("-");
btnSub.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String num1 = txtnum1.getText();
String num2 = txtnum2.getText();
double num1_d = Double.parseDouble(num1);
double num2_d = Double.parseDouble(num2);
String msg = num1_d + " - " + num2_d + " = " + (num1_d - num2_d) + "\n";
textArea.append(msg);
}
});
btnSub.setBounds(111, 110, btnWidth, btnHeight);
btnSub.setFont(btnFont);
frame.getContentPane().add(btnSub);
JButton btnMul = new JButton("*");
btnMul.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String num1 = txtnum1.getText();
String num2 = txtnum2.getText();
double num1_d = Double.parseDouble(num1);
double num2_d = Double.parseDouble(num2);
String msg = num1_d + " * " + num2_d + " = " + (num1_d * num2_d) + "\n";
textArea.append(msg);
}
});
btnMul.setBounds(226, 110, btnWidth, btnHeight);
btnMul.setFont(btnFont);
frame.getContentPane().add(btnMul);
JButton btnDiv = new JButton("/");
btnDiv.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String num1 = txtnum1.getText();
String num2 = txtnum2.getText();
double num1_d = Double.parseDouble(num1);
double num2_d = Double.parseDouble(num2);
String msg = num1_d + " / " + num2_d + " = " + (num1_d / num2_d) + "\n";
textArea.append(msg);
}
});
btnDiv.setBounds(325, 110, btnWidth, btnHeight);
btnDiv.setFont(btnFont);
frame.getContentPane().add(btnDiv);
//2. 계산 값이 출력되는 부분은 ScrollPane- TextArea를 사용해야 한다.
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(12, 158, 410, 93);
frame.getContentPane().add(scrollPane);
textArea = new JTextArea();
scrollPane.setViewportView(textArea);
}
}