AWT ( Frame / Button / Layout / JPanel ) - 간단한 예제
1. Frame
package test01.awt;
import java.awt.Frame;
import java.awt.Button;
public class Test01_Frame{
public static void main(String[] args) {
Frame frm=new Frame("나의첫번째프레임");
//가로세로길이 설정
frm.setSize(400,400);
//화면에 보이기
frm.setVisible(true);
frm.add(new Button("버튼1"));
frm.add(new Button("버튼2"));
}
}
2. Button
package test01.awt;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
class MyFrame01 extends Frame{
private Button btn1=new Button("버튼1");
private Button btn2=new Button("버튼2");
private Button btn3=new Button("버튼3");
private Button btn4=new Button("버튼4");
//레이아웃매니져 생성
private FlowLayout layout=new FlowLayout();
public MyFrame01(String title){
super(title);
setLayout(layout);
add(btn1);
add(btn2);
add(btn3);
add(btn4);
//가로세로길이 설정
setSize(400,400);
//화면에 보이기
setVisible(true);
}
}
public class Test02_Frame{
public static void main(String[] args) {
new MyFrame01("나의 두번째 프레임");
}
}
3. layout
package test01.awt;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
class MyFrame03 extends JFrame{
JButton btn1=new JButton("버튼1");
JButton btn2=new JButton("버튼2");
JButton btn3=new JButton("버튼3");
JButton btn4=new JButton("버튼4");
JButton btn5=new JButton("버튼5");
//레이아웃매니져 생성
BorderLayout layout=new BorderLayout();
public MyFrame03(){
super("세번째 프레임");
//프레임에 레이아웃매니져 설정하기
setLayout(layout);
//버튼을 5개 영역(동,서,남,북,가운데)에 배치하기
add(btn1,BorderLayout.NORTH);
// add(btn2,BorderLayout.WEST);
add(btn3,BorderLayout.EAST);
// add(btn4,BorderLayout.SOUTH);
add(btn5,BorderLayout.CENTER);
setSize(400,400);
setVisible(true);
}
}
public class Test03_BorderLayout{
public static void main(String[] args) {
new MyFrame03();
}
}
4. JPanel
package test01.awt;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
class MyFrame04 extends JFrame{
JButton btn1=new JButton("버튼1");
JButton btn2=new JButton("버튼2");
JPanel pan=new JPanel();
JTextArea ta=new JTextArea();
public MyFrame04(){
//패널에 버튼 붙이기
pan.add(btn1);
pan.add(btn2);
//레이아웃 매니져 설정하기
setLayout(new BorderLayout());
//패널을 North영역에 붙이기
add(pan,BorderLayout.NORTH);
add(ta,BorderLayout.CENTER);
setSize(400,400);
setVisible(true);
}
}
public class Test04_JPannel{
public static void main(String[] args) {
new MyFrame04();
}
}