1.编写一个Application程序,在生成的窗体的最下方,生成两个按纽,"Display"和"Exit".当按下Dispaly时,显示"我们来play一个game.".当按下"Exit"时,关闭窗口,退出应用程序.当单击窗体上的关闭按纽时,也可关闭程序.
2.编写一个含有主菜单File和Help的程序,其中File中有普通菜单项Open和Exit,Help中有普通菜单项Document.若对这三个普通菜单项单击操作时,有:
Open:则显示信息: I love this game.
Exit:则退出应用程序.
Document:则显示信息:Game is over ! Goodbye!
(提示:用ActionListener接口对AtionEvent事件处理.)
2.编写一个含有主菜单File和Help的程序,其中File中有普通菜单项Open和Exit,Help中有普通菜单项Document.若对这三个普通菜单项单击操作时,有:
Open:则显示信息: I love this game.
Exit:则退出应用程序.
Document:则显示信息:Game is over ! Goodbye!
(提示:用ActionListener接口对AtionEvent事件处理.)
import java.awt.*;
import java.awt.event.*;class Test extends Frame implements ActionListener
{Button display,exit;
Panel panel;
TextArea ta;
Test()
{
setBounds(100,100,200,200);
display=new Button("display");
exit=new Button("exit");
panel=new Panel();
ta=new TextArea (10,10);
setLayout(new BorderLayout());
display.addActionListener(this);
exit.addActionListener(this);
panel.add(display);
panel.add(exit);
add(panel,BorderLayout.SOUTH);
add(ta ,BorderLayout.CENTER);
setVisible(true);
addWindowListener(new WindowAdapter()
{public void windowClosing()
{System.exit(0);}
});
validate();
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==display)
{ta.setText("我们来play一个game.");
}
if (e.getSource()==exit)
{System.exit(0);
} }
public static void main(String args[])
{new Test(); }
}
//第二个程序
import java.awt.*;
import java.awt.event.*;class Test extends Frame implements ActionListener
{Menu file,help;
MenuBar mb;
MenuItem open,exit,document;
TextArea ta;
Test()
{
setBounds(100,100,200,200);
file=new Menu("File");
help=new Menu("Help");
open=new MenuItem("Open");
exit=new MenuItem("Exit");
document=new MenuItem("Document");
ta=new TextArea(10,10);
mb=new MenuBar();
add(ta);
open.addActionListener(this);
exit.addActionListener(this);
document.addActionListener(this);
file.add(open);
file.add(exit);
help.add(document);
mb.add(file);
mb.add(help );
setMenuBar(mb);
setVisible(true);
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(0);}
});
validate();
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==open)
{ta.setText("I love this game.");
}
if (e.getSource()==exit)
{System.exit(0);
}
if (e.getSource()==document)
{ta.setText("Game is over ! Goodbye!");
} }
public static void main(String args[])
{new Test(); }
}
记得给分哦