1. 程式人生 > >java,枯燥中遇見美麗JFrame,窗體(面板,JPanel)

java,枯燥中遇見美麗JFrame,窗體(面板,JPanel)

image title lose add src out center public xtend

package com.company;

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

public class Main extends JFrame {//JPanel面板
    public Main(){
        setBounds(100,100,500,300);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Container c=getContentPane();
        c.setLayout(new GridLayout(2,2,10,10));

        JPanel p1=new JPanel();
        p1.setLayout(new GridLayout(1,3,10,10));
        JPanel p2=new JPanel(new BorderLayout());
        JPanel p3=new JPanel(new GridLayout(1,2,10,10));
        JPanel p4=new JPanel(new GridLayout(2,1,10,10));
        /*
        添加布局
         */
        p1.setBorder(BorderFactory.createTitledBorder("面板1"));
        p2.setBorder(BorderFactory.createTitledBorder("面板2"));
        p3.setBorder(BorderFactory.createTitledBorder("面板3"));
        p4.setBorder(BorderFactory.createTitledBorder("面板4"));

        p1.add(new JButton("p1"));
        p1.add(new JButton("p1"));
        p1.add(new JButton("p1"));
        p1.add(new JButton("p1"));

        p2.add(new JButton("p2"),BorderLayout.CENTER);
        p2.add(new JButton("p2"),BorderLayout.SOUTH);
        p2.add(new JButton("p2"),BorderLayout.WEST);
        p2.add(new JButton("p2"),BorderLayout.NORTH);
        p2.add(new JButton("p2"),BorderLayout.EAST);

        p3.add(new JButton("p3") );
        p3.add(new JButton("p3") );

        p4.add(new JButton("p4") );
        p4.add(new JButton("p4") );


        c.add(p1);c.add(p2);c.add(p3);c.add(p4);
        setVisible(true);
    }

    public static void main(String[] args) {
        new Main();
	// write your code here
    }
}

技術分享圖片

java,枯燥中遇見美麗JFrame,窗體(面板,JPanel)