Frame
frame.setVisible(true)
// frame.setLocation(x, y);
// frame.setSize(w,h);
frame.setBounds(x,y,w,h);
frame.setBackground(Color color);
布局
frame.setLayout(null);
Button leftButton = new Button("Left");
Button rightButton = new Button("Right");
myFrame1.setLayout(new FlowLayout(FlowLayout.LEFT));
myFrame1.add(leftButton);
myFrame1.add(rightButton);
// 默认: CENTER
// frame.setLayout(new FlowLayout(FlowLayout.CENTER));
// frame.setLayout(new FlowLayout(FlowLayout.RIGHT));
Button installButton = new Button("Install");
Button exitButton = new Button("Exit");
myFrame2.setLayout(new BorderLayout());
myFrame2.add(exitButton, BorderLayout.WEST);
myFrame2.add(installButton, BorderLayout.EAST);
Panel
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");
myFrame3.setLayout(new GridLayout(3,2));
myFrame3.add(button1);
myFrame3.add(button2);
myFrame3.add(button3);
myFrame3.add(button4);
myFrame3.add(button5);
myFrame3.add(button6);
总代码
package com.shu.gui;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Execute {
public static void main(String[] args) {
MyFrame myFrame1 = new MyFrame(200,200,100,100,Color.BLUE);
MyFrame myFrame2 = new MyFrame(200,200,100,300,Color.GREEN);
MyFrame myFrame3 = new MyFrame(200,200,300,100,Color.YELLOW);
// MyFrame myFrame4 = new MyFrame(200,200,300,300,Color.PINK);
Button leftButton = new Button("Left");
Button rightButton = new Button("Right");
myFrame1.setLayout(new FlowLayout(FlowLayout.LEFT));
myFrame1.add(leftButton);
myFrame1.add(rightButton);
Button installButton = new Button("Install");
Button exitButton = new Button("Exit");
myFrame2.setLayout(new BorderLayout());
myFrame2.add(exitButton, BorderLayout.WEST);
myFrame2.add(installButton, BorderLayout.EAST);
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");
myFrame3.setLayout(new GridLayout(3,2));
myFrame3.add(button1);
myFrame3.add(button2);
myFrame3.add(button3);
myFrame3.add(button4);
myFrame3.add(button5);
myFrame3.add(button6);
//myFrame3.pack();
}
}
class MyFrame extends Frame {
static int id = 0;
public MyFrame(int w, int h, int x, int y, Color color) {
super("MyFrame" + (++id));
setBackground(color);
setSize(w, h);
setLocation(x, y);
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}