JAVA语言学习-Day8
GUI
组件:窗口、弹框、面板、文本框、列表框、按钮、图片、监听事件、鼠标、键盘事件、破解工具
1. 简介
Gui的核心:Swing AWT
-
界面不美观
-
2.AWT
awt介绍:
-
包含了很多的接口和类
-
元素:窗口、按钮、文本框
-
java.awt.*
-
组件Component
-
button、TextArea、Label... 存放在容器中
-
容器Container
-
Window
-
Frame
-
Dialog
-
-
面板Panel
-
Applet
-
-
-
组件和容器
-
Frame
Frame f = new Frame("JAVA图形界面窗口");
f.setVisible(true);//设置可见
f.setBackground(Color.yellow);//背景色
// f.setSize(400, 400);//设置大小
// f.setLocation(200,200);//初始位置
f.setResizable(false);//设置大小固定
f.setBounds(200,200,400,400);//等价设置大小及初始位置
-
Panel
Frame f = new Frame();
f.setLayout(null);
f.setBounds(200, 200, 400, 400);
f.setBackground(Color.gray);
Panel p = new Panel();
p.setBounds(50,50,100,100);//panel设置坐标,相对于frame
p.setBackground(new Color(193,15,60));
f.add(p);
f.setVisible(true);
//监听事件
//监听窗口关闭事件,适配器模式
f.addWindowListener(new WindowAdapter() {
-
布局管理器
-
流式布局
-
东南西北中布局
-
表格布局
事件监听
addActionListener();
public void actionPerformed(ActionEvent e){
//e.getActionCommand();获取按钮信息
}
组合+内部类
-
内部类
class Calculator extends Frame{
TextField t1,t2,t3;
public Calculator() {
// TextField t1 = new TextField(10);
// TextField t2 = new TextField(10);
// TextField t3 = new TextField(10);
t1 = new TextField(10);
t2 = new TextField(10);
t3 = new TextField(10);
Button b = new Button("=");
// b.addActionListener(new MyCalculatorListener(t1,t2,t3));
b.addActionListener(new MyCalculatorListener());
Label l = new Label("+");
setLayout(new FlowLayout());
add(t1);
add(l);
add(t2);
add(b);
add(t3);
pack();
setVisible(true);
}
private class MyCalculatorListener implements ActionListener{
// private TextField t1;
// private TextField t2;
// private TextField t3;
// Calculator c = null;
// public MyCalculatorListener(TextField t1,TextField t2,TextField t3) {
// public MyCalculatorListener(Calculator c) {
//// this.t1 = t1;
//// this.t2 = t2;
//// this.t3 = t3;
// this.c = c;
// }