当前位置: 技术问答>java相关
为什么程序不能退出?解决就马上给分。
来源: 互联网 发布时间:2015-02-26
本文导语: 源程序如下,运行的时候点菜单没有反应,为什么啊? package myprojects.mine; import java.awt.*; import java.awt.event.*; class Mine extends Frame { public Mine() { addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent...
源程序如下,运行的时候点菜单没有反应,为什么啊?
package myprojects.mine;
import java.awt.*;
import java.awt.event.*;
class Mine extends Frame {
public Mine() {
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
}
public static void main(String args[]) {
System.out.println("Starting Mine...");
mineFrame mf = new mineFrame();
mf.setSize(new Dimension(600,400));
mf.setTitle("Mine");
mf.setVisible(true);
}
}
class mineFrame extends Frame implements ActionListener
{
MenuBar m_MenuBar ;
Menu menuGame,menuHelp ;
MenuItem mi_Game_New,mi_Game_Easy,mi_Game_Exit;
mineFrame()
{
m_MenuBar = new MenuBar();
menuGame = new Menu("Game");
mi_Game_New = new MenuItem("New");
mi_Game_Easy = new MenuItem("Easy");
mi_Game_Exit = new MenuItem("Exit");
mi_Game_New.setActionCommand("New");
mi_Game_Easy.setActionCommand("Easy");
mi_Game_Exit.setActionCommand("Exit");
menuGame.add( mi_Game_New );
menuGame.addSeparator();
menuGame.add( mi_Game_Easy );
menuGame.addSeparator();
menuGame.add( mi_Game_Exit );
m_MenuBar.add( menuGame );
this.setMenuBar( m_MenuBar );
}
public void actionPerformed(ActionEvent e)
{
if( e.getActionCommand()=="Exit" )
{
dispose();
System.exit(0);
}
}
}
package myprojects.mine;
import java.awt.*;
import java.awt.event.*;
class Mine extends Frame {
public Mine() {
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
}
public static void main(String args[]) {
System.out.println("Starting Mine...");
mineFrame mf = new mineFrame();
mf.setSize(new Dimension(600,400));
mf.setTitle("Mine");
mf.setVisible(true);
}
}
class mineFrame extends Frame implements ActionListener
{
MenuBar m_MenuBar ;
Menu menuGame,menuHelp ;
MenuItem mi_Game_New,mi_Game_Easy,mi_Game_Exit;
mineFrame()
{
m_MenuBar = new MenuBar();
menuGame = new Menu("Game");
mi_Game_New = new MenuItem("New");
mi_Game_Easy = new MenuItem("Easy");
mi_Game_Exit = new MenuItem("Exit");
mi_Game_New.setActionCommand("New");
mi_Game_Easy.setActionCommand("Easy");
mi_Game_Exit.setActionCommand("Exit");
menuGame.add( mi_Game_New );
menuGame.addSeparator();
menuGame.add( mi_Game_Easy );
menuGame.addSeparator();
menuGame.add( mi_Game_Exit );
m_MenuBar.add( menuGame );
this.setMenuBar( m_MenuBar );
}
public void actionPerformed(ActionEvent e)
{
if( e.getActionCommand()=="Exit" )
{
dispose();
System.exit(0);
}
}
}
|
在this.setMenuBar(m_MenuBar);
即倒数十二行上面加一句:
menuGame.addActionListener (this);
即倒数十二行上面加一句:
menuGame.addActionListener (this);