简单的java代码,求讲解。。

import java.awt.event.*; 引入库 public class Test2 implements ActionListener{ 实现ActionListener JButton button; 定义按钮变量 public static void main(String[] args) { Test2 gui = new Test2(); 定义、实现类 gui.go(); 开始执行、打开界面 } publicvoid go(){ JFrame fra...
简单的java代码,求讲解。。
import javax.swing.*;
import java.awt.event.*; 引入库

public class Test2 implements ActionListener{ 实现ActionListener

JButton button; 定义按钮变量

public static void main(String[] args) {
Test2 gui = new Test2(); 定义、实现类

gui.go(); 开始执行、打开界面

}

publicvoid go(){
JFrame frame = new JFrame(); 定义、实例化窗口

button = new JButton("click me"); 实例化按钮

button.addActionListener(this); 按钮事件监听

frame.getContentPane().add(button); 放按钮到界面

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 关闭时退出窗口

frame.setSize(300,300); 大小

frame.setVisible(true); 可见

}

publicvoid actionPerformed(ActionEvent event){ 事件的方法

button.setText("I've been clicked!"); 改变按钮的文字

}
}2013-03-27
1. 理解包含关系
2. 理解事件2013-03-27
mengvlog 阅读 365 次 更新于 2025-10-29 09:40:45 我来答关注问题0
檬味博客在线解答立即免费咨询

代码相关话题

Copyright © 2023 WWW.MENGVLOG.COM - 檬味博客
返回顶部