轉載自:https://blog.csdn.net/qq_21388535/article/details/80058534
實現效果圖
代碼如下:
import javax.swing.JFrame;
import javax.swing.*;
import java.awt.*;
public class Demo0501 extends JFrame{
/**
* @param args
*/
JButton jb1,jb2,jb3,jb4,jb5,jb6;
JPanel jp1,jp2;
public static void main(String[] args) {
// TODO Auto-generated method stub
Demo0501 demo=new Demo0501();
}
public Demo0501(){
jb1=new JButton("西瓜");
jb2=new JButton("蘋果");
jb3=new JButton("香蕉");
jb4=new JButton("荔枝");
jb5=new JButton("葡萄");
jb6=new JButton("桔子");
jp1=new JPanel();
jp2=new JPanel();
jp1.add(jb1);
jp1.add(jb2);
jp2.add(jb4);
jp2.add(jb5);
jp2.add(jb6);
this.add(jp1,BorderLayout.NORTH);
this.add(jp2,BorderLayout.SOUTH);
this.add(jb3);
this.setTitle("多種布局格式");
this.setSize(400,200);
this.setLocation(300,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
JPanel知識點:
JPanel:面板組件,非頂層容器
一個界面只有一個JFrame窗體組件,但可以有多個JPanel面板組件,而JPanel上也可以使用布局管理器,這樣組合可以達到比較復雜的布局效果
注意事項:
1.JPanel是JComponent的子類
2.屬於容器類組件,可以加入別的組件
3.默認布局管理器是FlowLayout