데스크톱 애플리케이션부터 클라우드 서비스의 근간이 되는 웹 애플리케이션으로 진화하는 과정을 살펴본다. 그 첫 번째로 데스크톱 애플리케이션을 실습했다.
package lesson01.exam01;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
@SuppressWarnings("serial")
public class CalculatorFrame extends JFrame implements ActionListener {
//윈도우 기능 갖기 위해 JFrame클래스 상속
//=과 clear 버튼 클릭 이벤트를 처리하는(리스너) ActionListener인터페이스 구현
//선언
JTextField operand1 = new JTextField(4);
JTextField operand2 = new JTextField(4);
String[] operatorData = {"+", "-", "*", "/"};
JComboBox<String> operator = new JComboBox<String>(operatorData);
JButton equal = new JButton("=");
JTextField result = new JTextField(6);
JButton clear = new JButton("Clear");
//생성자
public CalculatorFrame() {
this.setTitle("Lesson01-Exam01");
Container contentPane = this.getContentPane();
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
contentPane.add(Box.createVerticalGlue());
//메소드 실행
contentPane.add(this.createInputForm());
contentPane.add(this.createToolBar());
contentPane.add(Box.createVerticalGlue());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setLocationRelativeTo(null);
}
@Override
public void actionPerformed(ActionEvent event) {
if (event.getSource() == equal) {
compute();
} else {
clearForm();
}
}
private void compute() {
double a = Double.parseDouble(operand1.getText());
double b = Double.parseDouble(operand2.getText());
double r = 0;
try {
switch (operator.getSelectedItem().toString()) {
case "+": r = a + b; break;
case "-": r = a - b; break;
case "*": r = a * b; break;
case "/":
if (b == 0) throw new Exception("0 으로 나눌 수 없습니다!");
r = a / b; break;
}
result.setText(Double.toString(r));
} catch (Exception err) {
JOptionPane.showMessageDialog(
null, err.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
private void clearForm() {
this.operand1.setText("");
this.operand2.setText("");
this.result.setText("");
}
//=에 이벤트 건다.
private Box createInputForm() {
Box box = Box.createHorizontalBox();
box.setMaximumSize(new Dimension(300, 30));
box.setAlignmentY(Box.CENTER_ALIGNMENT);
box.add(operand1);
box.add(operator);
box.add(operand2);
box.add(equal);
box.add(result);
equal.addActionListener(this);
return box;
}
//clear에 이벤트 건다.
private Box createToolBar() {
Box box = Box.createHorizontalBox();
box.add(clear);
clear.addActionListener(this);
return box;
}
public static void main(String[] args) {
CalculatorFrame app = new CalculatorFrame();
//생성자 실행
app.setVisible(true);
}
}
현재 CalculatorFrame.java에서 main 메소드를 실행하면

이러한 자바에서 기본으로 제공하는 Swing GUI 컴포넌트를 이용하여 만든 데스크톱 애플리케이션이 실행된다. main메소드에서 실행된 생성자에서 이벤트가 걸린 해당 메소드를 실행하여 버튼을 누르면 각 메소드에 걸린 이벤트의 종류에 따라 계산하거나 초기화하는 메소드를 실행한다.
이러한 데스크톱 애플리케이션은 PC에 설치되어 실행하기 때문에 빠르지만 배포가 번거롭고 보안이 취약하다는 단점이 있다. 그 해결 방안으로 자동 갱신이 있다. 즉 애플리케이션을 실행할 때 서버에 갱신된 버전이 있는지 조회 후, 만약 있다면 내려받아 재설치하는 방식이지만 여전히 보안이 취약하다는 단점이 있다.
참고도서 : https://freelec.co.kr/book/1674/
[열혈강의] 자바 웹 개발 워크북
[열혈강의] 자바 웹 개발 워크북
freelec.co.kr
'교재 실습 > 자바 웹 개발 워크북' 카테고리의 다른 글
6. 웹 애플리케이션 아키텍처의 특징 (1) | 2022.01.01 |
---|---|
5. 클라이언트·서버 아키텍처의 진화 (0) | 2021.12.30 |
4. 다중 클라이언트의 요청 처리 (2) | 2021.12.29 |
3. 클라이언트·서버 애플리케이션 (1) | 2021.12.28 |
1. 실습 환경설정 : Github repositories에 연동 (0) | 2021.12.27 |
댓글