JAVA SE基础笔记(四)-re
JAVA-GUI编程 笔记
- 1.简介
- 2、AWT
-
- 2.1、实现如图1-2
-
- (1)面向过程写法
- (2)内部类写法
- (3)完全改造成面向对象
- 3、Swing
-
- 3.1、鼠标花点
- 3.2、弹窗
- 3.3、标签(Icon和ImageIcon)
- 3.4、JScroll面板
- 3.5、JButton(单选框、复选框)
-
- 3.5.1、单选框
- 3.5.2、复选框
- 4、贪吃蛇
AWT
面向过程写法
package 寒假训练.狂神.GUI;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestCul {
public static void main(String[] args) {
new Calculator();
}
}
class Calculator extends Frame{
TextField num1,num2,num3;
public Calculator(){
//创建三个文本框
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(20);
Button button = new Button("=");
Label label = new Label("+");
//创建事件监听器
buttonActionListener listener = new buttonActionListener(num1,num2,num3);
button.addActionListener(listener);
//布局
setLayout(new FlowLayout(10));
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setSize(480,70);
setBackground(new Color(255,213,123));
//监听鼠标事件,关闭frame
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setVisible(true);
}
}
class buttonActionListener implements ActionListener{
TextField num1,num2,num3;
public buttonActionListener(TextField num1, TextField num2, TextField num3) {
this.num1 = num1;
this.num2 = num2;
this.num3 = num3;
}
@Override
public
package 寒假训练.狂神.GUI;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestCul {
public static void main(String[] args) {
new Calculator();
}
}
class Calculator extends Frame{
TextField num1,num2,num3;
public Calculator(){
//创建三个文本框
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(20);
Button button = new Button("=");
Label label = new Label("+");
//创建事件监听器
buttonActionListener listener = new buttonActionListener(num1,num2,num3);
button.addActionListener(listener);
//布局
setLayout(new FlowLayout(10));
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setSize(480,70);
setBackground(new Color(255,213,123));
//监听鼠标事件,关闭frame
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setVisible(true);
}
}
class buttonActionListener implements ActionListener{
TextField num1,num2,num3;
public buttonActionListener(TextField num1, TextField num2, TextField num3) {
this.num1 = num1;
this.num2 = num2;
this.num3 = num3;
}
@Override
public