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)
}
}
}
给按钮添加 ActionPerform 事件 内容写System.exit(0);package com.lx
import java.awt.Button
import java.awt.FlowLayout
import java.awt.Frame
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
public class Test implements ActionListener {
Frame f = new Frame()
public static void main(String[] args) {
Test t = new Test()
t.init()
}
private void init() {
Button b = new Button("exit")
b.addActionListener(this)
f.add(b)
f.setLayout(new FlowLayout())
f.setSize(100,100)
f.setVisible(true)
}
public void actionPerformed(ActionEvent arg0) {
f.setVisible(false)
f.dispose()
System.exit(0)
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)