Swing은 뭐지
layout: single
title: "Swing은 뭐지"
date: 2020-04-12 19:00:00 +0900
authour : Jinho
Swing?
스윙은 자바에서 GUI(Graphic User Interface)를 구현하기 위해 JDK에서 기본적으로 제공하는 개발 툴킷이다.
Java AWT | Java Swing | |
---|---|---|
플랫폼과의 관계 | 플랫폼과 무관(독립적) | 플랫폼에 종속적 |
컴포넌트 | 무겁다. 종류가 적다. | 가볍다. 종류가 다양하다. |
룩앤필 | pluggable 룩앤필 제공x | pluggalbe 룩앤필 제공o |
MVC | MVC패턴 따르지 않음 | MVC패턴을 따른다. |
와 내가 마크다운으로 표도 만들 수 있게 되다니… 새삼 놀랍다
MVC 패턴이란?
Model View Controler
- Model : 데이터를 가져와 어떤 작업을 처리하거나 처리한 작업 저장
- View : 데이터를 보여줌
- Controller : 뷰와 모델 사이의 흐름 제어
Swing GUI 프로그램 만드는 법
- 프레임 만들기
- 프레임에 스윙 컴포넌트 붙이기
- main함수 만들기
JPanel 이란?
- JFrame : 메인 프레임
- JPanle : 보조 프레임
- JLabel : 기능을 수행하는 컴포넌트
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
public class Project extends JFrame{
public void main(){
//프레임 설정
this.setSize(500,500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel NorthPanel = new JPanel(new BorderLayout());
JPanel CenterPanel = new JPanel(new GridLayout(0,3));
JPanel SouthPanel = new JPanel(new BorderLayout());
// 상단부
JLabel Title = new JLabel("Vending Machine");
Title.setFont(Title.getFont().deriveFont(20.0f));
Title.setHorizontalAlignment(SwingConstants.CENTER);
Title.setBounds(12, 10, 460, 29);
this.getContentPane().add(Title);
CenterPanel.add(new MenuPanel("imagePath"));
CenterPanel.add(new MenuPanel("imagePath"));
CenterPanel.add(new MenuPanel("imagePath"));
SouthPanel.add(new ListPanel(),BorderLayout.CENTER);
SouthPanel.add(new InputPanel(),BorderLayout.SOUTH);
NorthPanel.setPreferredSize(new Dimension(50,50));
CenterPanel.setPreferredSize(new Dimension(250,250));
SouthPanel.setPreferredSize(new Dimension(150,150));
this.getContentPane().add(NorthPanel,BorderLayout.NORTH); // 상단부
this.getContentPane().add(CenterPanel,BorderLayout.CENTER);
this.getContentPane().add(SouthPanel,BorderLayout.SOUTH); // 하단부분
this.setVisible(true);
}
///중단부
public class MenuPanel extends JPanel {
JButton button1 = new JButton("buy");
JButton button2 = new JButton("reduce");
public MenuPanel(String imagePath) {
this.setLayout(new BorderLayout());
JLabel label = new JLabel(imagePath);
this.add(label, BorderLayout.CENTER);
this.setBorder(new LineBorder(Color.BLACK));
JPanel buttonPanel = new JPanel();
buttonPanel.add(button2);
buttonPanel.add(button1);
this.add(buttonPanel, BorderLayout.SOUTH);
}
}
//하단
public class ListPanel extends JPanel{
JTextField List = new JTextField("contents");
JTextField Total = new JTextField("total fee");
JPanel BottomLabel = new JPanel(new BorderLayout());
public ListPanel() {
this.setLayout(new BorderLayout());
this.add(List,BorderLayout.CENTER);
BottomLabel.add(Total,BorderLayout.EAST);
this.add(BottomLabel,BorderLayout.SOUTH);
}
}
public class InputPanel extends JPanel {
JButton Purchase = new JButton("buy");
JTextField Price = new JTextField("blabla");
public InputPanel(){
this.setLayout(new BorderLayout());
this.add(Purchase,BorderLayout.EAST);
this.add(Price,BorderLayout.CENTER);
}
}
public static void main(String args[]) {
Project project = new Project();
project.main();
}
}
Leave a comment