세쨋 날(내부 클래스오 무명클래스 차이점 과제하기)
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();
}
}