GUI基础编程
程序员文章站
2022-07-14 17:22:00
...
GUI基础编程 Day 03
按钮监听事件
单个按钮
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 TestActionEvent01 {
public static void main(String[] args) {
Frame frame = new Frame();
Button button = new Button("stare");
MyMonitor myMonitor = new MyMonitor();
button.addActionListener(myMonitor);
WindowClose(frame); //关闭窗口
frame.add(button,BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
frame.setBounds(100,100,500,500);
}
//关闭窗口事件
public static void WindowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyMonitor implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("aaa");
}
}
多个按钮实现
多个按钮同时使用一个监听事件
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 TestActionEvent02 {
public static void main(String[] args) {
MyFrame01 myFrame01 = new MyFrame01();
}
}
class MyFrame01 extends Frame {
public MyFrame01() {
Button button01 = new Button("start");
Button button02 = new Button("stop");
MyActionListener02 myActionListener02 = new MyActionListener02();
button01.addActionListener(myActionListener02);
button02.addActionListener(myActionListener02);
add(button01, BorderLayout.NORTH);
add(button02, BorderLayout.SOUTH);
setVisible(true);
setBounds(100, 100, 500, 500);
pack();
setBackground(Color.red);
}
}
//多个按钮使用一个监听事件类
class MyActionListener02 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("输出为:"+e.getActionCommand());
if (e.getActionCommand().equals("start")){
System.out.println("开始");
}else if (e.getActionCommand().equals("stop")){
System.out.println("结束!");
}
}
}
注意在哪里添加关闭窗口的方法
输入框监听事件
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 TestText01 {
public static void main(String[] args) {
//启动!
MyFrame myFrame = new MyFrame();
}
}
class MyFrame extends Frame{
public MyFrame(){
TextField textField = new TextField();
add(textField);
//监听文本框输入的文字
MyActionListener myActionListener = new MyActionListener();
textField.addActionListener(myActionListener);
//设置替换编码 密码中常用
textField.setEchoChar('*');
setVisible(true);
pack();
}
}
class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField textField = (TextField) e.getSource(); //获取资源,返回一个对象
System.out.println(textField.getText());
textField.setText(""); //null ""
}
}
总结:
简易计算器制作
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestCalc{
public static void main(String[] args) {
Calculator calculator = new Calculator();
}
}
class Calculator extends Frame {
public Calculator(){
//三个文本框
TextField num1 = new TextField(10);
TextField num2 = new TextField(10);
TextField num3 = new TextField(20);
//一个按钮
Button button = new Button("=");
button.addActionListener(new MyCalculator(num1,num2,num3));
//一个标签
Label label = new Label("+");
//流式布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
setVisible(true);
setBounds(100,100,500,500);
pack();
}
}
class MyCalculator implements ActionListener{
//获取数据
private TextField num1,num2,num3;
public MyCalculator(TextField num1,TextField num2,TextField num3){
this.num1 = num1;
this.num2 = num2;
this.num3 = num3;
}
@Override
public void actionPerformed(ActionEvent e) {
//1.获取加数
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
//2.将加数做加法运算
num3.setText(""+(n1+n2));
//3.清除前面的数据
num1.setText("");
num2.setText("");
}
}
优化
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestCalc{
public static void main(String[] args) {
new Calculator().loadFrame();
}
}
//计算机类
class Calculator extends Frame {
//属性
TextField num1,num2,num3;
//方法
public void loadFrame(){
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(20);
Button button = new Button("=");
button.addActionListener(new MyCalculator(this));
Label label = new Label("+");
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
setVisible(true);
setBounds(100,100,500,500);
pack();
}
}
class MyCalculator implements ActionListener{
//获取计算机对象,在一个类中组合另一个类
Calculator calculator = null;
public MyCalculator(Calculator calculator){
this.calculator = calculator;
}
@Override
public void actionPerformed(ActionEvent e) {
//1.获取加数
int n1 = Integer.parseInt(calculator.num1.getText());
int n2 = Integer.parseInt(calculator.num2.getText());
//2.将加数做加法运算
calculator.num3.setText(""+(n1+n2));
//3.清除前面的数据
calculator.num1.setText("");
calculator.num2.setText("");
}
}
内部类
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestCalc{
public static void main(String[] args) {
new Calculator().loadFrame();
}
}
//计算机类
class Calculator extends Frame {
//属性
TextField num1,num2,num3;
//方法
public void loadFrame(){
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(20);
Button button = new Button("=");
button.addActionListener(new MyCalculator());
Label label = new Label("+");
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
setVisible(true);
setBounds(100,100,500,500);
pack();
}
//内部类
//内部类的最大好处 可以畅通无阻的访问外部类的属性和方法
private class MyCalculator implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//1.获取加数
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
//2.将加数做加法运算
num3.setText(""+(n1+n2));
//3.清除前面的数据
num1.setText("");
num2.setText("");
}
}
}
上一篇: 文件系统破坏导致系统无法启动
下一篇: Java GUI编程