结果图
代码
import javax.swing.*;
import java.awt.*;
public class test {
static class Example11_4 extends JFrame{
JTabbedPane pane; //定义属性 选项卡
//构造方法
public Example11_4(){
setBounds(100,100,500,300);
setVisible(true);
pane = new JTabbedPane(JTabbedPane.LEFT);
pane.add("观看FlowLayount",new FlowLayoutJPane1());
pane.add("观看FridLayout",new GridLayoutJPane1());
pane.add("观看BorderLayout",new BorderLayoutJPane1());
pane.validate();//验证
add(pane,BorderLayout.CENTER);
validate();
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
public static class FlowLayoutJPane1 extends JPanel
{
FlowLayoutJPane1(){
add(new JLabel("FlowLayout布局的面板"));
add(new JButton(new ImageIcon("src/14.jpg")));
add(new JScrollPane(new JTextArea(12,15)));
}
}
static class BorderLayoutJPane1 extends JPanel{
JButton bSouth,bNorth,bEast,bWest;
JTextArea bCenter;
BorderLayoutJPane1(){
setLayout(new BorderLayout());
bSouth = new JButton("南");
bNorth = new JButton("北");
bEast = new JButton("东");
bWest = new JButton("西");
bCenter = new JTextArea("中心");
add(bNorth,BorderLayout.NORTH);
add(bSouth,BorderLayout.SOUTH);
add(bWest,BorderLayout.WEST);
add(bEast,BorderLayout.EAST);
add(bCenter,BorderLayout.CENTER);
validate();
}
}
static class GridLayoutJPane1 extends JPanel{
GridLayoutJPane1(){
GridLayout gridLayout = new GridLayout(12,12);//网格布局
setLayout(gridLayout);
Label label[][]= new Label[12][12];
for(int i =0;i<12;i++)
{
for(int j =0;j<12;j++)
{
label[i][j] = new Label();//为什么?
if((i+j)%2 == 0)
label[i][j].setBackground(Color.black);
else
label[i][j].setBackground(Color.white);
add(label[i][j]);
}
}
}
}
public static void main(String args[]){
new Example11_4();
}
}