EX) 박스 레이아웃
import java.awt.Color;
import java.awt.Component;
import javax.swing.*;
class MyFrame extends JFrame
{
public MyFrame()
{
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("MyFrame");
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
makeButton(panel, "Button1");
makeButton(panel, "Button2");
makeButton(panel, "Button3");
makeButton(panel, "B4");
add(panel);
setVisible(true);
}
private void makeButton(JPanel panel, String text)
{
JButton button = new JButton(text);
button.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(button);
}
}
public class class1
{
public static void main(String[] args)
{
MyFrame f = new MyFrame();
}
}
* 절대위치로 배치하기
1. 배치관리자를 Null로 설정한다.
- setlayout(null);
2. add() 메소드를 사용하여 컴포넌트를 컨테이너에 추가한다.
-
3. 절대위치를 사용
- setbounds();
4. 컴포넌트의
EX) 절대위치
import java.awt.Color;
import java.awt.Component;
import javax.swing.*;
class MyFrame extends JFrame
{
JButton b1;
private JButton b2, b3;
public MyFrame()
{
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("MyFrame");
JPanel panel = new JPanel();
panel.setLayout(null);
b1 = new JButton("Button 1");
panel.add(b1);
b2 = new JButton("Button 2");
panel.add(b2);
b3 = new JButton("Button 3");
panel.add(b3);
b1.setBounds(20, 5, 95, 30);
b2.setBounds(55, 45, 105, 70);
b3.setBounds(180, 15, 105, 90);
add(panel);
setVisible(true);
}
}
public class class1
{
public static void main(String[] args)
{
MyFrame f = new MyFrame();
}
}
'Study > Java' 카테고리의 다른 글
| 12. 09. 19 (소스) (0) | 2012.09.19 |
|---|---|
| 12. 09. 10 (0) | 2012.09.10 |
| 둘째 날 (0) | 2012.09.03 |
| 프레임 생성 (0) | 2012.08.29 |
| GUI (0) | 2012.08.29 |