1 import javax.swing.*; 2 import java.awt.event.*; 3 4 class Gui implements ActionListener{ 5 6 JButton button = new JButton("click me"); 7 8 public void show() { 9 JFrame frame = new JFrame(); 10 11 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 12 13 frame.getContentPane().add(button); 14 frame.setSize(300, 200); 15 frame.setVisible(true); 16 17 button.addActionListener(this); 18 } 19 20 public void actionPerformed( ActionEvent event ) { 21 button.setText("I‘ve been clicked!"); 22 } 23 } 24 25 class GuiTest { 26 public static void main( String[] args ) { 27 Gui gui = new Gui(); 28 gui.show(); 29 } 30 }
这个例子演示了如何给BUTTON设置click之后的响应函数
时间: 2024-10-26 03:04:25