GUI
目录
GUI设计基础入门
流式布局
package yang;
import java.awt.*;
public class Button1 {
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.RIGHT(右)(中)(左)
frame.setLayout(new FlowLayout(FlowLayout.RIGHT));
frame.setSize(200,200);
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
//可见性
}
}
东西南北中
package yang;
import java.awt.*;
public class Button2 {
public static void main(String[] args) {
Frame frame = new Frame("Test BorderLayout");
Button east = new Button("east");
Button west = new Button("west");
Button north = new Button("north");
Button south = new Button("south");
Button center = new Button("center");
frame.add(east,BorderLayout.EAST);
frame.add(west,BorderLayout.WEST);
frame.add(north,BorderLayout.NORTH);
frame.add(south,BorderLayout.SOUTH);
frame.add(center,BorderLayout.CENTER);
frame.setSize(600,600);
frame.setResizable(false);
frame.setVisible(true);
}
}
表格布局
package yang;
import java.awt.*;
public class Button3 {
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");
Button button4 = new Button("button4");
Button button5 = new Button("button5");
Button button6 = new Button("button6");
//GridLayout(3(行),2(列),10(左右距离),10(上下距离))
frame.setLayout(new GridLayout(3,2,10,10));
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.add(button5);
frame.add(button6);
frame.pack();
frame.setVisible(true);
}
}
示例:混合布局
package yang;
import java.awt.*;
public class ButtonZero {
public static void main(String[] args) {
Frame frame = new Frame();
frame.setSize(400,300);
frame.setLocation(300,400);
frame.setBackground(Color.BLACK);
frame.setVisible(true);
frame.setLayout(new GridLayout(2,1));
Panel p1 = new Panel(new BorderLayout());
Panel p2 = new Panel(new GridLayout(2,1));
Panel p3 = new Panel(new BorderLayout());
Panel p4 = new Panel(new GridLayout(2,2));
p1.add(new Button("East-1"),BorderLayout.EAST);
p1.add(new Button("West-1"),BorderLayout.WEST);
p2.add(new Button("1"));
p2.add(new Button("2"));
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("Button"+1));
}
p3.add(p4,BorderLayout.CENTER);
frame.add(p1);
frame.add(p3);
}
}