Language/Java

[자바API_AWT/Swing] GUI (Graphic User Interface)의 기초

simDev1234 2022. 3. 26. 00:46

1. GUI란?

- GUI는 사용자가 컴퓨터와 눈에 보이는(Graphic) 상호작용(interface)을 할 수 있게 한 것
- GUI의 종류 : AWT패키지, Swing패키지

AWT(Abstract Windowing Toolkit)
- AWT은 중량 컴포넌트(heavy weight)이며, 운영체제의 자원을 사용한다. 운영체제에 부담되나 속도 빠르다.

Swing
- Swing은 AWT를 확장한 경량 컴포넌트(Light weight)이며, 다양한 플랫폼에도 사용할 수 있게 했다.

 

2. GUI의 전체 구조도

- 이미지 참조 링크 : https://myeonguni.tistory.com/1006

참조 : 명우니 닷컴

 

3. 컴포넌트란?

- 윈도우창에 들어가는 모든 독립적인 단위모듈컴포넌트라고 한다.
- 이 중에서 다른 컴포넌트를 하나로 묶어주는 컴포넌트를 컨테이너라 한다.
- 컨테이너 컴포넌트의 조상은 AWT패키지의 Cotainer이다.
- 컨테이너 외의 컴포넌트들의 조상은 Swing패키지 내에서 JComponent이다.

3-1) Container와 JComponent의 주요 메소드

(AWT) Container

관련 메소드
컴포넌트 getter
컴포넌트 추가
getComponent() : Component
add(Component comp) : Component
add(String name, Component comp) : Component
add(Component comp, int index) : Component
add(Component comp, Object constraints) : void
add(Component comp, Object constraints, int index) : void
컴포넌트 제거 remove(int index) : void
remove(Component comp) : void
removeAll() : void
레이아웃(배치방식) 설정 setLayout(LayoutManager mgr) : void

 

(Swing) JComponent

- 참조 : https://m.blog.naver.com/rain483/220735906752

관련 메소드 설명
모양 void setForeground(Color)
void setBackground(Color)
void setOpaque(boolean)
void setFont(Font)
Font getFont()
전경색 설정
배경색 설정
불투명성 설정
폰트 설정
폰트 리턴
위치와 크기 int getWidth()
int getHeight()
int getX()
int getY()
Point getLocationOnScreen
void setLocation(int, int)
void setSize
폭 리턴
높이 리턴
x좌표 리턴
y좌표 리턴
스크린 좌표상에서의 컴포넌트 좌표
위치 지정
크기 지정
상태 void setEnable(boolean)
void setVisible(boolean)
boolean isVisible()
컴포넌트 활성화/비활성화
컴포넌트 보이기/숨기기
컴포넌트의 보이는 상태 리턴
컨테이너 Container getParent()
Contaienr getTopLevelAncestor()
부모 컨테이너 리턴
최상위 부모 컨테이너 리턴

 

3-2) Swing 컨테이너

- 컨테이너는 가벼운 컴포넌트들을 하나로 묶기 위해 사용된다.

종류 이름 내용 default 배치방식
최상위
컨테이너
JFrame 윈도우창 BoderLayout
JApplet 브라우저 내에서 작동하는 윈도우창 BoderLayout
JDiaglog 일종의 메세지창/
알럿과 같은 대화상자 형식의 애플리케이션에 적합
BoderLayout
JWindow (생략) (생략)
일반 JPanel 컴포넌트를 패널형식으로 묶음 FlowLayout

 

3-3) 가장 많이 사용되는 13가지 컴포넌트

- 코드 설명 번역 ) 가장 많이 사용하는 13가지 컴포넌트 https://www.educba.com/swing-components-in-java/

이름 그림/설명 코드
ImageIcon (생략) ImageIcon homeIcon = new ImageIcon(“src/images/home.jpg”);
JButton

<생성자의 인자>
- 문자열 또는 이미지
- 이미지+문자열도 가능
JButton okBtn = new JButton(“Ok”);

JButton homeBtn = new JButton(homeIcon);

JButton btn2 = new JButton(homeIcon, “Home”);
JLabel checkbox, button등에 붙는 라벨
*독자적으로 사용 가능
JLabel textLbl = new JLabel(“This is a text label.”);

JLabel imgLabel = new JLabel(homeIcon);
JComboBox

<생성자의 인자>
- 문자열 배열
String[] cityStrings = { "Mumbai", "London", "New York", "Sydney", "Tokyo" };
JComboBox cities = new JComboBox(cityList);
cities.setSelectedIndex(3);
JCheckBox

<생성자의 인자>
- 옆에 붙는 라벨 문자열
- default체크값(on= true)
CheckBox chkBox = new JCheckBox(“Show Help”, true);
JRadioButton


<생성자의 인자>
- 옆에 붙는 라벨 문자열
- default체크값(on= true)
*ButtonGroup으로 집합가능
ButtonGroup radioGroup = new ButtonGroup();
JRadioButton rb1 = new JRadioButton(“쉬움”, true);
JRadioButton rb2 = new JRadioButton(“중간”);
JRadioButton rb3 = new JRadioButton(“어려움”);
radioGroup.add(rb1);
radioGroup.add(rb2);
radioGroup.add(rb3);
JTextField

<생성자의 인자>
- 컴포넌트 설명하는 문자열
- 너비(=열의 갯수)
*글자수 제한 없음
JTextField txtBox = new JTextField(20);
JTextArea

<생성자의 인자>
- 컴포넌트 설명하는 문자열
- 높이(행 갯수), 너비(열 갯수)
JTextArea txtArea = new JTextArea(“This text is default text for text area.”, 5, 20);
JPasswordField
(extends JTextField)
JPasswordField pwdField = new JPasswordField(15);
var pwdValue = pwdField.getPassword();
JList
DefaultListItem cityList = new DefaultListItem();
cityList.addElement(“Mumbai”):
cityList.addElement(“London”):
cityList.addElement(“New York”):
cityList.addElement(“Sydney”):
cityList.addElement(“Tokyo”):
JList cities = new JList(cityList);
cities.setSelectionModel(ListSelectionModel.SINGLE_SELECTION);
JFileChooser
JFileChooser fileChooser = new JFileChooser();
JButton fileDialogBtn = new JButton(“Select File”);
fileDialogBtn.AddEventListner(new ActionListner(){
fileChooser.showOpenDialog();
})
var selectedFile = fileChooser.getSelectedFile();
JTabbedPane
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab(“Tab 1”, new JPanel());
tabbedPane.addTab(“Tab 2”, new JPanel());
JSlider
JSlider volumeSlider = new JSlider(0, 100, 50);
var volumeLevel = volumeSlider.getValue();

 

3-3) 그외

이름 그림
JProgressBar
JToolTip
JMenu

 




[참고]
- awt와 swing의 차이 - https://diaryofgreen.tistory.com/140
- 컴포넌트란? - https://mommoo.tistory.com/55
- 명우니닷컴 https://myeonguni.tistory.com/1006
- 가장 많이 사용하는 13가지 컴포넌트 https://www.educba.com/swing-components-in-java/
- JComboBox 콤보박스 - https://movefast.tistory.com/63
- JList https://lunaticlobelia.tistory.com/118
- JProgrssBar https://www.javatpoint.com/java-jprogressbar
- JToolTip https://log.hanjava.net/post/2466129264/jtooltip-%EC%9D%B4%EC%95%BC%EA%B8%B0-i
- JMenu https://www.javatpoint.com/java-jmenuitem-and-jmenu
- JDiaglog in java https://www.educba.com/jdialog-in-java/
- JFileChooser https://creatordev.tistory.com/51
- GUI개요 https://m.blog.naver.com/PostView.naver?isHttpsRedirect=true&blogId=sks6624&logNo=150164573882
- 알통몬의 인생 https://m.blog.naver.com/rain483/220735906752