GUI 윈도우로 작동하는 덧셈 프로그램 문제
다음과 같은 윈도우로 작동하는 덧셈 프로그램을 작성하십시오.
이 윈도우의 첫 번째, 두 번째 텍스트 상자에 수를 입력하고 확인 버튼을 누르면 세 번째 텍스트 상자에 합계가 출력되어야 합니다. 그리고 취소 버튼을 누르면 세 개의 텍스트 상자가 모두 클리어되어야 합니다.
※ 실행 클래스와 확인 버튼을 처리하는 이벤트 리스너 클래스, 취소 버튼을 처리하는 이벤트 리스너 클래스를 각각 만든다.
-------------------------------------------------------------------------------------------------
1. 실행 클래스
package GUI프로그래밍;
import java.awt.*;
import javax.swing.*;
public class Answer {
public static void main(String[] args) {
JFrame frame = new JFrame("덧셈프로그램");
frame.setLocation(500, 400);
Container contentPane = frame.getContentPane();
JPanel pane1 = new JPanel();
JPanel pane2 = new JPanel();
contentPane.add(pane1, BorderLayout.CENTER);
contentPane.add(pane2, BorderLayout.SOUTH);
pane1.setLayout(new FlowLayout());
JTextField text1 = new JTextField(5);
JTextField text2 = new JTextField(5);
JTextField text3 = new JTextField(5);
pane1.add(text1);
pane1.add(new JLabel("+"));
pane1.add(text2);
pane1.add(new JLabel("="));
pane1.add(text3);
pane2.setLayout(new FlowLayout());
JButton button1 = new JButton("확인");
JButton button2 = new JButton("취소");
pane2.add(button1);
pane2.add(button2);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ConfirmButtonActionListener actionListener1 = new ConfirmButtonActionListener(text1, text2, text3);
//확인 버튼을 처리하는 이벤트 리스너 클래스를 불러옴
CancelButtonActionListener actionListener2 = new CancelButtonActionListener(text1, text2, text3);
//취소 버튼을 처리하는 이벤트 리스너 클래스를 불러옴
button1.addActionListener(actionListener1);
button2.addActionListener(actionListener2);
frame.pack();
frame.setVisible(true);
}
}
'자바의 기초문법' 카테고리의 다른 글
HashMap을 이용한 메뉴, 가격 수정 문제 (0) | 2016.01.04 |
---|---|
자바 네트워크 통신 프로그래밍: ServerSocket, socket (0) | 2015.12.29 |
GUI 프로그래밍: Swing (0) | 2015.12.29 |
멀티스레드 프로그래밍: Thread, Runnable (0) | 2015.12.28 |
객체의 직렬화와 역직렬화 (0) | 2015.12.28 |