본문 바로가기
Code/GUI

[Java Code] 패널 - 영역 분리하기

by 스응 2023. 2. 14.
728x90
728x90

코드

public class MyFramePanel extends JFrame {

    JButton[] buttons = new JButton[6];

    private JPanel panel1;
    private JPanel panel2;

    public MyFramePanel2() {
        initData();
        setInitLayout();
    }

    private void initData() {
        setTitle("패널 연습");
        setSize(600, 400);
        setDefaultCloseOperation(3);

        panel1 = new JPanel();
        panel1.setBackground(Color.red);
        panel2 = new JPanel();
        panel2.setBackground(Color.yellow);

        for (int i = 0; i < buttons.length; i++) {
            buttons[i] = new JButton("버튼 " + (i + 1));
        }

    }

    private void setInitLayout() {
        add(panel1, BorderLayout.CENTER);
        panel1.setLayout(new FlowLayout(FlowLayout.LEFT));
        add(panel2, BorderLayout.SOUTH);
        panel2.setLayout(new FlowLayout(FlowLayout.RIGHT));

        for (int j = 0; j < 3; j++) {
            panel1.add(buttons[j]);
        }

        for (int h = 3; h < buttons.length; h++) {
            panel2.add(buttons[h]);
        }

        setVisible(true);
    }

    public static void main(String[] args) {
        new MyFramePanel();
    }

}

 

실행

320x100
반응형

댓글