package com.task02;
import java.awt.*;
import java.awt.event.*;
public class WelcomeGUL {
static Frame f=new Frame("Welcome GUL");
static Label lb1=new Label("欢迎进入java的图形用户界面!");
static Label lb2=new Label("在这里显示按钮事件描述");
static Button b1=new Button("单击我");
static Button b2=new Button("退出");
public static void main(String[] args) {
f.setSize(200,200);
f.setLayout(new FlowLayout());
f.add(lb1);
f.add(lb2);
f.add(b1);
f.add(b2);
b1.addMouseListener(new ButtonlHandler());
b2.addActionListener(new Button2Handler());
f.setVisible(true);
}
class Button1Handler implements MouseListener{
public void mouseClicked(MouseEvent e) {
WelcomeGUL.lb2.setText("已单击鼠标!");
}
public void mouseEntered(MouseEvent e) {
WelcomeGUL.lb2.setText("已进入按钮上方!");
}
public void mouseExited(MouseEvent e) {
WelcomeGUL.lb2.setText("已离开按钮上方!");
}
public void mousePressed(MouseEvent e) {
WelcomeGUL.lb2.setText("已按下按钮!");
}
public void mouseReleased(MouseEvent e) {
}
}
class Button2Handler implements ActionListener{
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
}
java编写程序,提示说20行有问题
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
4条回答 默认 最新
- CSDN专家-sinJack 2022-01-04 09:55关注
Button1Handler 是普通类的话,就定义在主类外面。静态内部类的话,就用static修饰。
import java.awt.*; import java.awt.event.*; public class WelcomeGUL { static Frame f=new Frame("Welcome GUL"); static Label lb1=new Label("欢迎进入java的图形用户界面!"); static Label lb2=new Label("在这里显示按钮事件描述"); static Button b1=new Button("单击我"); static Button b2=new Button("退出"); public static void main(String[] args) { f.setSize(200,200); f.setLayout(new FlowLayout()); f.add(lb1); f.add(lb2); f.add(b1); f.add(b2); b1.addMouseListener(new Button1Handler()); b2.addActionListener(new Button2Handler()); f.setVisible(true); } } class Button1Handler implements MouseListener{ public void mouseClicked(MouseEvent e) { WelcomeGUL.lb2.setText("已单击鼠标!"); } public void mouseEntered(MouseEvent e) { WelcomeGUL.lb2.setText("已进入按钮上方!"); } public void mouseExited(MouseEvent e) { WelcomeGUL.lb2.setText("已离开按钮上方!"); } public void mousePressed(MouseEvent e) { WelcomeGUL.lb2.setText("已按下按钮!"); } public void mouseReleased(MouseEvent e) { } } class Button2Handler implements ActionListener{ public void actionPerformed(ActionEvent e) { System.exit(0); } }
静态内部类
import java.awt.*; import java.awt.event.*; public class WelcomeGUL { static Frame f=new Frame("Welcome GUL"); static Label lb1=new Label("欢迎进入java的图形用户界面!"); static Label lb2=new Label("在这里显示按钮事件描述"); static Button b1=new Button("单击我"); static Button b2=new Button("退出"); public static void main(String[] args) { f.setSize(200,200); f.setLayout(new FlowLayout()); f.add(lb1); f.add(lb2); f.add(b1); f.add(b2); b1.addMouseListener(new Button1Handler()); b2.addActionListener(new Button2Handler()); f.setVisible(true); } static class Button1Handler implements MouseListener{ public void mouseClicked(MouseEvent e) { WelcomeGUL.lb2.setText("已单击鼠标!"); } public void mouseEntered(MouseEvent e) { WelcomeGUL.lb2.setText("已进入按钮上方!"); } public void mouseExited(MouseEvent e) { WelcomeGUL.lb2.setText("已离开按钮上方!"); } public void mousePressed(MouseEvent e) { WelcomeGUL.lb2.setText("已按下按钮!"); } public void mouseReleased(MouseEvent e) { } } static class Button2Handler implements ActionListener{ public void actionPerformed(ActionEvent e) { System.exit(0); } } }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用
悬赏问题
- ¥15 关于#vscode#的问题:布料滤波算法中C++实现pcl在Vscode中pcl库没有#include <pcl>
- ¥15 fpga:ov5640采集tft显示
- ¥20 创建taro项目,在vscode上能够写跨平台写微信小程序代码
- ¥15 网站出现错误跳转问题
- ¥15 Docker容器里已经安装了ssh,但打包迁移到新机器一直容器一直提示unrecognized service。
- ¥15 综合布线实例设计,就好看好看不恐怖可好滤镜好聚
- ¥15 使用moviepy库视频合并时出错
- ¥30 FLUENT液固传质UDF
- ¥15 怎么看梯度直方图以,怎么判断梯度消失/爆炸,怎么解决
- ¥15 aspnetdll文件访问拒绝