📄 actioneventdemo.java
字号:
import java.awt.*;import java.awt.event.*;// The ActionEventDemo class, which is a Frame, also serves// as an ActionListener.public class ActionEventDemo extends Frame implements ActionListener { Button button; MenuBar mb; Menu editMenu; MenuItem clearMI; TextField enterTF, displayTF; public ActionEventDemo() {// A button is created that is intended to terminate the// application when it is pressed. The button registers// an ActionListener that is implemented as an anonymous inner class.// When the button is pressed, the actionPerformed() method defined// in the anonymous inner class is called and the program terminates. button = new Button("Quit"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { System.exit(0); } });// Two TextFields are created. The first one, enterTF, is for // the user to type in some text. When the Return key is pressed,// the contents of enterTF will be displayed in the second textfield.// The TextFields register the ActionListener implemented// by the ActionEventDemo class. The addActionListener() method is// passed the "this" reference meaning the ActionEventDemo object. enterTF = new TextField(20); enterTF.addActionListener(this); displayTF = new TextField(20); displayTF.setEditable(false);// A MenuBar is created that contains one Menu that contains one// MenuItem. Then the MenuItem is selected, the contents of the// TextField at the bottom of the Frame are cleared. clearMI = new MenuItem("clear"); clearMI.addActionListener(this); editMenu = new Menu("Edit"); editMenu.add(clearMI); mb = new MenuBar(); mb.add(editMenu); setMenuBar(mb); Panel cp = new Panel(); cp.add(enterTF); cp.add(button); Panel sp = new Panel(); sp.add(displayTF); add(cp, BorderLayout.CENTER); add(sp, BorderLayout.SOUTH); setBounds(100, 100, 300, 200); setVisible(true); }// The ActionEventDemo class serves as an ActionListener and must// implement the actionPerformed() method. If the action command// associated with the ActionEvent is "clear" that means the button// has been pressed and the contents of the displayTF TextField are// cleared. Otherwise, the ActionEvent was generated by the TextField// and the content of the displayTF TextField is set to be the text// contained by the enterTF TextField. public void actionPerformed(ActionEvent ae) { if ( ae.getActionCommand().equals("clear") ) { displayTF.setText(""); }else { displayTF.setText(enterTF.getText()); } } public static void main(String args[]) { ActionEventDemo aed = new ActionEventDemo(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -