Java图像化——三种布局
流式布局FlowLayout
设置为流式布局
package lesson;
import java.awt.*;
public class TestFlowLayout {
public static void main(String[] args) {
Frame frame=new Frame();
//组件 按钮
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
//设置为流式布局 FlowLayout
frame.setLayout(new FlowLayout());
frame.setSize(200,200);
//把按钮添加上去
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
}
}
设置按钮位置为左边
frame.setLayout(new FlowLayout(FlowLayout.LEFT))
东西南北中BorderLayout
package lesson;
import java.awt.*;
public class TestBorderLayout {
public static void main(String[] args) {
Frame frame = new Frame();
Button east=new Button("East");
Button west=new Button("West");
Button south=new Button("South");
Button north=new Button("North");
Button center=new Button("Center");
frame.add(east,BorderLayout.EAST);
frame.add(west,BorderLayout.WEST);
frame.add(south,BorderLayout.SOUTH);
frame.add(north,BorderLayout.NORTH);
frame.add(center,BorderLayout.CENTER);
frame.setSize(200,200);
frame.setVisible(true);
}
}
表格布局GridLayout
package lesson;
import sun.net.ftp.FtpClient;
import java.awt.*;
public class TestGridLayout {
public static void main(String[] args) {
Frame frame = new Frame("TestGirdLayout");
Button btn1=new Button("btn1");
Button btn2=new Button("btn2");
Button btn3=new Button("btn3");
Button btn4=new Button("btn4");
Button btn5=new Button("btn5");
Button btn6=new Button("btn6");
//3hang2lie
frame.setLayout(new GridLayout(3,2));
frame.add(btn1);
frame.add(btn2);
frame.add(btn3);
frame.add(btn4);
frame.add(btn5);
frame.add(btn6);
frame.pack();//自动布局,帮助优化
frame.setVisible(true);
}
}
练习题
package lesson;
import java.awt.*;
public class TestLianxi1 {
public static void main(String[] args) {
Frame frame = new Frame("TestLianxi");
frame.setSize(400,300);
frame.setLocation(300,400);
frame.setBackground(Color.blue);
frame.setVisible(true);
frame.setLayout(new GridLayout(2,1));
//4个面板
Panel p1=new Panel(new BorderLayout());
Panel p3=new Panel(new BorderLayout());
Panel p2=new Panel(new GridLayout(2,1));
Panel p4=new Panel(new GridLayout(2,2));
//上面ok
p1.add(new Button("east-1"),BorderLayout.EAST);
p1.add(new Button("west-1"),BorderLayout.WEST);
p2.add(new Button("p2-btn-1"));
p2.add(new Button("p2-btn-2"));
//把p2加到p1中心
p1.add(p2,BorderLayout.CENTER);
//下面
p3.add(new Button("east-2"),BorderLayout.EAST);
p3.add(new Button("west-2"),BorderLayout.WEST);
//中间四个
for (int i = 0; i <4 ; i++)
{
p4.add(new Button("for-"+i));
}
p3.add(p4,BorderLayout.CENTER);
frame.add(p1);
frame.add(p3);
}}
我自己也又写了一个,但是发现好像少点东西
package lesson;
import java.awt.*;
public class TestLianxi {
public static void main(String[] args) {
Frame frame=new Frame("TestLianxi");
//组件 按钮
Button button1 = new Button("button");
Button button2 = new Button("button");
Button button3 = new Button("button");
Button button4 = new Button("button");
Button button5 = new Button("button");
Button button6 = new Button("button");
Button button7 = new Button("button");
Button button8 = new Button("button");
Button button9 = new Button("button");
Button button10 = new Button("button");
//设置最左边的面板
Panel panel1=new Panel();
panel1.setLayout(new GridLayout(2,1));
panel1.add(button1);
panel1.add(button2);
//设置中间偏上的面板
Panel panel2=new Panel();
panel2.setLayout(new GridLayout(2,1));
panel2.add(button3);
panel2.add(button4);
//设置中间偏下的面板
Panel panel3 = new Panel();
panel3.setLayout(new GridLayout(2,2));
panel3.add(button5);
panel3.add(button6);
panel3.add(button7);
panel3.add(button8);
//设置最右边的面板
Panel panel4=new Panel();
panel4.setLayout(new GridLayout(2,1));
panel4.add(button9);
panel4.add(button10);
//主界面
frame.setBounds(300,500,500,500);
frame.setBackground(new Color(4, 0, 255));
//panel设置坐标,相对于frame的位置
panel1.setBounds(0,0,100,500);
panel1.setBackground(new Color(223, 58, 54));
panel2.setBounds(100,0,300,250);
panel2.setBackground(new Color(103, 223, 34));
panel3.setBounds(100,250,300,250);
panel1.setBackground(new Color(223, 221, 5));
panel4.setBounds(400,0,100,500);
panel4.setBackground(new Color(29, 223, 0));
//frame.add(panel)
frame.add(panel1);
frame.add(panel2);
frame.add(panel3);
frame.add(panel4);
frame.setVisible(true);
}
}