Bibi's DevLog ๐ค๐
[Java - AWT ํ์ต์ ๋ฆฌ] ์๋ฐ์ GUIํ๋ก๊ทธ๋๋ฐ - (1) AWT์ปดํฌ๋ํธ, ๋ฉ๋ด ์ปดํฌ๋ํธ, ๋ ์ด์์ ๋งค๋์ ๋ณธ๋ฌธ
[Java - AWT ํ์ต์ ๋ฆฌ] ์๋ฐ์ GUIํ๋ก๊ทธ๋๋ฐ - (1) AWT์ปดํฌ๋ํธ, ๋ฉ๋ด ์ปดํฌ๋ํธ, ๋ ์ด์์ ๋งค๋์
๋น๋น bibi 2020. 11. 23. 18:09์ด ๊ธ์ ์๋ ๋งํฌ ๋ฌธ์๋ฅผ ๋ณด๋ฉฐ ๊ณต๋ถํ ๊ฒ์ ์์ฝ์ ๋ฆฌํ ๊ฒ์ ๋๋ค. ๊ฐ์ฌํฉ๋๋ค๐โ๏ธ
'์๋ฐ์ ์ ์' ๋จ๊ถ์ฑ ๋์ ๋ธ๋ก๊ทธ ์๋ฃ - AWT
์๋ ๊นํ๋ธ ๋งํฌ์์ ๊ฐ์ ๋ด์ฉ์ ๋ ํธํ๊ฒ ๋ณผ ์ ์์ต๋๋ค.
github.com/bibi6666667/Codesquad-Cocoa-JAVA/blob/main/mission/src/mission6/AWT.md
AWT
- AWT๋?
- AWT = Abstract Window Toolkit๋ก, ์๋ฐ์ Window GUI ํ๋ก๊ทธ๋๋ฐ ๋๊ตฌ์ด๋ค.
- GUI = Graphical User Interface. ๊ทธ๋ํฝ ๊ธฐ๋ฐ์ ์ฌ์ฉ์ ์ธํฐํ์ด์ค. ์ฌ์ฉ์๊ฐ ๊ทธ๋ํฝ์ ํตํด ํ๋์จ์ด์ ์ํธ์์ฉํ๋ ํ๊ฒฝ. (๋ง์ฐ์ค ๋ฑ์ ํตํด).
- GUI์ ์์ : ์๋์ฐ, ์คํฌ๋กค๋ฐ, ์์ด์ฝ ์ด๋ฏธ์ง, ๋ฒํผ ๋ฑ์ด ์๋ค.
- GUI๋ CUI (Character User Interface, ํ ์คํธ ๊ธฐ๋ฐ์ ์ฌ์ฉ์ ์ธํฐํ์ด์ค) ์ ๋์กฐ์ ์ธ ๊ฐ๋ ์ด๋ค.
- AWT๋ OS์ ์ปดํฌ๋ํธ๋ค์ ๊ทธ๋๋ก ์ฌ์ฉํ๋ค. ๋ฐ๋ผ์ AWT์์ฉํ๋ก๊ทธ๋จ์ ์ธํ์ ์คํ๋๋ OS(์๋์ฐ/iOS..)์ ๋ฐ๋ผ ๋ค๋ฅด๊ฒ ๋ํ๋๋ค.
Swing
- Swing์ AWT๋ฅผ ํ์ฅํ ๊ฒ์ด์ง๋ง AWT๋ณด๋ค ๋ค์, ํ๋ถํ ์ปดํฌ๋ํธ๋ฅผ ์ ๊ณต. - ์ต๊ทผ์ JavaFX๊ฐ ๋ ์ข๋ค๊ณ .
- Swing๊ณผ AWT์ ์ฌ์ฉ๋ฒ์ ๊ฑฐ์ ๋น์ทํ๋ฏ๋ก AWTํ๋ก๊ทธ๋๋ฐ์ ๋จผ์ ์ตํ๋ฉด ๋๋ค.
AWT์ ๊ตฌ์ฑ
AWT ํจํค์ง
-
(
import java.awt.*;
๋ก๋ ์ ์ธํ ์ ์๋ค) -
ํจํค์ง๋ช ์ด
java.awt
๋ก ์์ํ๋ฉด ๋ชจ๋ AWT๊ด๋ จ ํจํค์ง์ด๋ค.java.awt
: AWT๋ฅผ ์ด์ฉํ GUI์ดํ๋ฆฌ์ผ์ด์ ์์ฑ์ ํ์ํ ๊ธฐ๋ณธ์ ํด๋์ค์ ์ปดํฌ๋ํธ ์ ๊ณต.java.awt.event
: GUI์ดํ๋ฆฌ์ผ์ด์ ์์ ๋ฐ์ํ๋ ์ด๋ฒคํธ๋ฅผ ์ฒ๋ฆฌํ๋๋ฐ ํ์ํ ํด๋์ค,์ธํฐํ์ด์ค ์ ๊ณต.java.awt.datatransfer
: ์ดํ๋ฆฌ์ผ์ด์ ๋ด/์ดํ๋ฆฌ์ผ์ด์ ๊ฐ ๋ฐ์ดํฐ ์ ์ก ๊ตฌํ์ ํ์ํ ํด๋์ค, ์ธํฐํ์ด์ค ์ ๊ณต.java.awt.dnd
: ๋์ด๋๊ธฐ=๋๋๊ทธ ์ค ๋๋กญ(Drag and Drop) ๊ตฌํ์ ํ์ํ ํด๋์ค ์ ๊ณต.java.awt.font
: ํฐํธ์ ๊ด๋ จ๋ ํด๋์ค,์ธํฐํ์ด์ค๋ฅผ ์ ๊ณต.java.awt.image
: ์ด๋ฏธ์ง ์์ฑ, ๋ณ๊ฒฝ์ ์ฌ์ฉ๋๋ ํด๋์ค ์ ๊ณต.java.awt.print
: ์ถ๋ ฅ์ ๊ด๋ จ๋ ํด๋์ค์ ์ธํฐํ์ด์ค ์ ๊ณต.
์ปดํฌ๋ํธ Component
- ๋ชจ๋ ์ปดํฌ๋ํธ์ ์กฐ์์ Componentํด๋์ค. (โป์์ธ : ๋ฉ๋ด ๊ด๋ จ ์ปดํฌ๋ํธ)
- Componentํด๋์ค์ ์กฐ์์ Objectํด๋์ค.
- ์ปดํฌ๋ํธ(AWT์ปดํฌ๋ํธ) : GUI์์ฉํ๋ก๊ทธ๋จ์ ํ๋ฉด์ ๊ตฌ์ฑํ๋ ๋ฐ ์ฌ์ฉ๋๋, Componentํด๋์ค์ ๊ทธ ์์ ํด๋์ค๋ค.
- ์๋์ฐ, ์คํฌ๋กค๋ฐ, ๋ฒํผ, ์ฒดํฌ๋ฐ์ค, ๋ฆฌ์คํธ .. ๋ฑ์ด ์๋ค.
- ์ผ๋ฐ ์ปดํฌ๋ํธ : (์ต์์์กฐ์ : Component).
- Button, Label, Checkbox, List, Choice, Scrollbar, Dialog(๋ํ์์์์), Frame, TextArea/TextField, FileDialog(ํ์ผ์ด๊ธฐ)..
- ๋ฉ๋ด ์ปดํฌ๋ํธ : (์ต์์์กฐ์ : MenuComponent)
- MenuBar, MenuItem(Menu-PopupMenu, CheckboxMenuItem) ..
์ปดํฌ๋ํธ์ ์ฃผ์ ๋ฉ์๋
Color getBackground()
: ์ปดํฌ๋ํธ์ ๋ฐฐ๊ฒฝ์์ ์ป๋๋คvoid setBackground(Color c)
: ์ปดํฌ๋ํธ์ ๋ฐฐ๊ฒฝ์์ ์ง์ ๋ ์์ผ๋ก ๋ณ๊ฒฝํ๋ค.Cursor getCursor()
: ์ปดํฌ๋ํธ์ ์ง์ ๋ ์ปค์๋ฅผ ์ป๋๋ค.void setCursor(Cursor c)
: ์ปดํฌ๋ํธ์ ์ปค์(๋ง์ฐ์คํฌ์ธํฐ) ๋ฅผ ์ง์ ํ๋ค.Font getFont()
: ์ปดํฌ๋ํธ์ ์ง์ ๋์ด ์๋ Font๋ฅผ ์ป๋๋ค.void setFont(Font f)
: ์ปดํฌ๋ํธ์ Font๋ฅผ ์ง์ ํ๋ค.Color getForeground()
: ์ปดํฌ๋ํธ์ ์ ๊ฒฝ์์ ์ป๋๋ค.void setForeground(Color c)
: ์ปดํฌ๋ํธ์ ์ ๊ฒฝ์์ ์ง์ ํ๋ค.int getHeight()
: ์ปดํฌ๋ํธ์ ๋์ด๋ฅผ ์ป๋๋ค.int getWidth()
: ์ปดํฌ๋ํธ์ ํญ์ ์ป๋๋ค.void setBounds(int x, int y, int witdh, int height)
: ์ปดํฌ๋ํธ์ ์์น(x,y)์ ํฌ๊ธฐ(width, height)๋ฅผ ์ง์ ํ๋ค.Rectangle getBounds()
: ์ปดํฌ๋ํธ์ ์์น์ ํฌ๊ธฐ(Rectangle ๊ฐ์ฒด)๋ฅผ ์ป๋๋ค.Point getLocation()
: ์ปดํฌ๋ํธ์ ์์น๋ฅผ ์ป๋๋ค.void setLocation(int x, int y)
: ์ปดํฌ๋ํธ์ ์์น๋ฅผ ์ง์ ํ๋ค.Dimension getSize()
: ์ปดํฌ๋ํธ์ ํฌ๊ธฐ๋ฅผ ์ป๋๋ค.void setSize(int width, int height)
: ์ปดํฌ๋ํธ์ ํฌ๊ธฐ๋ฅผ ์ง์ ํ๋ค.boolean hasFocus()
: ์ปดํฌ๋ํธ๊ฐ ํ์ฌ focus๋ฅผ ๊ฐ๊ณ ์๋์ง ์๋ ค์ค๋ค.void requestFocus()
: ์ปดํฌ๋ํธ๊ฐ focus๋ฅผ ๊ฐ๋๋ก ํ๋ค.void paint(Graphics g)
: ์ปดํฌ๋ํธ๋ฅผ ํ๋ฉด์ ๊ทธ๋ฆฐ๋ค.void repaint()
: ์ปดํฌ๋ํธ๋ฅผ ํ๋ฉด์ ๋ค์ ๊ทธ๋ฆฐ๋ค.void setEnabled(boolean b)
: ์ปดํฌ๋ํธ๋ฅผ ์ฌ์ฉ๊ฐ๋ฅ(true)/๋ถ๊ฐ๋ฅ(false)ํ๊ฒ ํ๋ค.Container getParent()
: ์ปดํฌ๋ํธ๊ฐ ํฌํจ๋์ด์ ธ ์๋ ์ปจํ ์ด๋(parent)๋ฅผ ์ป๋๋ค.void setVisible(boolean b)
: ์ปดํฌ๋ํธ๊ฐ ํ๋ฉด์ ๋ณด์ด๊ฒ(true)/๋ณด์ด์ง ์๊ฒ(false)ํ๋ค.
์ปจํ ์ด๋ Container
- ์ปจํ ์ด๋ : Component์ ์์์ธ Container ๋๋ Container์ ์์๋ค.
- ์ปจํ ์ด๋๋ ๋ค๋ฅธ ์ปดํฌ๋ํธ ๋๋ ์ปจํ ์ด๋๋ฅผ ํฌํจํ ์ ์๋ค.
-
๋ ๋ฆฝ์ ์ปจํ ์ด๋ : ๋ ๋ฆฝ์ ์ผ๋ก ์ฌ์ฉ๋ ์ ์์ & ๋ค๋ฅธ ์ปดํฌ๋ํธ ๋๋ ์ข ์์ ์ปจํ ์ด๋๋ฅผ ํฌํจํ ์ ์๋ค.
Frame
- ๊ฐ์ฅ ์ผ๋ฐ์ ์ปจํ
์ด๋.
Window
์ ๋ชจ์์ด ๊ฐ๋ค. - titlebar์ ํฌ๊ธฐ์กฐ์ ๋ฒํผ, ๋ซ๊ธฐ๋ฒํผ์ด ์๋ค.
- ๋ฉ๋ด๋ฅผ ์ถ๊ฐํ ์ ์๋ค.
Window
Frame
์ ์กฐ์์ด๋ค.- ์ปดํฌ๋ํธ๋ฅผ ๋ด์ ์ ์๋ ํ๋ฉด๊ณต๊ฐ๋ง์ ๊ฐ๋๋ค.
- ๊ฒฝ๊ณ์ , titlebar, ํฌ๊ธฐ์กฐ์ ๋ฒํผ, ๋ซ๊ธฐ๋ฒํผ ์๊ณ ๋ฉ๋ด ์ถ๊ฐ๋ ๋ถ๊ฐ๋ฅ.
Dialog
(๋ํ์์์์)- titlebar์ ๋ซ๊ธฐ๋ฒํผ์ ์์ง๋ง, ๋ฉ๋ด๋ ์๋ค.
- ๊ธฐ๋ณธ์ ์ผ๋ก ํฌ๊ธฐ๋ฅผ ๋ณ๊ฒฝํ ์ ์๋ค.
- ์ฃผ๋ก ์ฌ์ฉ์์๊ฒ ๋ฉ์์ง๋ฅผ ๋ณด์ฌ์ฃผ๊ฑฐ๋ ์๋ต์ ๋ฐ๋ ๋ฐ ์ฌ์ฉํ๋ค.
-
์ข ์์ ์ปจํ ์ด๋ : ๋ ๋ฆฝ์ ์ผ๋ก ์ฌ์ฉ๋ ์ ์์ & ๋ค๋ฅธ ์ปจํ ์ด๋์ ํฌํจ๋์ด์ผ๋ง ํ๋ค.
Panel
- ํ๋ฉด๊ณต๊ฐ.
Frame
์ฒ๋ผ ์ฌ๋ฌ ์ปดํฌ๋ํธ๋ฅผ ๋ด์ ์ ์์ผ๋ ๋จ๋ ์ฌ์ฉ์ ๋ถ๊ฐ๋ฅ. ScrollPane
(์คํฌ๋กคํ์ธ)Panel
๊ณผ ๊ฐ์ ํ๋ฉด๊ณต๊ฐ์ด๋ ๋จ ํ๋์ ์ปดํฌ๋ํธ๋ง ํฌํจํ ์ ์์.- ์์ ๋ณด๋ค ํฐ ์ปดํฌ๋ํธ๊ฐ ํฌํจ๋๋ฉด ์คํฌ๋กค๋ฐ๊ฐ ์๋์ผ๋ก ๋ํ๋จ.
์ปจํ ์ด๋์ ์ฃผ์ ๋ฉ์๋
-
์ปจํ ์ด๋์๋ ์ฌ๋ฌ ๊ฐ์ ์ค๋ฒ๋ก๋ฉ๋ add๋ฉ์๋๋ค์ด ์๋ค.
- ๋ฐ๋ผ์ ์ปจํ ์ด๋์ add๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ ๊ฒ ๋ง์ผ๋ก๋ ๋ค๋ฅธ ์ปดํฌ๋ํธ๋ค์ ํฌํจ์ํฌ ์ ์๋ค.
-
์ปจํ ์ด๋์ ํฌํจ๋ ์ปดํฌ๋ํธ๋ค์ ๊ธฐ๋ณธ์ ์ผ๋ก ์ปจํ ์ด๋์ ๋ฐฐ๊ฒฝ์&์ ๊ฒฝ์, ํฐํธ ๋ฑ์ ์ค์ ์ ๋ฐ๋ฅธ๋ค.
-
Component[] getComponents()
: ์ปจํ ์ด๋์ ํฌํจ๋์ด ์๋ ๋ชจ๋ ์ปดํฌ๋ํธ๋ฅผ ์ป๋๋ค. -
Component getComponent(int n)
: ์ปจํ ์ด๋์ n๋ฒ์จฐ๋ก ์ถ๊ฐ๋ ์ปดํฌ๋ํธ๋ฅผ ์ป๋๋ค. -
Component getComponentAt(int x, int y)
: ์ปจํ ์ด๋์ ์ง์ ์์น(x, y)์ ์๋ ์ปดํฌ๋ํธ๋ฅผ ์ป๋๋ค. -
Component add(Component comp)
: ์ปจํ ์ด๋์ ์ปดํฌ๋ํธ๋ฅผ ์ถ๊ฐํ๋ค. -
void remove(Component comp)
: ์ปจํ ์ด๋์์ ์ง์ ๋ ์ปดํฌ๋ํธ๋ฅผ ์ ๊ฑฐํ๋ค. -
Insets getInsets()
: ์ปจํ ์ด๋์ ๊ฒฝ๊ณ์ ํฌ๊ธฐ๋ฅผ ์ ์ ์๋ Inset๊ฐ์ฒด๋ฅผ ์ป๋๋ค. -
LayoutManager getLayout()
: ์ปจํ ์ด๋์ ์ค์ ๋์ด ์๋ LayoutManager๋ฅผ ์ป๋๋ค. -
void setLayout(LayoutManager mgr)
: ์ปจํ ์ด๋์ LayoutManager๋ฅผ ์ค์ ํ๋ค.
AWT์ ์ฃผ์ ์ปดํฌ๋ํธ
์ด์ AWT๋ฅผ ์ด์ฉํ GUIํ๋ก๊ทธ๋๋ฐ์ ์ฌ์ฉ๋๋ ์ปดํฌ๋ํธ์ ์ข
๋ฅ ๋ฐ ๊ธฐ๋ณธ์ ๊ธฐ๋ฅ์ ์ค์ต์ผ๋ก ์์๋ณธ๋ค.
โ
AWT๋ฅผ ์์ํ๋ ๋ฐ ๋ณ๋์ ์ค์น๋ ํ์์์ผ๋ฉฐ, import java.awt.*;
๋ก ์ํฌํธ๋ง ํด ์ฃผ๋ฉด ๋๋ค.
Frame ํ๋ ์
- Frame์ GUIํ๋ก๊ทธ๋๋ฐ์ ๊ฐ์ฅ ๋ํ์ ์ปดํฌ๋ํธ์ด์, ๋ค๋ฅธ ์ปดํฌ๋ํธ๋ค์ ํฌํจํ ์ ์๋ ์ปจํ ์ด๋์ด๋ค.
- titlebar, ์ต๋ํ๋ฒํผ, ์ต์ํ๋ฒํผ, ๋ซ๊ธฐ๋ฒํผ์ ๊ฐ์ง๋ค.
- ํฌ๊ธฐ ์กฐ์ ์ด ๊ฐ๋ฅํ๋ค.
- ๋จ, ๋ซ๊ธฐ ๋ฒํผ์ ๋๋ฌ๋ ์๋ํ์ง ์๋๋ค. ์ด๋ฒคํธ์ฒ๋ฆฌ๋ฅผ ํตํด ๊ธฐ๋ฅ์ ๋ฐ๋ก ์ถ๊ฐํด์ฃผ์ด์ผ ๋์ํ๋ค.
- ์คํ์ ๋ฉ์ถ๋ ค๋ฉด ์ฝ์์ฐฝ์์ ์ ์ง๋ฅผ ๋๋ฅด๊ฑฐ๋ ์์ ๊ด๋ฆฌ์์์ ์ข ๋ฃํด์ผ ํ๋ค.
Frame์ ๋ฉ์๋
Frame(String title)
: Frame์ ์์ฑํ๋ค.title
์ Frame์ titlebar์ ๋ณด์ฌ์ง text๋ฅผ ๋งํ๋ค(ํ์)
String getTitle()
: titlebar์ ์๋ text๋ฅผ ์ป๋๋ค.void setTitle(String title)
: titlebar์ text๋ฅผ ๋ณ๊ฒฝํ๋ค.void setState(int state)
: Frame์ ์ํ๋ฅผ ๋ณ๊ฒฝํ ์ ์๋ค.state
์๋ ๋ ๊ฐ์ง ๊ฐ ์ค ํ๋๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
Frame.ICONIFIED
: Frame์ ์ต์ํ ์ํ๊ฐ ๋๊ฒ ํ๋ค.Frame.NORMAL
: Frame์ ์ ์์ํ(์ต์ํ ์ด์ ์ํ)๊ฐ ๋๊ฒ ํ๋ค.
int getState()
: Frame์ ํ์ฌ ์ํ๋ฅผ ์ ์ ์๋ค.void setResizable(boolean resizable)
: Frame์ ํฌ๊ธฐ๋ฅผ ๋ณ๊ฒฝ๊ฐ๋ฅ(true)/๋ถ๊ฐ๋ฅ(false) ํ๊ฒ ํ๋ค.- false์ผ ๋๋ ์ฌ์ฉ์๊ฐ Frame์ ํฌ๊ธฐ๋ฅผ ๋ณ๊ฒฝํ ์ ์๋ค.
Frame ์ฌ์ฉํ๊ธฐ
import java.awt.*;
public class FrameTest {
public static void main(String[] args) {
Frame f = new Frame("Login"); // Frame ๊ฐ์ฒด ์์ฑ. ๊ดํธ ์์ Frame์ title์ ํด๋น.
f.setSize(300, 200) ; // Frame์ ํฌ๊ธฐ๋ฅผ ์ค์ .
f.setVisible(true); // ์ค์ ์์
์ด ๋ชจ๋ ๋๋๋ฉด ๋งจ ๋ง์ง๋ง์ผ๋ก ํ๋ ์์ ํ๋ฉด์ ๋ณด์ด๊ฒ ํ๋ค.
// Frame๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค๊ณ ํ๋ฉด์ ๋ณด์ด์ง ์๋๋ค. ๋ฐ๋์ ์ด ์ต์
์ ์ถ๊ฐํด์ฃผ์ด์ผ ํ๋ค.
}
}
Frame์ ํ๋ฉด ์ค์์ ๋ํ๋๊ฒ ํ๊ธฐ
import java.awt.*;
public class FrameTest2 {
public static void main(String[] args) {
Frame f = new Frame("Login");
f.setSize(300,200);
Toolkit toolkit = Toolkit.getDefaultToolkit(); // ๊ตฌํ๋ Toolkit๊ฐ์ฒด๋ฅผ ์ป๋๋ค
Dimension screenSize = toolkit.getScreenSize(); // ํ๋ฉด์ ํฌ๊ธฐ๋ฅผ ๊ตฌํ๋ค.
// Frame์ ํ๋ฉด ๊ฐ์ด๋ฐ ์์นํ๊ฒ ํ๊ธฐ
// : Frame์ ์์น๋ฅผ ํ๋ฉดํฌ๊ธฐ์ ์ ๋ฐ๊ฐ์์ Frameํฌ๊ธฐ์ ์ ๋ฐ๊ฐ์ ๋บ ์์น๋ก ํ๋ค.
// x์ถ ์์น : ํ๋ฉด ๋๋น / 2 - Frame ๋๋น / 2
// y์ถ ์์น : ํ๋ฉด ๋์ด / 2 - Frame ๋์ด / 2
f.setLocation(screenSize.width/2 - 150, screenSize.height/2 - 100);
f.setVisible(true); // ๋ง์ง๋ง์ผ๋ก ์์ฑ๋ Frame์ ๋ํ๋๊ฒ ํ๋ค.
}
}
Button ๋ฒํผ
- Button : ์ฌ์ฉ์๊ฐ ํด๋ฆญํ์ ๋ ์ด๋ค ์์ ์ด ์ํ๋๋๋ก ํ ๋ ์ฌ์ฉ๋๋ค.
Button์ ๋ฉ์๋
Button(String label)
: ์ง์ ๋ label์ ๊ฐ์ง Button์ ์์ฑํ๋ค.label
: Button์์ ๋ํ๋ text๋ฅผ ๋งํ๋ค.
String getLabel()
: Button์ ๋ํ๋ ์๋ text๋ฅผ ์ป๋๋ค.void setLabel(String label)
: Button์ ๋ํ๋ ์๋ text๋ฅผ ๋ณ๊ฒฝํ๋ค.
Button ์ฌ์ฉํ๊ธฐ
import java.awt.*;
public class ButtonTest {
public static void main(String[] args) {
Frame f = new Frame("Login");
f.setSize(300,200);
Button b = new Button("ํ ์ธ");// "ํ ์ธ"์ด๋ผ๋ ๊ธ์๊ฐ ์๋ Button ์์ฑ
b.setSize(100,50); // Button์ ํฌ๊ธฐ ์ค์
f.add(b); // ์์ฑ๋ Button์ Frame์ ํฌํจ์ํจ๋ค.
f.setVisible(true);
}
}
๋ ์ด์์ ๋งค๋์ Layout Manager (Button ํฌ๊ธฐ ์ค์ )
์ ์์ ์์ ๋ฒํผ ํฌ๊ธฐ๋ฅผ ํ๋ ์๋ณด๋ค ์๊ฒ ์ค์ ํด๋ ํ๋ ์์ ๊ฝ ์ฐจ๋ ํ์์ด ๋ํ๋๋ค.
์ด๋ Frame์ ์ค์ ๋์ด ์๋ Layout Manager(๋ ์ด์์ ๋งค๋์ ) ๋๋ฌธ์ด๋ค.
- ์ปจํ ์ด๋์๋ ๋ ์ด์์๋งค๋์ ๋ฅผ ์ ํ์ ์ผ๋ก ์ค์ ํ ์ ์๋ค.
- ๋ ์ด์์๋งค๋์ ๋ ์ปจํ ์ด๋ ํฌ๊ธฐ๊ฐ ๋ณ๊ฒฝ๋ ๋ ๋ง๋ค, ์ปจํ ์ด๋์ ํฌํจ๋ ์ปดํฌ๋ํธ๋ค์ ํฌ๊ธฐ์ ์์น๋ฅผ ์๋์ผ๋ก ๊ด๋ฆฌํด ์ค๋ค.
- ์ฅ์ ) ํ๋ก๊ทธ๋๋จธ๊ฐ ์๋์ ์ผ๋ก ์ผ์ผ์ด ์ปจํ ์ด๋ ๋ด ์ปดํฌ๋ํธ ๋ฐฐ์น์ ์ ๊ฒฝ์ฐ์ง ์์๋ ๋๋ค.
- ๋ค์ํ ๋ ์ด์์ ๋งค๋์ ์ ์ข
๋ฅ์ ํน์ฑ์ด ์๋ค. - ์์ผ๋ก ๋ฐฐ์ฐ๊ฒ ๋ ๊ฒ์.
- Frame์ ๊ธฐ๋ณธ์ ์ผ๋ก ์ค์ ๋์ด ์๋ ๋ ์ด์์๋งค๋์ ๋ BorderLayout์ด๋ค.
- ์ง์ ๋ ๋ ์ด์์๋งค๋์ ์ค์ ์ ํด์ ํ๋ฉด ์ปดํฌ๋ํธ๊ฐ ์๋์ผ๋ก ๋ฐฐ์น๋์ง ์๋๋ค.
- ํ๋ก๊ทธ๋๋จธ๊ฐ ์ง์ ์ปดํฌ๋ํธ์ ํฌ๊ธฐ,์์น ๋ฑ์ ์ค์ ํด ์ฃผ์ด์ผ ํ๋ค.
setLocation()
,setSize()
๋ฅผ ํตํด ์์น์ ํฌ๊ธฐ๋ฅผ ์ค์ .
์๋๋ ๋ ์ด์์๋งค๋์ ๋ฅผ ํด์ ํ๊ณ ์ง์ ๋ฒํผ ํฌ๊ธฐ์ ์์น๋ฅผ ์ค์ ํ๋ ์์ ์ด๋ค.
import java.awt.*;
public class ButtonTest2 {
public static void main(String[] args) {
Frame f = new Frame("Login");
f.setSize(300, 200);
f.setLayout(null); // ๋ ์ด์์๋งค๋์ ์ ์ค์ ์ ํด์ ํ๋ค.
Button b = new Button("ํ ์ธ");
b.setSize(100,50);
b.setLocation(100,75); // Frame๋ด์์ Button์ ์์น ์ค์
f.add(b);
f.setVisible(true);
}
}
Choice
- Choice : ์ฌ๋ฌ item์ด ์๋ ๋ชฉ๋ก์ ๋ณด์ฌ์ฃผ๊ณ , ๊ทธ ์ค ํ ๊ฐ์ง๋ฅผ ์ ํํ๋๋ก ํ๋ค.
- (์ฝค๋ณด๋ฐ์ค combo box / ๋๋๋ค์ด ๋ฆฌ์คํธ๋ฐ์ค drop-down listbox)
Choice์ ๋ฉ์๋
void add(String item)
: Choice์ item์ ์ถ๊ฐํ๋ค.void remove(String item)
: Choice์์ item์ ์ ๊ฑฐํ๋ค.void remove(int index)
: ์ง์ ๋ ์ธ๋ฑ์ค์ item์ ์ ๊ฑฐํ๋ค.- AWT์์๋ index๋ 0๋ฒ์งธ๋ถํฐ ์์ํ๋ค! ์ฒซ ๋ฒ์งธ item์ ์ธ๋ฑ์ค = 0.
void removeAll()
: Choice์ ๋ชจ๋ item์ ์ ๊ฑฐํ๋ค.void insert(String item, int index)
: ์ง์ ๋ ์ธ๋ฑ์ค์ item์ ์ถ๊ฐํ๋ค.String getItem(int index)
: ์ง์ ๋ ์ธ๋ฑ์ค์ item์ ์ป๋๋ค.int getItemCount()
: ํ์ฌ Choice์ ์ถ๊ฐ๋์ด ์๋ item์ด ๋ช ๊ฐ์ธ์ง ์๋ ค์ค๋ค.int getSelectedIndex()
: ํ์ฌ ์ ํ๋์ด์ ธ ์๋ item์ index๊ฐ์ ์ป๋๋ค.String getSelectedItem()
: ํ์ฌ ์ ํ๋์ด์ ธ ์๋ item์ ์ป๋๋ค.
Choice ์ฌ์ฉํ๊ธฐ
import java.awt.*;
public class ChoiceTest {
public static void main(String[] args) {
Frame f = new Frame("Choice - Select");
f.setSize(300, 200);
f.setLayout(null);
Choice day = new Choice(); // Choice ๊ฐ์ฒด ์์ฑ
day.add("SUN"); // .add()๋ฅผ ํตํด Choice์ ๋ชฉ๋ก์ ๋ํ๋ item๋ค์ ์ถ๊ฐ.
day.add("MON");
day.add("TUE");
day.add("WED");
day.add("THU");
day.add("FRI");
day.add("SAT");
day.setSize(100,50);
day.setLocation(100,70);
f.add(day); //
f.setVisible(true);
}
}
List ๋ฆฌ์คํธ
- List๋ Choice์ฒ๋ผ ๋ชฉ๋ก ์ค ์ํ๋ ํญ๋ชฉ์ ์ ํํ๊ฒ ํ ๋ ์ฌ์ฉํ๋ค.
- List์ Choice์ ๋ค๋ฅธ ์ :
- List๋ ์ฒ์๋ถํฐ ๋ชจ๋ item๋ชฉ๋ก์ ๋ณด์ฌ์ค๋ค
- List๋ item๋ชฉ๋ก ์ค์์ ํ๋ ๋๋ ์ฌ๋ฌ ๊ฐ๋ฅผ ์ ํํ ์ ์๋ค.
List์ ๋ฉ์๋
-
List(int rows, boolean multipleMode)
rows
: ๋ช ์ค ์ง๋ฆฌ ํฌ๊ธฐ์ List๋ฅผ ๋ง๋ค์ง ์ง์ .multipleMode
: ๋ค์ค์ ํ์ด ๊ฐ๋ฅํ๊ฒ ์ง์ ํ๋ค. (true - ๋ค์ค์ ํ ๊ฐ๋ฅ)
-
List(int rows)
- List์ ๋ณด์ฌ์ค item ์๋ง ์ง์ ํ๋ค.
- ์ ๋ฉ์๋์ ๋ฌ๋ฆฌ multipleMode๊ฐ์ false๋ก ์๋์ง์ ๋์ด ๋ค์ค์ ํ์ด ๋ถ๊ฐ๋ฅํ ์ ์ธ๋ฐฉ๋ฒ์ด๋ค.
-
List()
- rows์ ๊ฐ์ ๊ธฐ๋ณธ๊ฐ์ธ 4๋ก ์ง์ ๋๋ค.
- multipleMode์ ๊ฐ์ false๋ก ์๋์ง์ ๋์ด ๋ค์ค์ ํ์ด ๋ถ๊ฐ๋ฅํ ์ ์ธ๋ฐฉ๋ฒ.
-
void add(String item)
: item์ List์ ์ถ๊ฐํ๋ค. -
void add(String item, int index)
: ์ง์ ์ธ๋ฑ์ค์ item์ ์ถ๊ฐํ๋ค. -
void replaceItem(String newValue, int index)
: ์ง์ ์ธ๋ฑ์ค์ item์ newValue๋ก ๋ฐ๊พผ๋ค. -
void remove(String item)
: List์์ ํด๋น item ์ ๊ฑฐ -
void remove(int index)
: ์ง์ ์ธ๋ฑ์ค์ item ์ ๊ฑฐ -
void removeAll()
: List์ ๋ชจ๋ item ์ ๊ฑฐ -
int getRows()
: List์ ์คํฌ๋กค ์์ด ๋ณผ ์ ์๋ ์์ดํ ์ ์๋ฅผ ์ป๋๋ค. -
int getItemCount()
: List์ ์๋ item์ด ๋ชจ๋ ๋ช ๊ฐ์ธ์ง ์๋ ค์ค๋ค. -
String getItem(int index)
: ์ง์ ์ธ๋ฑ์ค์ item์ ์ป๋๋ค. -
String[] getItems()
: List์ ๋ชจ๋ item์ ์ป๋๋ค. -
void select(int index)
: ์ง์ ์ธ๋ฑ์ค์ item์ ์ ํํ๋ค. -
void deselect(int index)
: ์ง์ ์ธ๋ฑ์ค์ item์ ์ ํํด์ ํ๋ค. -
int getSelectedIndex()
: ํ์ฌ ์ ํ๋ item์ ์ธ๋ฑ์ค๋ฅผ ์ป๋๋ค. -
int[] getSelectedIndexes()
: ํ์ฌ ์ ํ๋ item๋ค์ ์ธ๋ฑ์ค๋ฅผ ๋ชจ๋ ์ป๋๋ค.- multipleMode๊ฐ true์ธ ๊ฒฝ์ฐ ์ฌ์ฉ ๊ฐ๋ฅ.
-
String getSelectedItem()
: ํ์ฌ ์ ํ๋ item์ ์ป๋๋ค. -
String[] getSelectedItems()
: ํ์ฌ ์ ํ๋ item๋ค์ ์ป๋๋ค.- multipleMode๊ฐ true์ธ ๊ฒฝ์ฐ ์ฌ์ฉ ๊ฐ๋ฅ.
-
boolean isIndexSelected(int index)
: ์ง์ ์ธ๋ฑ์ค์ item์ด ์ ํ๋์ด์๋์ง ์๋ ค์ค๋ค. -
void setMultipleMode(boolean b)
: List๋ฅผ multipleMode๋ก ํ ๊ฒ์ธ์ง ์ค์ .- b๊ฐ true์ด๋ฉด multipleMode๊ฐ ๋๋ค.
List ์ฌ์ฉํ๊ธฐ
import java.awt.*;
public class ListTest {
public static void main(String[] args) {
Frame f = new Frame("List - Select");
f.setSize(300,200);
f.setLayout(null);
List selectOne = new List(6); // 6๊ฐ ๋ชฉ๋ก์ ๊ฐ์ง List ์์ฑ
selectOne.setLocation(20,40);
selectOne.setSize(100,120);
selectOne.add("Student");
selectOne.add("Teacher");
selectOne.add("Driver");
selectOne.add("Computer Programmer");
selectOne.add("Sales Man");
selectOne.add("Musician");
selectOne.add("Director");
// ์์ฑ์์ ๋ ๋ฒ์งธ ์ธ์๊ฐ์ด true์ด๋ฉด multipleMode๊ฐ ๋๋ค.
List selectMany = new List(6, true);
selectMany.setLocation(150,40);
selectMany.setSize(100,120);
selectMany.add("Student");
selectMany.add("Teacher");
selectMany.add("Driver");
selectMany.add("Computer Programmer");
selectMany.add("Sales Man");
selectMany.add("Musician");
selectMany.add("Director");
f.add(selectOne);
f.add(selectMany);
f.setVisible(true);
}
}
Label ๋ ์ด๋ธ
- Label์ ํตํด ํ๋ฉด์ ๊ธ์๋ฅผ ํ์ํ ์ ์๋ค.
- ์ค๋ช ์ด๋ ๋ฉ์์ง๋ฅผ ํ๋ฉด์ ๋ํ๋ผ ๋ ์ฃผ๋ก ์ฌ์ฉํ๋ค.
Label์ ์ฃผ์ ๋ฉ์๋
-
Label(String text, int alignment)
text
: ํ๋ฉด์ ๋ํ๋ผ ๊ธ์alignment
: text์ ์ ๋ ฌ๋ฐฉ์Label.LEFT
Label.CENTER
Label.RIGHT
์ค ์ ํํด ์ง์
-
Label(String text)
text
๋ฅผ ๋ํ๋ด๋alignment
๊ธฐ๋ณธ๊ฐ์ธ ์ผ์ชฝ์ ๋ ฌ๋ก ์ค์ ๋๋ค.
-
String getText()
: Label์ text๋ฅผ ์ป์ด์จ๋ค -
void setText(String text)
: Label์ text๋ฅผ ์ฃผ์ด์ง ๊ฐ์ผ๋ก ๋ณ๊ฒฝ -
void setAlignment(int alignment)
: Label์ text์ ๋ ฌ์ ์ง์
Label ์ฌ์ฉํ๊ธฐ
- Frame์ ๋ ์ด์์๋งค๋์ ๊ฐ ์ค์ ๋์ง ์์ ๊ฒฝ์ฐ, ์ปดํฌ๋ํธ๋ค์ ํฌ๊ธฐ๋ฅผ ์ง์ ํ์ง ์์ผ๋ฉด ํ๋ฉด์ ํ์๋์ง ์๋๋ค.
.setSize()
๋๋.setBounds()
๋ฅผ ํตํด ์ปดํฌ๋ํธ ํฌ๊ธฐ๋ฅผ ์ง์ ํด์ผ ํ๋ค.
import java.awt.*;
public class LabelTest {
public static void main(String[] args) {
Frame f = new Frame("Login");
f.setSize(300, 200);
f.setLayout(null);
// Label์ ์์ฑํ๊ณ , ํฌ๊ธฐ์ ์์น ์ง์ .
Label id = new Label("ID :");
id.setBounds(50,50, 30,10);
// 50,50 ์์น์ ๊ฐ๋ก๊ธธ์ด 30, ์ธ๋ก๊ธธ์ด 10
Label pwd = new Label("Password : ");
pwd.setBounds(50,65, 100,10);
f.add(id);
f.add(pwd);
f.setVisible(true);
}
}
Checkbox, CheckboxGroup
- Checkbox : boolean์ฒ๋ผ true/false(๋๋ on/off) ์ค ํ๋์ ๊ฐ์ ๊ฐ์ง ์ ์๋ ์ปดํฌ๋ํธ์ด๋ค.
- CheckboxGroup์ผ๋ก ๊ทธ๋ฃนํํ์ง ์์ผ๋ฉด ๋ชจ๋ ๊ฐ์ ์ค๋ณต์ ํ ๊ฐ๋ฅํ๋ค.
- CheckboxGroup : ์ฌ๋ฌ ๊ฐ๋ค ์ค ํ๋๋ฅผ ์ ํํ๋ radio button์ ๋ง๋ค ์ ์๋ค.
- ๊ฐ์ ๊ทธ๋ฃน ๋ด Checkbox์ค์์ ํ ๊ฐ์ง๋ง ์ ํ๊ฐ๋ฅํ๋ค.
Checkbox์ ๋ฉ์๋
-
Checkbox(String text, boolean state)
: Checkbox ๊ฐ์ฒด ์์ฑtext
- Checkbox ์ ํจ๊ป ๋ณด์ฌ์ง text๋ฅผ ์ง์ state
- true์ด๋ฉด Checkbox๊ฐ ์ ํ๋ ์ํ๋ก, false์ด๋ฉด ์ ํํด์ ๋ ์ํ๋ก ์์ฑ.
-
Checkbox(String text)
- Checkbox์ ํจ๊ป ๋ณด์ฌ์ง text๋ฅผ ์ง์ . state๋ false(์ ํํด์ )๋ก ์์ฑ.
-
Checkbox()
text
์์ด Checkbox๋ง ๋ํ๋จ. state๋ false(์ ํํด์ )๋ก ์์ฑ.
-
Checkbox(String text, CheckboxGroup group, boolean state)
- group - CheckboxGroup ๊ฐ์ฒด์ ์ฐธ์กฐ. CheckboxGroup์ ์ด์ฉํด radio button์ ๋ง๋ ๋ค.
-
String getLabel()
: Checkbox์ label์ ์ป๋๋ค -
void setLabel(String label)
: Checkbox์ label์ ์ฃผ์ด์ง ๊ฐ์ผ๋ก ๋ณ๊ฒฝํ๋ค. -
boolean getState()
: Checkbox์ ์ํ๋ฅผ ์ป๋๋ค. true์ด๋ฉด ์ฒดํฌ๋ ์ํ. -
void setState(boolean state)
: Checkbox์ ์ํ๋ฅผ ์ค์ . state๋ฅผ true๋ก ํ๋ฉด Checkbox๊ฐ ์ฒดํฌ๋ ์ํ๊ฐ ๋๋ค.
Checkbox ์ฌ์ฉํ๊ธฐ
import java.awt.*;
public class CheckboxTest {
public static void main(String[] args) {
Frame f = new Frame("Questions");
f.setSize(305,250);
// Frame์ LayoutManager๋ฅผ FlowLayout์ผ๋ก ์ค์ .
// -> ์ปดํฌ๋ํธ๋ค์ ํฌ๊ธฐ, ์์น๋ฅผ ๋ฐ๋ก ์ ํ์ง ์์๋ ๋จ.
f.setLayout(new FlowLayout());
Label q1 = new Label("1. ๋น์ ์ ๊ด์ฌ ๋ถ์ผ๋? (๋ค์ค์ ํ ๊ฐ๋ฅ)");
Checkbox news = new Checkbox("news", true);
Checkbox sports = new Checkbox("sports");
Checkbox movies = new Checkbox("movies");
Checkbox music = new Checkbox("music");
f.add(q1); f.add(news); f.add(sports); f.add(movies); f.add(music);
Label q2 = new Label("2. ์ผ๋ง๋ ์์ฃผ ๊ทน์ฅ์ ๊ฐ์ญ๋๊น?");
CheckboxGroup group1 = new CheckboxGroup(); // ๊ทธ๋ฃนํ (ํ๋๋ง ์ ํ๊ฐ๋ฅ)
Checkbox movies1 = new Checkbox("ํ ๋ฌ์ ๋ ๋ฒ", group1, true);
Checkbox movies2 = new Checkbox("์ผ์ฃผ์ผ์ ํ ๋ฒ", group1, false);
Checkbox movies3 = new Checkbox("์ผ์ฃผ์ผ์ ๋ ๋ฒ", group1, false);
f.add(q2); f.add(movies1); f.add(movies2); f.add(movies3);
Label q3 = new Label("3. ํ๋ฃจ์ ์ผ๋ง๋ ์ปดํจํฐ๋ฅผ ์ฌ์ฉํ์ญ๋๊น?");
CheckboxGroup group2 = new CheckboxGroup(); // ๊ทธ๋ฃนํ (ํ๋๋ง ์ ํ๊ฐ๋ฅ)
Checkbox com1 = new Checkbox("5์๊ฐ ์ดํ", group2, true);
Checkbox com2 = new Checkbox("10์๊ฐ ์ดํ", group2, false);
Checkbox com3 = new Checkbox("15์๊ฐ ์ด์", group2, false);
f.add(q3); f.add(com1); f.add(com2); f.add(com3);
f.setVisible(true);
}
}
TextField
- TextField : ์ฌ์ฉ์๋ก๋ถํฐ ๊ฐ์ ์
๋ ฅ๋ฐ๋ ์ปดํฌ๋ํธ.
- ํธ์ง์ด ๊ฐ๋ฅํ๋ค.
- ํ ์ค๋ง ์ ๋ ฅํ ์ ์์ด, ๋น๊ต์ ์งง์ ๊ฐ์ ์ ๋ ฅ๋ฐ์ ๋ ์ฌ์ฉ.(์ด๋ฆ, id, ๋น๋ฐ๋ฒํธ)..
TextField์ ๋ฉ์๋
TextField(String text, int col)
: TextField ๊ฐ์ฒด ์์ฑtext
: TextField์ ๋ณด์ฌ์ง text ์ง์ col
: (์ ๋ ฅ๋ฐ์ ๊ธ์์ ์) TextField์ ํฌ๊ธฐ ์ง์ .
void setEchoChar(char c)
: ์ง์ ๋ ๋ฌธ์๋ฅผ EchoChar๋ก ํ๋ค (์ฃผ๋ก ๋น๋ฐ๋ฒํธ ์ ๋ ฅ์ ์ฌ์ฉ)- ์
๋ ฅํ๋ ๊ฐ์ ๊ด๊ณ์์ด ํ๋ฉด์๋ ์ง์ ๋ ๋ฌธ์
c
๊ฐ ๋ณด์ด๊ฒ ๋๋ค.
- ์
๋ ฅํ๋ ๊ฐ์ ๊ด๊ณ์์ด ํ๋ฉด์๋ ์ง์ ๋ ๋ฌธ์
int getColumns()
: TextField์ col ์๋ฅผ ์ป๋๋ค.
TextComponent(TextField์ ์กฐ์)๋ก๋ถํฐ ์์๋ฐ์ ๋ฉ์๋
void setText(String t)
: TextField์ text๋ฅผ t๋ก ํ๋ค.String getText()
: TextField์ text๋ฅผ ์ป๋๋ค.void select(int selectionStart, int selectionEnd)
: selectionStart~selectionEnd๊น์ง์ text๋ฅผ ์ ํ(ํ์ด๋ผ์ดํธ).void selectAll()
: TextField์ ๋ชจ๋ text๋ฅผ ์ ํ๋ ์ํ๋ก ๋ง๋ ๋ค.String getSelectedText()
: TextField์ text์ค ์ ํ๋ ๋ถ๋ถ์ ์ป๋๋ค.void setEditable(boolean b)
: TextField์ text๋ฅผ ํธ์ง๊ฐ๋ฅ(true)/๋ถ๊ฐ๋ฅ(false)ํ๊ฒ ์ค์ .
TextField ์ฌ์ฉํ๊ธฐ
import java.awt.*;
public class TextFieldTest {
public static void main(String[] args) {
Frame f = new Frame("Login");
f.setSize(400,65);
// ๋ ์ด์์๋งค๋์ ๋ฅผ FlowLayout๋ก ํ๋ค.
f.setLayout(new FlowLayout());
Label lid = new Label("ID : ", Label.RIGHT); // ์ค๋ฅธ์ชฝ ์ ๋ ฌ
Label lpwd = new Label("Password : ", Label.RIGHT);
TextField id = new TextField(10); // 10๊ธ์๋ฅผ ์
๋ ฅํ ์ ์ด๋ ํฌ๊ธฐ๋ก ์์ฑ
TextField pwd = new TextField(10); // 10๊ธ์๋ฅผ ์
๋ ฅํ ์ ์ด๋ ํฌ๊ธฐ๋ก ์์ฑ
pwd.setEchoChar('*'); // ์
๋ ฅํ ๊ฐ ๋์ *๊ฐ ๋ณด์ด๊ฒ ํ๋ค.
// ํ ๊ธ์์ด๋ฏ๋ก ํ๋ฐ์ดํ๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค.
f.add(lid);
f.add(id);
f.add(lpwd);
f.add(pwd);
f.setVisible(true);
}
}
TextArea
- TextArea : ์ฌ๋ฌ ์ค์ text๋ฅผ ์
๋ ฅํ๊ฑฐ๋ ๋ณด์ฌ์ค ์ ์๋ ํธ์ง๊ฐ๋ฅํ ์ปดํฌ๋ํธ.
- ์คํฌ๋กค๋ฐ๋ฅผ ์ด์ฉํด ํ๋ฉด์ ๋ณด์ด๋ ๊ฒ ๋ณด๋ค ๋ง์ ์์ text๋ฅผ ๋ด์ ์ ์๋ค.
TextArea์ ๋ฉ์๋
(์ค๋ช : p.22)
TextArea(String text, int row, int col, int scrollbar)
:text
: ๋ณด์ฌ์ง ํ ์คํธrow
: (ํ์) TextArea์ ์ค ์(์ธ๋ก ๊ธธ์ด)col
: (ํ์) TextArea์ ์ด ์(๊ฐ๋ก ๊ธธ์ด)scrollbar
: TextAreadp ์ฌ์ฉํ scrollbar์ ์ข ๋ฅ ๋ฐ ์ฌ์ฉ์ฌ๋ถ ์ง์ TextArea.SCROLLBARS_BOTH
,TextArea.SCROLLBARS_NONE
,TextArea.SCROLLBARS_HORIZONTAL_ONLY
,TextArea.SCROLLBARS_VERTICAL_ONLY
์ค ์ ํ
int getRows()
,int getColumns()
: ํ, ์ด ๊ฐ์ ์ป๊ธฐvoid setRows(int rows)
,void setColumns(int columns)
: ์ง์ ๊ฐ์ผ๋ก ํ, ์ด ๊ฐ์ ์ค์ void append(String str)
: text ๋งจ ๋ง์ง๋ง์ str ๋ง๋ถ์ด๊ธฐvoid insert(String str, int pos
) : text ์ ์ง์ ์์น pos์ str์ ๋ฃ๊ธฐvoid replaceRange(String str, int start, int end)
void setText(String t)
,String getText()
void select(int selectionStart, int selectionEnd)
,void selectAll()
String getSelectedText()
void setEditable(boolean b)
TextArea ์ฌ์ฉํ๊ธฐ
import java.awt.*;
public class TextAreaTest {
public static void main(String[] args) {
Frame f = new Frame("Comments");
f.setSize(400,220);
f.setLayout(new FlowLayout());
TextArea comments = new TextArea("ํ๊ณ ์ถ์ ๋ง์ ์ ์ผ์ธ์.", 10, 50);
f.add(comments);
comments.selectAll(); // TextArea์ text ์ ์ฒด๊ฐ ์ ํ๋๋๋ก ํ๋ค.
f.setVisible(true);
}
}
Scrollbar
- ScrollBar : ์ฌ์ฉ์๊ฐ ์ ํด์ง ๋ฒ์ ๋ด์ ์๋ ๊ฐ์ ์ฝ๊ฒ ์ ํํ ์ ์๋๋ก ํด์ฃผ๋ ์ปดํฌ๋ํธ.
- ์ฃผ๋ก ๋ณผ๋ฅจ ์ค์ , ์๋ ์กฐ์ , ์์ ์ ํ ๋ฑ์ ์ฌ์ฉ.
Scrollbar์ ๋ฉ์๋
(์ค๋ช : p.24)
Scrollbar()
: ์๋ฌด ์ต์ ์์ด ์์ฑํ ์๋ ์๋ค.Scrollbar(int orientation, int value, int visible, int min, int max)
orientation
: Scrollbar์ ์ข ๋ฅ.Scrollbar.VERTICAL
,Scrollbar.HORIZONTAL
์ค ํ1
value
: Scrollbar์ ์ด๊ธฐ๊ฐvisible
: Scroll๋ฒํผ(bubble) ์ ํฌ๊ธฐmin
: Scrollbar์ ์ต์๊ฐmax
: Scrollbar์ ์ต๋๊ฐ
int getValue()
void setValue(int newValue)
Scrollbar ์ฌ์ฉํ๊ธฐ
import java.awt.*;
public class ScrollbarTest {
public static void main(String[] args) {
Frame f = new Frame("Scrollbar");
f.setSize(300,200);
f.setLayout(null);
Scrollbar hor = new Scrollbar(Scrollbar.HORIZONTAL, 0, 50, 0, 100);
hor.setSize(100, 15);
hor.setLocation(60,30);
Scrollbar ver = new Scrollbar(Scrollbar.VERTICAL, 50, 20, 0, 100);
ver.setSize(15, 100);
ver.setLocation(30,30);
f.add(hor);
f.add(ver);
f.setVisible(true);
}
}
Canvas
- Canvas : ๋น ํ๋ฉด ๊ณต๊ฐ์ ์ ๊ณตํ๋ ์ปดํฌ๋ํธ. ๊ทธ๋ฆผ์ ๊ทธ๋ฆฌ๊ฑฐ๋ ๊ธ์๋ฅผ ์ ์ ์ ์๋ค.
- ์ฃผ๋ก ๊ทธ๋ฆผ์ ๊ทธ๋ฆฌ๊ฑฐ๋ ์ด๋ฏธ์ง๋ฅผ ๋ฃ๋ ๊ณต๊ฐ์ผ๋ก ์ฌ์ฉ.
- ์ฌ์ฉ์์ ์ ์ปดํฌ๋ํธ๋ฅผ ๋ง๋ค ๋๋ ์ฌ์ฉ ๊ฐ๋ฅ.
Canvas ์ฌ์ฉํ๊ธฐ
import java.awt.*;
public class CanvasTest {
public static void main(String[] args) {
Frame f = new Frame("CanvasTest");
f.setSize(300, 200);
f.setLayout(null); // Frame์ LayoutManager ์ค์ ํด์
Canvas c = new Canvas();
c.setBackground(Color.pink); // ์บ๋ฒ์ค ๋ฐฐ๊ฒฝ = ๋ถํ์
c.setBounds(50, 50, 150, 100); // ์์น(x์ถ/y์ถ), ๋๋น, ๋์ด
f.add(c);
f.setVisible(true);
}
}
Panel
- Panel : Frame๊ณผ ๊ฐ์ด ๋ค๋ฅธ ์ปดํฌ๋ํธ๋ฅผ ์๊ธฐ ์์ญ ๋ด์ ํฌํจ์ํฌ ์ ์๋ ์ปจํ ์ด๋.
- Panel ์์ ์ด ๋ค๋ฅธ ์ปจํ ์ด๋์ ํฌํจ๋๊ฑฐ๋, Panel ์์ Panel์ด ํฌํจ๋๋ ๊ฒ๋ ๊ฐ๋ฅ.
- Frame๊ณผ์ ์ฐจ์ด์
- titlebar๋ ๋ฒํผ์ด ์ ํ ์๊ณ , ๋น์ด์๋ ํ๋ฉด๊ณต๊ฐ๋ง ์๋ค.
- Panel์ ์ปจํ ์ด๋์ด๋ฏ๋ก ์์ ๋ง์ ๋ ์ด์์์ ์ ์ง ๊ฐ๋ฅ - Panel์ ํตํด ์ปดํฌ๋ํธ ๋ฐฐ์น๋ฅผ ๋ค์ํํ ์ ์๋ค.
Panel ์ฌ์ฉํ๊ธฐ
import java.awt.*;
public class PanelTest {
public static void main(String[] args) {
Frame f = new Frame("Panel");
f.setSize(300,200);
f.setLayout(null); // Frame์ด LayoutManager๋ฅผ ์ฌ์ฉํ์ง ์๊ฒ ํ๋ค.
Panel p = new Panel(); // Panel์ LayoutManager๋ฅผ ์ฌ์ฉ - Panel์์ ๋ฒํผ ์์น,ํฌ๊ธฐ๋ ์๋๋ฐฐ์น.
p.setBackground(Color.green);
p.setSize(100, 100);
p.setLocation(50, 50);
Button ok = new Button("OK");
p.add(ok); // Button์ Panel์ ํฌํจ์ํจ๋ค.
f.add(p); // Panel์ Frame์ ํฌํจ์ํจ๋ค.
f.setVisible(true);
}
}
ScrollPane
- ScrollPane : ์ปจํ ์ด๋์ด๋, ๋ค๋ฅธ ์ปดํฌ๋ํธ๋ฅผ ๋จ ํ๋๋ง ํฌํจํ ์ ์๋ค.
- ์ ํ๋ ๊ณต๊ฐ์์ ํฌ๊ธฐ๊ฐ ํฐ ์ปดํฌ๋ํธ๋ฅผ ํ๋ฉด์ ๋ณด์ฌ์ค ๋ ์ฌ์ฉ.
- ํฌํจ๋ ์ปดํฌ๋ํธ๋ณด๋ค ์๊ธฐ์์ ๋ณด๋ค ํฐ ๊ฒฝ์ฐ ์๋์ผ๋ก ์คํฌ๋กค๋ฐ๊ฐ ๋ํ๋๋ค.
ScrollPane ๋ฉ์๋
ScrollPane(int scrollbarDisplayPolicy)
scrollbarDisplayPolicy
- ์๋ ๊ฐ ์ค ํ๋๋ฅผ ์ง์ (์คํฌ๋กค๋ฐ ํ์ ์ฌ๋ถ)SCROLLBARS_ALWAYS
,SCROLLBARS_AS_NEEDED
,SCROLLBARS,NEVER
ScrollPane()
์ฒ๋ผ ์๋ฌด ์ค์ ์์ด ๊ฐ์ฒด๋ฅผ ์์ฑํ ์๋ ์๋ค.
ScrollPane ์ฌ์ฉํ๊ธฐ
import java.awt.*;
public class ScrollPaneTest {
public static void main(String[] args) {
Frame f = new Frame("ScrollPaneTest");
f.setSize(300, 200);
ScrollPane sp = new ScrollPane();
Panel p = new Panel();
p.setBackground(Color.green);
p.add(new Button("์ฒซ๋ฒ์งธ")); // Button๋ค์ Panel์ ํฌํจ์ํจ๋ค.
p.add(new Button("๋๋ฒ์งธ"));
p.add(new Button("์ธ๋ฒ์งธ"));
p.add(new Button("๋ค๋ฒ์จฐ"));
sp.add(p); // Panel์ ScrollPane์ ํฌํจ์ํจ๋ค
f.add(sp); // ScrollPane์ Frame์ ํฌํจ์ํจ๋ค
f.setVisible(true);
}
}
Dialog
- Dialog : ์ฃผ๋ก ํ๋ฉด์ ๋ฉ์์ง์ฐฝ์ ๋ณด์ฌ์ค ๋ ์ฌ์ฉ.
- ์๋ฌ ๋ฐ์์ ์๋ฆด ๋, ํ์ผ ์ญ์ ์ ์ฌ์ฉ์์๊ฒ ํ์ธ์ ๋ฐ์ ๋ .. ๋ฑ
- Dialog๋ ๋ค๋ฅธ ์ปดํฌ๋ํธ๋ค์ ํฌํจํ ์ ์๋ ์ปจํ ์ด๋ ์ด๋ค.
- Frame๊ณผ ์ ์ฌํ ๋ชจ์์ ํ๊ณ ์๋ค.
Dialog์ ๋ฉ์๋
Dialog(Frame parent, String title, boolean modal)
(์์ฑ์)parent
: (ํ์) ์ด๋ค Frame์ ์ํ ๊ฒ์ธ์ง ์ง์ .- Dialog๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์๋ ํ๋์ Frame์ด Dialog์ parent๋ก ์ง์ ๋์ด์ผ ํจ.
- ์์์ ๋ถ๋ชจ-์์ ๊ด๊ณ๋ ์๋์ง๋ง, Dialog๊ฐ ์ด๋ Frame๊ณผ ๊ด๋ จ์๋์ง ์ง์ ํ๊ธฐ ์ํจ์.
title
: (ํ์) Dialog์ titlebar์ ๋ํ๋ text ์ง์ .modal
: Dialog๋ฅผ modal(ํ์์๋ต)๋ก ํ ๊ฒ์ธ์ง ์ง์ . ์ง์ ํ์ง ์์ผ๋ฉด false๊ฐ ๋๋ค.- โป
modal
์ ์ฌ์ฉ์ ์ ๋ ฅ์ ๋ ์ ํ๋ค๋ ์๋ฏธ. ๋ฐ๋๋ง์ modeless.
- โป
- true์ธ ๊ฒฝ์ฐ, ๋ถ๋ชจ Frame์ Dialog๊ฐ ์ฌ๋ผ์ง๊ธฐ ์ ๊น์ง๋ ์ฌ์ฉํ ์ ์๊ฒ ๋๋ค.
- ์ฃผ๋ก ์ฌ์ฉ์์๊ฒ ์ค์ ํ์ธ์ ๋ฐ์์ผ ํ ๊ฒฝ์ฐ ์ฌ์ฉ.
- false์ธ ๊ฒฝ์ฐ, Dialog๊ฐ ์ด๋ ค์์ด๋ ๋ถ๋ชจ Frame์ ์ฌ์ฉํ ์ ์๋ค.
void show()
,void hide()
->void setVisible(boolean b)
: Dialog ๋ณด์ด๊ธฐ/์จ๊ธฐ๊ธฐvoid dispose()
: Dialog ์ญ์ (ํ๋ฉด์ ์ ๋ณด์ด๊ฒ ํ ํ, ๋ฉ๋ชจ๋ฆฌ์์ ์ ๊ฑฐ)String getTitle()
void setModal(boolean b)
void setResizable(boolean resizable)
: Dialogํฌ๊ธฐ๋ฅผ ๋ณ๊ฒฝ๊ฐ๋ฅ/๋ถ๊ฐ๋ฅํ๊ฒ ํจ- ๊ธฐ๋ณธ์ ์ผ๋ก Dialog๋ ํฌ๊ธฐ๋ณ๊ฒฝ์ด ๋ถ๊ฐ๋ฅ.
Dialog ์ฌ์ฉํ๊ธฐ
import java.awt.*;
public class DialogTest {
public static void main(String[] args) {
Frame f = new Frame("Parent");
f.setSize(300, 200);
// parent Frame์ f, modal์ true(ํ์์๋ต Dialog)
Dialog info = new Dialog(f, "Information", true);
info.setSize(140, 90);
info.setLocation(50,50); // parent Frame์ด ์๋, ํ๋ฉด์ด ์์น๊ธฐ์ค์ด ๋จ.
info.setLayout(new FlowLayout());
// Dialog์ ๊ธฐ๋ณธ ๋ ์์ด์ ๋งค๋์ ๋ BorderLayout์ด๊ธฐ ๋๋ฌธ์ FlowLayout๋ก ๋ณ๊ฒฝ.
Label msg = new Label("This is modal Dialog", Label.CENTER);
Button ok = new Button("ok");
// ์์ง ์ ๋ฐฐ์ด ๋ด์ฉ(์ด๋ฒคํธ ์ฒ๋ฆฌ) - ok๋ฒํผ ๋๋ฅด๋ฉด Dialog ์ฐฝ ์์ ๊ธฐ.
ok.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//ok๋ฒํผ์ ๋๋ฅด๋ฉด ์ํ๋ ๋ฉ์๋ : Dialog ์์ ๊ธฐ.
info.setVisible(false); // Dialog๋ฅผ ์ ๋ณด์ด๊ฒ ํ๋ค.(์จ๊น)
info.dispose(); // Dialog๋ฅผ ๋ฉ๋ชจ๋ฆฌ์์ ์์ค๋ค.(์ญ์ )
// ๋ ์ค ์ด๋ ๋ฐฉ๋ฒ์ ์ฌ์ฉํด๋ Dialog๋ ๋ณด์ด์ง ์๊ฒ ๋๋ฉฐ, ๋ถ๋ชจFrame์ ์ฌ์ฉ๊ฐ๋ฅํด์ง๋ค.
}
});
info.add(msg);
info.add(ok);
f.setVisible(true); // Frame์ ๋ณด์ด๊ฒ ํ ๋ค์,
info.setVisible(true); // Dialog๋ฅผ ๋ณด์ด๊ฒ ํ๋ค.
}
}
FileDialog
- FileDialog : ํ์ผ์ ์ด๊ฑฐ๋ ์ ์ฅํ ๋ ์ฌ์ฉ๋๋ Dialog.
- ๋๋ถ๋ถ์ ๊ธฐ๋ฅ์ด ์ด๋ฏธ ๊ตฌํ๋์ด ์์ด, ๋ฐ๋ก ์ฝ๋๋ฅผ ์ถ๊ฐํ์ง ์์๋ ๋๋ค.
FileDialog ๋ฉ์๋
FileDialog(Frame parent, String title, int mode)
(์์ฑ์)parent
: (ํ์) ์ด๋ค Frame์ ์ํ ๊ฒ์ธ์ง ์ง์ title
: (ํ์) FileDialog์ titlebar์ ๋ํ๋ text ์ง์ mode
:FileDialog.LOAD
(์ด๊ธฐ),FileDialog.SAVE
(์ ์ฅ) ์ค ํ๋ ์ ํ
String getFile()
: FileDialog์ ์ํด ์ ํ๋ ํ์ผ์ ์ด๋ฆ์ ์ป๋๋ค.String getDirectory()
: FileDialog์ ์ํด ์ ํ๋ ํ์ผ์ ๊ฒฝ๋ก(path)๋ฅผ ์ป๋๋ค.void setFile(String file)
,void setDirectory(String dir)
- FileDialog์ ์ง์ ๋ ํ์ผ/๋๋ ํ ๋ฆฌ ์ค์ .
FileDialog ์ฌ์ฉํ๊ธฐ
import java.awt.*;
public class FileDialogTest {
public static void main(String[] args) {
Frame f = new Frame("Parent");
f.setSize(300, 200);
FileDialog fileOpen = new FileDialog(f, "ํ์ผ์ด๊ธฐ", FileDialog.LOAD);
f.setVisible(true); // Frame์ ํ์ํ๊ณ
fileOpen.setDirectory("c:\\"); // c ๋๋ ํ ๋ฆฌ ๋ด์ ํ์ผ๋ค์ด ๋ณด์ด๊ฒ ์ด๊ธฐ
fileOpen.setVisible(true); // FileDialog๋ฅผ ํ์.
// FileDialog์์ ํ์ผ์ ์ ํํ ๋ค์ '์ด๊ธฐ'๋ฒํผ์ ๋๋ฅด๋ฉด,
// getFile()๊ณผ getDirectory()๋ฅผ ์ด์ฉํด ์ ํ๋ ํ์ผ์ ์ด๋ฆ๊ณผ ๋๋ ํ ๋ฆฌ ์์น๋ฅผ ์ป์ ์ ์๋ค.
// ์๋ฌด๊ฒ๋ ์ ํํ์ง ์์ ์, null์ ๋ฐํ.
System.out.println(fileOpen.getDirectory() + fileOpen.getFile());
}
}
Font
- Font : Font์ ์ธ์คํด์ค๋ฅผ ์์ฑํ ๋ค ์ปดํฌ๋ํธ์ ๊ธ์จ์ฒด๋ฅผ ์ง์ ํ ์ ์๋ค.
setFont(Font f)
: ์ปดํฌ๋ํธ์ ์ฌ์ฉ๋๋ text์ ๊ธ์จ์ฒด๋ฅผ ์ง์ ํ ์ ์๋ค.
Font ์์ฑ์
Font(String name, int style, int size)
name
: ์ฌ์ฉํ ํฐํธ์ ์ด๋ฆ.- ๊ธฐ๋ณธ ์ ๊ณต ํฐํธ :
Serif
,SansSerif
,Dialog
,DialogInput
,Monospaced
- ๋ค๋ฅธ ํฐํธ ์ฌ์ฉ์ ์ํด์๋ ๊ทธ ํฐํธ๊ฐ ์ปดํจํฐ์ ์ค์น๋์ด ์์ด์ผ ํ๋ค.
- ํ์ฌ ์ค์น๋ Font๋ฆฌ์คํธ ์ป๊ธฐ :
GraphicsEnvironment
ํด๋์ค์ ๋ฉ์๋๋ค์ ์ด์ฉํ๋ค.
- ๊ธฐ๋ณธ ์ ๊ณต ํฐํธ :
style
: ํฐํธ์ ์คํ์ผ ์ง์ .Font.PLAIN
,Font.BOLD
,Font.ITALIC
,Font.BOLD+Font.ITALIC
์ค ์ ํ.
size
: ํฐํธ์ ํฌ๊ธฐ ์ง์ .
Font ์กฐํํ๊ธฐ
import java.awt.*;
public class FontList {
public static void main(String[] args) {
GraphicsEnvironment ge = null; // GraphicsEnvironment๊ฐ์ฒด์์ฑ
ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
Font[] fonts = ge.getAllFonts(); // ์์คํ
๋ด์ ํฐํธ ๋ชฉ๋ก ๊ฐ์ ธ์ค๊ธฐ
for(int i=0; i < fonts.length; i++) {
System.out.println(fonts[i].getFontName()); // ํฐํธ ์ด๋ฆ ํ๋์ฉ ์ถ๋ ฅ
}
}
}
Font ์ฌ์ฉํ๊ธฐ
import java.awt.*;
public class FontTest {
public static void main(String[] args) {
Frame f = new Frame("Font Test");
String abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Label abc1 = new Label(abc);
Label abc2 = new Label(abc);
Label abc3 = new Label(abc);
Label abc4 = new Label(abc);
Label abc5 = new Label(abc);
// Font๋ฅผ ์ค์
Font f1 = new Font("Serif", Font.PLAIN, 20); // ๋ณดํต์ฒด
Font f2 = new Font("Serif", Font.ITALIC, 20); // ๊ธฐ์ธ์์ฒด
Font f3 = new Font("Serif", Font.BOLD, 20); // ๊ตต์์ฒด
Font f4 = new Font("Serif", Font.BOLD+Font.ITALIC, 20); // ๊ตต์ ๊ธฐ์ธ์์ฒด
// ์ค์ ํ Font๋ฅผ Label์ ์ ์ฉ
abc1.setFont(f1);
abc2.setFont(f2);
abc3.setFont(f3);
abc4.setFont(f4);
f.setLayout(new FlowLayout());
f.add(abc1);
f.add(abc2);
f.add(abc3);
f.add(abc4);
f.add(abc5); // ํฐํธ๋ฅผ ์ง์ ํด ์ฃผ์ง ์์ผ๋ฉด, ์์ ์ด ํฌํจ๋ ์ปจํ
์ด๋์ ๊ธฐ๋ณธ์ค์ ์ ๋ฐ๋ฅธ๋ค.
f.setSize(400, 200);
f.setVisible(true);
}
}
Color
- Color : ์์ ํํํ๋ ํด๋์ค. ์ํ๋ RGB๊ฐ์ ๊ฐ์ง๊ณ ๊ทธ ์๊น์ ๊ฐ์ฒด๋ฅผ ์์ฑ, ์ฌ์ฉํ ์ ์๋ค.
- ์์ฃผ ์ฐ์ด๋ ์์ static์ผ๋ก ์ ์๋์ด ์์ด RGB๊ฐ์ ๋ชฐ๋ผ๋ ์ฌ์ฉ ๊ฐ๋ฅ.
- ์ฃผ๋ก ์ปดํฌ๋ํธ์ ๋ฐฐ๊ฒฝ์/์ ๊ฒฝ์ ์ค์ ํ๋ ๋ฉ์๋ -
setForeground(Color c)
,setBackground(Color c)
์ ๋งค๊ฐ๋ณ์๋ก ์ฌ์ฉ๋จ.
Color์ ์์ฑ์ / ๋ฉ์๋
Color(int r, int g, int b, int a)
๋๋Color(float r, float g, float b, float a)
r
- red,g
- green,b
- blue.r
,g
,b
๋ ๋ชจ๋ 0255์ ์ ์๊ฐ ๋๋ 0.01.0์ ์ค์๊ฐ์ ๊ฐ๋๋ค.
a
- (์ ํ) ์์ ๋ถํฌ๋ช ๋๋ฅผ ์ง์ ํ๋ alpha๊ฐ์ผ๋ก. ๊ฐ์ด ํด์๋ก ๋ถํฌ๋ช ํ๋ค.- 0
255์ ์ ์๊ฐ ๋๋ 0.01.0์ ์ค์๊ฐ์ ๊ฐ๋๋ค.
- 0
Color ์ฌ์ฉํ๊ธฐ
import java.awt.*;
public class ColorTest {
public static void main(String[] args) {
Frame f = new Frame("Color Test");
f.setLayout(new GridLayout(14, 2)); // ๊ฐ๋ก 14์ค, ์ธ๋ก 2์ค์ ํ? ์์ฑ
Panel p1 = new Panel(); p1.setBackground(Color.black);
Panel p2 = new Panel(); p2.setBackground(Color.blue);
Panel p3 = new Panel(); p3.setBackground(Color.cyan);
Panel p4 = new Panel(); p4.setBackground(Color.darkGray);
Panel p5 = new Panel(); p5.setBackground(Color.gray);
Panel p6 = new Panel(); p6.setBackground(Color.green);
Panel p7 = new Panel(); p7.setBackground(Color.lightGray);
Panel p8 = new Panel(); p8.setBackground(Color.magenta);
Panel p9 = new Panel(); p9.setBackground(Color.orange);
Panel p10 = new Panel(); p10.setBackground(Color.pink);
Panel p11 = new Panel(); p11.setBackground(Color.red);
Panel p12 = new Panel(); p12.setBackground(Color.white);
Panel p13 = new Panel(); p13.setBackground(Color.yellow);
Panel p14 = new Panel(); p14.setBackground(new Color(50,100,100));
// ์ Color๊ฐ์ฒด ์ ์ธํด RGB๊ฐ์ ์ค์ ํ ์๋ ์๋ค.
f.add(new Label("Color.black")); f.add(p1); // ์ ์ด๋ฆ๊ณผ ์ Panel ์ถ๊ฐ.
f.add(new Label("Color.blue")); f.add(p2);
f.add(new Label("Color.cyan")); f.add(p3);
f.add(new Label("Color.darkGray")); f.add(p4);
f.add(new Label("Color.gray")); f.add(p5);
f.add(new Label("Color.green")); f.add(p6);
f.add(new Label("Color.lightGray")); f.add(p7);
f.add(new Label("Color.magenta")); f.add(p8);
f.add(new Label("Color.orange")); f.add(p9);
f.add(new Label("Color.pink")); f.add(p10);
f.add(new Label("Color.red")); f.add(p11);
f.add(new Label("Color.white")); f.add(p12);
f.add(new Label("Color.yellow")); f.add(p13);
f.add(new Label("Color(50, 100, 100)")); f.add(p14);
f.setSize(250, 300);
f.setVisible(true);
}
}
๋ฉ๋ด ๋ง๋ค๊ธฐ
๋ฉ๋ด๋ฅผ ๊ตฌ์ฑํ๋ ์ปดํฌ๋ํธ
-
Frame์ ๋ฉ๋ด๋ฅผ ๊ตฌ์ฑํ๊ธฐ ์ํด์๋ MenuComponent ํด๋์ค์
MenuBar
,Menu
,MenuItem
๊ณผ ๊ฐ์ ์ปดํฌ๋ํธ๋ค์ ์ฌ์ฉํ๋ค. -
๋ชจ๋ AWT์ปดํฌ๋ํธ์ ์กฐ์์ด Componentํด๋์ค์ธ ๊ฒ์ฒ๋ผ, ๋ฉ๋ด๋ฅผ ๊ตฌ์ฑํ๋ ์ปดํฌ๋ํธ๋ค์ ์กฐ์์ MenuComponent์ด๋ค.
- MenuComponentํด๋์ค๋
MenuBar
์MenuItem
์ ์์์ผ๋ก ๊ฐ๊ณ , MenuItem
์Menu
/PopupMenu
,CheckboxMenuItem
์ ์์์ผ๋ก ๊ฐ๋๋ค.
- MenuComponentํด๋์ค๋
-
๊ณ์ธตํ ๋ฉ๋ด : ํด๋์ฒ๋ผ Menu์ Menu๋ฅผ ๋ด์ ๊ณ์ธตํ์ ๋ฉ๋ด๋ฅผ ๊ตฌ์ฑํ ์ ์๋ค.
-
์ต์์ ๋ฉ๋ด : ํญ์ ํ๋ฉด์ ๋ํ๋๋ ์ต์์๋ฉ๋ด๋ฅผ MenuBar์ ๋ด๊ณ , MenuBar๋ ๋ค์ Frame์ ์ถ๊ฐํจ์ผ๋ก์จ ๋ฉ๋ด ์์ฑ์ด ๋๋๋ค.
-
Menu
: Menu์ MenuItem์ด ํฌํจ๋ ์ ์๋ค.- ๊ณ์ธตํ ๋ฉ๋ด : Menu์ Menu๋ฅผ ํฌํจ์ํจ๋ค.
-
MenuBar
: Menu๋ง ํฌํจ๋ ์ ์๋ค. MenuItem์ ํฌํจ์ํฌ ์ ์๋ค. -
MenuItem
: Menu์ ํฌํจ๋๋ค. -
PopupMenu
: ๋ง์ฐ์ค ์ค๋ฅธ์ชฝ ๋ฒํผ์ ๋๋ฅด๋ฉด ๋ํ๋๋, ์์น๊ฐ ๊ณ ์ ๋์ด ์์ง ์์ ๋ฉ๋ด.- Frame๋ด์ ์ด๋์๋ ์คํ ๊ฐ๋ฅ. (= ์ค๋ฅธ์ชฝ ํด๋ฆญ)
๋ฉ๋ด ์ปดํฌ๋ํธ์ ๋ฉ์๋, ์์ฑ์
setHelpMenu(Menu menu)
- OS๋ง๋ค Help๋ฉ๋ด๊ฐ ๋ค๋ฅด๊ฒ ์ทจ๊ธ๋๋ค. - ์๋์ฐ์์๋ ํน๋ณํ ๋ค๋ฅด๊ฒ ๋ค๋ค์ง์ง๋ ์์.
addSeparator()
CheckboxMenuItem(String name, boolean status)
CheckboxMenuItem(String name)
๋ฉ๋ด ์ปดํฌ๋ํธ๋ค๊ณผ PopupMenu ์ฌ์ฉํ๊ธฐ
๋ฉ๋ด ์ปดํฌ๋ํธ
import java.awt.*;
public class MenuTest {
public static void main(String[] args) {
Frame f = new Frame("Frame with Menu");
f.setSize(300, 200);
MenuBar mb = new MenuBar(); // ๋ฉ๋ด๋ฐ ์ ์ธ
Menu mFile = new Menu("File"); // ๋ฉ๋ด ์ ์ธ
MenuItem miNew = new MenuItem("New");
MenuItem miOpen = new MenuItem("Open");
Menu mOthers = new Menu("Others"); // MenuItme์ด ์๋ Menu.
MenuItem miExit = new MenuItem("Exit");
mFile.add(miNew);//Menu์ MenuItem๋ค์ ์ถ๊ฐ.
mFile.add(miOpen);
mFile.add(mOthers); // Menu์ Menu ์ถ๊ฐ.
mFile.addSeparator(); // ๋ฉ๋ด ๋ถ๋ฆฌ์ ์ถ๊ฐ.
mFile.add(miExit);
MenuItem miPrint = new MenuItem("Print ...");
MenuItem miPreview = new MenuItem("Print Preview");
MenuItem miSetup = new MenuItem("Print Setup ...");
mOthers.add(miPrint);
mOthers.add(miPreview);
mOthers.add(miSetup); // Others์ ํ์ ๋ฉ๋ด๋ค์ ๋ง๋ค์ด ๋ฃ๋๋ค.
Menu mEdit = new Menu("Edit");
Menu mView = new Menu("View");
Menu mHelp = new Menu("Help");
CheckboxMenuItem miStatusBar = new CheckboxMenuItem("Statusbar");
mView.add(miStatusBar); // View์ ์ํ๋ฐ ์ถ๊ฐ
mb.add(mFile); // MenuBar์ Menu๋ค์ ์ถ๊ฐํ๋ค.
mb.add(mEdit);
mb.add(mView);
mb.setHelpMenu(mHelp); // mHelp๋ฅผ HelpMenu๋ก ์ง์ .
f.setMenuBar(mb); // Frame์ MenuBar๋ฅผ ํฌํจ์ํจ๋ค.
f.setVisible(true);
}
}
PopupMenu
import java.awt.*;
import java.awt.event.*; // ์ด๋ฒคํธ ์ฒ๋ฆฌ๋ฅผ ์ํด ์ถ๊ฐ.
public class PopupMenuTest {
public static void main(String[] args) {
final Frame f = new Frame("PopupMenu Test");
f.setSize(300, 200);
final PopupMenu pMenu = new PopupMenu("Edit");
// final ? ๋ฉ์๋ ๋ด์ ์ ์๋ ํด๋์ค์์๋,
// ๊ฐ์ ๋ฉ์๋ ๋ด์ ์ง์ญ๋ณ์๋ค ์ค finalํค์๋๊ฐ ๋ถ์ธ ๊ฒ๋ค๋ง ์ฐธ์กฐํ ์ ์๋ค.
// ์ต๋ช
ํด๋์ค๋ฅผ main๋ฉ์๋ ๋ด์ ์ ์ํ๊ธฐ ๋๋ฌธ์ ์ฌ์ฉ.
MenuItem miCut = new MenuItem("Cut");
MenuItem miCopy = new MenuItem("Copy");
MenuItem miPaste = new MenuItem("Paste");
pMenu.add(miCut); // PopupMenu์ MenuItem๋ค์ ์ถ๊ฐํ๋ค.
pMenu.add(miCopy);
pMenu.add(miPaste);
f.add(pMenu); // PopupMenu๋ฅผ Frame์ ์ถ๊ฐํ๋ค.
f.addMouseListener(new MouseAdapter() { // ์ต๋ช
ํด๋์ค
public void mousePressed(MouseEvent me) {
// ์ค๋ฅธ์ชฝ ๋ง์ฐ์ค๋ฒํผ์ ๋๋ฅด๋ฉด PopupMenu๋ฅผ ํ๋ฉด์ ๋ณด์ฌ์ค๋ค
if (me.getModifiers() == me.BUTTON3_MASK)
pMenu.show(f, me.getX(), me.getY()); // Frame๋ด์์ ๋ง์ฐ์คํฌ์ธํฐ์ x,y์ขํ ์ป๊ธฐ
}
});
f.setVisible(true);
}
}
๋ ์ด์์ ๋งค๋์ Layout Manager
- ๋ ์ด์์ ๋งค๋์ : ์ปจํ
์ด๋ ๋ด์ ์ปดํฌ๋ํธ ๋ฐฐ์น๋ฅผ ์๋์ผ๋ก ๊ด๋ฆฌํ๋ค.
- ์๋ก์ด ์ปดํฌ๋ํธ ์ถ๊ฐ / ์ปจํ ์ด๋ ํฌ๊ธฐ ๋ณ๊ฒฝ .. ๋ฑ ์ปดํฌ๋ํธ์ ๋ฐฐ์น, ์ฌ๋ฐฐ์น๋ฅผ ์๋ ์ฒ๋ฆฌ.
๋ ์ด์์ ๋งค๋์ ์ ์ข ๋ฅ (5๊ฐ์ง)
-
์ปจํ ์ด๋ ๋น ํ๋์ ๋ ์ด์์๋งค๋์ ๋ฅผ ์ค์ ํ ์ ์๋ค.
BorderLayout
FlowLayout
GridLayout
CardLayout
GridbagLayout
-
ํ Frame์ ์ฌ๋ฌ ๋ ์ด์์๋งค๋์ ๋ฅผ ์ฌ์ฉํด ์ปดํฌ๋ํธ๋ค์ ๋ฐฐ์นํ ์๋ ์๋ค.
- Frame์ ์ฌ๋ฌ ์ปจํ ์ด๋๋ฅผ ๋ฃ๊ณ , ๊ฐ ์ปจํ ์ด๋๋ณ๋ก ๋ ์ด์์์ ์ค์ ํ๋ฉด ๋๋ค.
- (p.48 ๊ณ์ฐ๊ธฐ ์์ ์ฐธ๊ณ )
์ปจํ ์ด๋๋ณ ๊ธฐ๋ณธ ๋ ์ด์์ ๋งค๋์
- ๋ชจ๋ ์ปจํ
์ด๋๋ ๊ธฐ๋ณธ์ ์ผ๋ก ๋ ์ด์์ ๋งค๋์ ๊ฐ ์ง์ ๋์ด ์๋ค.
FlowLayout
- Panel, AppletBorderLayout
- Window, Dialog, Frame
BorderLayout
-
BorderLayout :
- ์ปจํ ์ด๋๋ฅผ North, South, East, West, Center(๋,์,๋จ,๋ถ,์ค์) ์ 5์์ญ์ผ๋ก ๋๋๋ค.
- ๊ฐ ์์ญ์ ํ๋์ ์ปดํฌ๋ํธ๋ง์ ๋ฐฐ์นํ๋ค.
- ํ ์์ญ์ ์ฌ๋ฌ ์ปดํฌ๋ํธ ๋ฐฐ์นํ๋ฉด, ๋ง์ง๋ง์ผ๋ก ์ถ๊ฐํ ์ปดํฌ๋ํธ๋ง ๋ณด์ธ๋ค
- ํ ์์ญ์ ์ฌ๋ฌ ์ปดํฌ๋ํธ ๋ฃ์ผ๋ ค๋ฉด Panel์ ๋ฃ๊ณ ๊ทธ Panel์ ์์ญ์ ๋ฃ์ด์ผ ํ๋ค.
- Window, Frame, Dialog์ ๊ธฐ๋ณธ layout์ด๋ค.
-
Frame์ ํฌ๊ธฐ๋ฅผ ์ํ๋ก ๋๋ ธ์ ๋
- West, Center, East ์์ญ์ ๊ธธ์ด๋ง ์ํ๋ก ๋์ด๋๊ณ , North South ์์ญ์๋ ์๋ฌด๋ฐ ๋ณํ๊ฐ ์๋ค.
-
Frame์ ํฌ๊ธฐ๋ฅผ ์ข์ฐ๋ก ๋๋ ธ์ ๋
- North, Center, South์์ญ์ ๊ธธ์ด๋ง ์ข์ฐ๋ก ๋์ด๋๊ณ , West East ์์ญ์๋ ์๋ฌด๋ฐ ๋ณํ๊ฐ ์๋ค.
BorderLayout์ ๋ฉ์๋
BorderLayout()
๋๋BorderLayout(int hgap, int vgap)
(์์ฑ์)hgap
: (์ ํ) ์ข์ฐ ๊ฐ๊ฒฉvgap
: (์ ํ) ์ํ ๊ฐ๊ฒฉ
add(String name, Component c)
๋๋add(Component c, String name)
- ๊ฐ ์์ญ์ ์ปดํฌ๋ํธ ์ถ๊ฐํ๋ ๋ฉ์๋.
- 5์์ญ์ ๋ชจ๋ ์ฑ์์ผ ํ๋ ๊ฒ์ ์๋๊ณ , ํ์ํ ์์ญ๋ง ์ถ๊ฐํ๋ค.
BorderLayout ์ฌ์ฉํ๊ธฐ
import java.awt.*;
public class BorderLayoutTest {
public static void main(String[] args) {
Frame f = new Frame("BorderLayoutTest");
f.setSize(200, 200);
// Frame ์ ๊ธฐ๋ณธ์ ์ผ๋ก BorderLayout๋ก ์ค์ ๋์ด ์์ผ๋ฏ๋ก, ๋ฐ๋ก ์ค์ ํ์ง ์์๋ ๋จ
f.setLayout(new BorderLayout());
Button north = new Button("North");
Button south = new Button("South");
Button east = new Button("East");
Button west = new Button("West");
Button center = new Button("Center");
// Frame์ 5๊ฐ์ ๊ฐ ์์ญ์ Button์ ํ๋์ฉ ์ถ๊ฐํ๋ค
f.add(north, "North"); // f.add("North",north); . ์ ๊ฐ์ด ์ธ ์๋ ์๋ค
f.add(south, "South"); // South์ ๋์๋ฌธ์ ์ ํํ
f.add(east, "East"); // East๋์ , BorderLayout.EAST ์ฌ์ฉ๊ฐ๋ฅ
f.add(west, "West");
f.add(center, "Center");
f.setVisible(true);
}
}
FlowLayout
- FlowLayout
- ์ปดํฌ๋ํธ๋ค์ด ์ถ๊ฐ๋๋ ์์์ ๋ฐ๋ผ ์ผ์ชฝ-์ค๋ฅธ์ชฝ์ผ๋ก ๋ฐฐ์น๋๋ค.
- ๊ณต๊ฐ์ด ๋ถ์กฑํ๋ฉด ์๋ซ์ค์ ์ถ๊ฐ๋๋ค. (์ค๋ฐ๊ฟ)
- ๋ง์น ์๋ํ๋ก์ธ์ค์์ ๊ธ์๋ฅผ ์ ๋ ฅํ๋ ๊ฒ๊ณผ ๋น์ทํ ๋ฐฉ์.
- ์ปดํฌ๋ํธ ์ ๋ ฌ์ด ๊ฐ๋ฅ : ์ผ์ชฝ/์ค๋ฅธ์ชฝ/๊ฐ์ด๋ฐ ์ ๋ ฌ์ด ๊ฐ๋ฅํ๋ค.
- ํ๋ฉด์ ๋๋ฆฌ๊ฑฐ๋ ์ค์ด๋ฉด..
- ๋ฐฐ์น๋ ๋ฐ๋๋, ๊ฐ์ ๋ฐฐ์น๋ฐฉ์์ ๋ฐ๋ฅธ๋ค.
- ์ปดํฌ๋ํธ ํฌ๊ธฐ๋ ๊ทธ๋๋ก ์ ์ง๋๋ค.
FlowLayout์ ๋ฉ์๋, ์์ฑ์
FlowLayout()
๋๋FlowLayout(int align, int hgap, int vgap)
(์์ฑ์)- ์๋ฌด๊ฒ๋ ์ง์ ํ์ง ์์ผ๋ฉด ๊ฐ์ด๋ฐ์ ๋ ฌ, ์ํ์ข์ฐ์ฌ๋ฐฑ 5px์ผ๋ก ์์ฑ๋๋ค.
align
: ์ปดํฌ๋ํธ์ ์ ๋ ฌ ์ง์ .FlowLayout.LEFT
,FlowLayout.CENTER
,FlowLayout.RIGHT
์ค ํ1
hgap
: ์ปดํฌ๋ํธ ๊ฐ ์ข์ฐ ๊ฐ๊ฒฉ ์ง์ .vgap
: ์ปดํฌ๋ํธ ๊ฐ ์ํ ๊ฐ๊ฒฉ ์ง์ .
FlowLayout ์ฌ์ฉํ๊ธฐ
import java.awt.*;
public class FlowLayoutTest {
public static void main(String[] args) {
Frame f = new Frame("FlowLayoutTest");
f.setSize(250, 100);
f.setLayout(new FlowLayout(FlowLayout.LEFT)); // ์ผ์ชฝ์ ๋ ฌ์ FlowLayout ์์ฑ
f.add(new Button("์ฒซ๋ฒ์งธ"));
f.add(new Button("๋๋ฒ์งธ"));
f.add(new Button("์ธ๋ฒ์งธ"));
f.add(new Button("๋ค๋ฒ์งธ"));
f.add(new Button("๋ค์ฏ๋ฒ์งธ"));
f.setVisible(true);
}
}
GridLayout
-
GridLayout
- ์ปจํ ์ด๋๋ฅผ ํ ์ด๋ธ(ํ)์ฒ๋ผ ํ(row),์ด(col)๋ก ๋๋์ด ์ปดํฌ๋ํธ๋ฅผ ๋ฐฐ์นํ๋ค.
- ์ปดํฌ๋ํธ๋ค์ ๋ชจ๋ ๊ฐ์ ํฌ๊ธฐ๋ก ๋๋์ด์ง๋ค.
- ์ปดํฌ๋ํธ๋ค์ ์ปจํ ์ด๋์ ์ฒซ ๋ฒ์งธ ํ์ ์ด๋ค - ๋ ๋ฒ์งธ ํ์ ์ด๋ค - .. ์ ์์๋ก ์ฑ์์ง๋ค.
- ์ปจํ
์ด๋์ ํฌ๊ธฐ๋ฅผ ๋ณ๊ฒฝํ๋ฉด..
- ๊ฐ ์์ญ์ด ๋ชจ๋ ๊ฐ์ ๋น์จ๋ก ์ปค์ง๊ฑฐ๋ ์์์ง๋ค.
-
GridbagLayout
- GridbagLayout์ GridLayout๊ณผ ๊ฐ์ด ์ปจํ ์ด๋๋ฅผ ์ด๊ณผ ํ์ผ๋ก ๋๋์ด ์ปดํฌ๋ํธ๋ค์ ๋ฐฐ์นํ ์ ์๋ค.
- ๊ฐ ์์ญ์ ์๋ก ๋ค๋ฅธ ํฌ๊ธฐ๋ก ์ง์ ๋ ์ ์์ผ๋ฉฐ ์ธ์ ํ ์ด ๋๋ ํ์ผ๋ก์ ํ์ฅ์ด ๊ฐ๋ฅํ๋ค.
- (HTML๋ฌธ์์์์ Table๋ ์ด์์์ ์ค์ ํ๋ ๊ฒ๊ณผ ๋น์ทํ๋ค.)
GridLayout์ ์์ฑ์, ๋ฉ์๋
GridLayout(int row, int col)
๋๋GridLayout(int row, int col, int hgap, int vgap)
(์์ฑ์)row
,col
: (ํ์) ์ปจํ ์ด๋๋ฅผ ๋ช ๊ฐ์ ํ/์ด๋ก ๋๋์ง ์ง์ .hgap
,vgap
: ๊ฐ ์์ญ ๊ฐ ์ข์ฐ/์ํ ๊ฐ๊ฒฉ์ ์ง์ .
GridLayout ์ฌ์ฉํ๊ธฐ
import java.awt.*;
public class GridLayoutTest {
public static void main(String[] args) {
Frame f = new Frame("GridLayoutTest");
f.setSize(150, 150);
f.setLayout(new GridLayout(3, 2));
// 3ํ 2์ด์ ํ
์ด๋ธ์ ๋ง๋ ๋ค
f.add(new Button("1")); // ์ถ๊ฐ๋๋ ์์๋๋ก Button์ ๋ฒํธ๋ฅผ ๋ถ์๋ค
f.add(new Button("2"));
f.add(new Button("3"));
f.add(new Button("4"));
f.add(new Button("5"));
f.add(new Button("6"));
f.setVisible(true);
}
}
CardLayout
- CardLayout
- ์ฌ๋ฌ ํ๋ฉด์ ์ฌ๋ผ์ด๋์ฒ๋ผ ๋ฐ๊ฟ๊ฐ๋ฉฐ ๋ณด์ฌ์ค ์ ์๋ ๋ ์ด์์์ด๋ค.
- ์ฌ๋ฌ ๊ฐ์ ์ปจํ ์ด๋๋ฅผ CardLayout์ ์ถ๊ฐํ ๋ค์,
- ์์๋๋ก/์์๋ก ์ปจ๋ค์ด๋๋ฅผ ์ ํํด ๋ณด์ฌ์ค ์ ์๋ค.
- (์์) ์ค์น๋ง๋ฒ์ฌ ์ฒ๋ผ ๋จ๊ณ๋ณ๋ก ํ๋ฉด์ ์ด๋ํ๋ ๊ฒฝ์ฐ.
CardLayout์ ์์ฑ์, ๋ฉ์๋
CardLayout()
๋๋CardLayout(int hgap, int vgap)
(์์ฑ์)hgap
,vgap
: ์ปจํ ์ด๋-CardLayout๊ฐ์ ์ํ/์์ง ๊ฐ๊ฒฉ ์ง์ .
add(Container parent, String name)
,show(Container parent, String name)
name
: ์ปจํ ์ด๋์ ์ด๋ฆparent
: ์ถ๊ฐ๋ /๋ณด์ฌ์ง ์ปจํ ์ด๋
first(Container parent)
,last(Container parent)
previous(Container parent)
,next(Container parent)
CardLayout ์ฌ์ฉํ๊ธฐ
import java.awt.*;
import java.awt.event.*;
public class CardLayoutTest {
public static void main(String[] args) {
final Frame f = new Frame("CardLayoutTest");
final CardLayout card = new CardLayout(10, 10);
f.setLayout(card); // CardLayout๋ก ์ค์
Panel card1 = new Panel();
card1.setBackground(Color.lightGray);
card1.add(new Label("Card 1"));
Panel card2 = new Panel();
card2.add(new Label("Card 2"));
card2.setBackground(Color.orange);
Panel card3 = new Panel();
card3.add(new Label("Card 3"));
card3.setBackground(Color.cyan);
f.add(card1, "1"); // Frame์ card1์ "1"์ด๋ผ๊ณ ์ด๋ฆ ๋ถ์ฌ ์ถ๊ฐํ๋ค
f.add(card2, "2");
f.add(card3, "3");
class Handler extends MouseAdapter {
public void mouseClicked(MouseEvent e) {
// ๋ง์ฐ์ค ์ค๋ฅธ์ชฝ ๋ฒํผ์ ๋๋ ์ ๋
if (e.getModifiers() == e.BUTTON3_MASK) {
card.previous(f); // CardLayout์ ์ด์ Panel์ ๋ณด์ฌ์ค๋ค
} else {
card.next(f); // CardLayout์ ๋ค์ Panel์ ๋ณด์ฌ์ค๋ค
}
}
} // class Handler
card1.addMouseListener(new Handler());
card2.addMouseListener(new Handler());
card3.addMouseListener(new Handler());
f.setSize(200, 200);
f.setLocation(200, 200);
f.setVisible(true);
card.show(f, "1"); // Frame์ ์ถ๊ฐ๋ Component๋ค ์ค ์ด๋ฆ์ด "1"์ธ ๊ฒ์ ๋ณด์ฌ์ค๋ค.
}
}