JPanel (GridLayout(),FlowLayout(),BorderLayout(),null)

赫连照
2023-12-01
package jyh;

import java.awt.*;
import javax.swing.*;

public class ComplexLayoutDemo extends JFrame{
    private JPanel panel1=new JPanel();
    private JPanel panel2=new JPanel();
    private JPanel panel3=new JPanel();
    private JPanel panel4=new JPanel();
    public ComplexLayoutDemo()
    {
        setSize(500,500);
        setLocation(400,400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        LayoutPanel1();
        LayoutPanel2();
        LayoutPanel3();
        LayoutPanel4();
        setLayout(new GridLayout(2, 2));
        add(panel1);
        add(panel2);
        add(panel3);
        add(panel4);
    }
    private void LayoutPanel1(){
         JButton north=new JButton("北");
         JButton south=new JButton("南");
         JButton east=new JButton("东");
         JButton west=new JButton("西");
         JButton center=new JButton("中");
         panel1.setLayout(new BorderLayout());
         panel1.add(north, BorderLayout.NORTH);
         panel1.add(south, BorderLayout.SOUTH);
         panel1.add(west, BorderLayout.WEST);
         panel1.add(east, BorderLayout.EAST);
         panel1.add(center, BorderLayout.CENTER);
    }
    private void LayoutPanel2(){
        JButton button1=new JButton("按钮 1");
        JButton button2=new JButton("按钮 2");
        JButton button3=new JButton("按钮 3");
        JButton button4=new JButton("按钮 4");
        panel2.setLayout(new FlowLayout());
        panel2.add(button1);
        panel2.add(button2);
        panel2.add(button3);
        panel2.add(button4);
    }
    private void LayoutPanel3(){
        JButton button1=new JButton("按钮 1");
        JButton button2=new JButton("按钮 2");
        JButton button3=new JButton("按钮 3");
        JButton button4=new JButton("按钮 4");
        panel3.setLayout(new GridLayout(2, 2));
        panel3.add(button1);
        panel3.add(button2);
        panel3.add(button3);
        panel3.add(button4);
    }
    private void LayoutPanel4(){
        JButton button=new JButton("按钮");
        JTextField textField=new JTextField("文本框");
        panel4.setLayout(null);
        button.setLocation(20,20);
        button.setSize(100,20);
        textField.setBounds(20, 50, 200, 100);
        panel4.add(button);
        panel4.add(textField);
    }
    public static void main(String[] args) {
        ComplexLayoutDemo frame=new ComplexLayoutDemo();
        frame.setVisible(true);
    }

}
 类似资料: