java事件处理button如何单击按钮退出
importjava.awt.*;importjava.awt.event.*;publicclasslesson1{privateFramef;privateButto...
import java.awt.*;
import java.awt.event.*;
public class lesson1
{
private Frame f;
private Button b;
public lesson1() {
f = new Frame("event");
b = new Button("Exit");
}
public void launchFrame()
{
b.addMouseListener(new ButtonHandler());
f.addWindowListener(new ClosingHander());
f.add(b,BorderLayout.CENTER);
f.setSize(400,300);
f.setVisible(true);
f.setVisible(true);
f.setSize(400,300);
}
public static void main(String args[])
{
lesson1 aa=new lesson1();
aa.launchFrame();
}
}
class ButtonHandler extends MouseAdapter{
{
public void mouseClicked(MouseEvent e)
{
if(e.getActionCommand().equals("Eixt"))
{
System.exit(0);
}
}
}
class ClosingHander extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
} 展开
import java.awt.event.*;
public class lesson1
{
private Frame f;
private Button b;
public lesson1() {
f = new Frame("event");
b = new Button("Exit");
}
public void launchFrame()
{
b.addMouseListener(new ButtonHandler());
f.addWindowListener(new ClosingHander());
f.add(b,BorderLayout.CENTER);
f.setSize(400,300);
f.setVisible(true);
f.setVisible(true);
f.setSize(400,300);
}
public static void main(String args[])
{
lesson1 aa=new lesson1();
aa.launchFrame();
}
}
class ButtonHandler extends MouseAdapter{
{
public void mouseClicked(MouseEvent e)
{
if(e.getActionCommand().equals("Eixt"))
{
System.exit(0);
}
}
}
class ClosingHander extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
} 展开
3个回答
展开全部
如果是要实现单击按钮退出,建议删掉这行代码:
if(e.getActionCommand().equals("Eixt"))
或者,将实现接口的两个类变为public的内部类,同时稍微修改下,如下
import java.awt.*;
import java.awt.event.*;
public class lesson1 {
private Frame f;
private Button b;
public lesson1() {
f = new Frame("event");
b = new Button("Exit");
}
public void launchFrame() {
b.addMouseListener(new ButtonHandler());
f.addWindowListener(new ClosingHander());
f.add(b, BorderLayout.CENTER);
f.setSize(400, 300);
f.setVisible(true);
f.setVisible(true);
f.setSize(400, 300);
}
public static void main(String args[]) {
lesson1 aa = new lesson1();
aa.launchFrame();
}
class ButtonHandler extends MouseAdapter {
public void mouseClicked(MouseEvent e)
{
if (e.getSource() == b) {
System.exit(0);
}
}
}
class ClosingHander extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
}
if(e.getActionCommand().equals("Eixt"))
或者,将实现接口的两个类变为public的内部类,同时稍微修改下,如下
import java.awt.*;
import java.awt.event.*;
public class lesson1 {
private Frame f;
private Button b;
public lesson1() {
f = new Frame("event");
b = new Button("Exit");
}
public void launchFrame() {
b.addMouseListener(new ButtonHandler());
f.addWindowListener(new ClosingHander());
f.add(b, BorderLayout.CENTER);
f.setSize(400, 300);
f.setVisible(true);
f.setVisible(true);
f.setSize(400, 300);
}
public static void main(String args[]) {
lesson1 aa = new lesson1();
aa.launchFrame();
}
class ButtonHandler extends MouseAdapter {
public void mouseClicked(MouseEvent e)
{
if (e.getSource() == b) {
System.exit(0);
}
}
}
class ClosingHander extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
}
展开全部
监听按钮,当按钮被点击时,就退出。代码如下:
jb_button.addMouseListener(new MouseAdapter() { // 对jb_button按钮添加监听事件
@Override
public void mouseClicked(MouseEvent e) { // 当鼠标点击时
System.exit(0); // 退出
}
});
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
if(e.getActionCommand().equals("Eixt"))
"exit" 写错了吧...
"exit" 写错了吧...
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询