一、第一个界面:Frame frame = new Frame();
import java.awt.*;
public class one {
public static void main(String[] args) {
Frame frame = new Frame("第一个图形界面");
//需要设置可见性
frame.setVisible(true);
//设置窗口大小
frame.setSize(400,400);
//设置背景颜色
frame.setBackground(new Color(192,19, 100));
//设置初始位置
frame.setLocation(200,200);
//大小固定
frame.setResizable(false);
//多个窗口
Myframe myframe = new Myframe(100,100,200,200,Color.BLUE);
}
}
//封装实现多个窗口
class Myframe extends Frame{
static int id =0;
public Myframe(int x,int y,int w,int h,Color color){
super("Myframe"+(++id));
setVisible(true);
setBounds(x,y,w,h);
setBackground(color);
}
}
panle画板:
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class two {
public static void main(String[] args) {
Frame frame = new Frame();
Panel panel = new Panel();
frame.setLayout(null); //布局
frame.setBounds(200,200,200,200);
frame.setBackground(Color.blue);
panel.setBounds(50,50,100,100);
panel.setBackground(Color.BLACK);
frame.add(panel);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
布局:
- 流式布局 :frame.setLayout( new FlowLayout() ); //默认居中
- new FlowLayout(FlowLayout.LEFT) //左对齐
- 东西南北中布局:BorderLayout.东南西北
- 网格布局:frame.setLayout( new GridLayout() );
- frame.pack() //设置布局最优样式
事件监听:
Frame frame =new Frame();
Button button=new Button();
因为addActionListener需要一个ActionListener,所以重写一个ActionListener(MyActionListener)
MyActionListener myActionListener =new MyActionListener();
button.addActionListener(myActionListener);
frame.add(button,布局);
frame.pack();
class MyActionListener implements ActionListener{方法}