这个JAVA在eclipse点击run没反应怎么回事
这个JAVA在eclipse点击run没反应怎么回事packagecom.task02;importjava.awt.*;importjava.awt.event.*;p...
这个JAVA在eclipse点击run没反应怎么回事package com.task02;
import java.awt.*;
import java.awt.event.*;
public class WelcomeGUI {
static Frame f = new Frame("Welcome GUI");
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) {
// TODO Auto-generated method stub
f.setSize(200,200);
f.setLayout(new FlowLayout());
f.add(lb1);
f.add(lb2);
f.add(b1);
f.add(b2);
b1.addMouseListener(new Button1Handler());
b1.addActionListener(new Button2Handler());
}
}
class Button1Handler implements MouseListener {
public void mouseClicked(MouseEvent e) {
WelcomeGUI.lb2.setText("以单击鼠标!");
}
public void mouseEntered(MouseEvent e) {
WelcomeGUI.lb2.setText("已进入按钮上方!");
}
public void mouseExited(MouseEvent e) {
WelcomeGUI.lb2.setText("已离开按钮上方!");
}
public void mousePressed(MouseEvent e) {
WelcomeGUI.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 WelcomeGUI {
static Frame f = new Frame("Welcome GUI");
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) {
// TODO Auto-generated method stub
f.setSize(200,200);
f.setLayout(new FlowLayout());
f.add(lb1);
f.add(lb2);
f.add(b1);
f.add(b2);
b1.addMouseListener(new Button1Handler());
b1.addActionListener(new Button2Handler());
}
}
class Button1Handler implements MouseListener {
public void mouseClicked(MouseEvent e) {
WelcomeGUI.lb2.setText("以单击鼠标!");
}
public void mouseEntered(MouseEvent e) {
WelcomeGUI.lb2.setText("已进入按钮上方!");
}
public void mouseExited(MouseEvent e) {
WelcomeGUI.lb2.setText("已离开按钮上方!");
}
public void mousePressed(MouseEvent e) {
WelcomeGUI.lb2.setText("已按下按钮!");
}
public void mouseReleased(MouseEvent e) {
}
}
class Button2Handler implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
} 展开
2个回答
展开全部
主要问题: 没有让窗口可见
次要问题:1 b1添加了2次响应事件,b2没有添加....
2 给窗口添加关闭响应事件
参考代码. 修改后的main方法
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());//这里的b1修改成b2
f.setLocationRelativeTo(null);//窗口居中
f.setVisible(true);//窗口可见
f.addWindowListener(new WindowAdapter() {//当点击窗口右上角的关闭按钮时,程序退出
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询