javaGUI编程
Frame初见
public class TestFrame1 {
public static void main(String[] args) {
Frame frame = new Frame();
frame.setVisible(true);
frame.setSize(800,800);
frame.setBackground(Color.blue);
frame.setLocation(300,300);
frame.setResizable(false);
}
}
MyFrame的封装
public class TestFrame2 {
public static void main(String[] args) {
MyFrame myFrame = new MyFrame(300,300,800,800,Color.blue);
myFrame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyFrame extends Frame {
static int id = 0;
public MyFrame(int x,int y,int w,int h,Color color){ Frame frame = new Frame();
setSize(w,h);
setBackground(color);
setLocation(x,y);
setResizable(false);
setVisible(true);
}
}
panel面板
public class TestFrame3 {
public static void main(String[] args) {
Frame frame = new Frame();
Panel panel = new Panel();
panel.setBackground(Color.white);
panel.setBounds(150,150,150,150);
frame.setResizable(false);
frame.setLayout(null);
frame.add(panel);
frame.setBounds(300,300,500,500);
frame.setBackground(new Color(2, 88, 73));
frame.setLocation(500,500);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
);
}
}
三种布局方式
public class TestAllLayout {
public static void main(String[] args) {
MyFrame frame = new MyFrame(300,300,900,900,Color.green) ;
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");
frame.setLayout(new GridLayout(3,2));
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.add(button5);
frame.add(button6);
frame.pack();
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}