pushcounterpanel.java

来自「Java 程序设计教程(第五版)EXAMPLESchap04源码」· Java 代码 · 共 51 行

JAVA
51
字号
//********************************************************************//  PushCounterPanel.java       Authors: Lewis/Loftus////  Demonstrates a graphical user interface and an event listener.//********************************************************************import java.awt.*;import java.awt.event.*;import javax.swing.*;public class PushCounterPanel extends JPanel{   private int count;   private JButton push;   private JLabel label;   //-----------------------------------------------------------------   //  Constructor: Sets up the GUI.   //-----------------------------------------------------------------   public PushCounterPanel ()   {      count = 0;      push = new JButton ("Push Me!");      push.addActionListener (new ButtonListener());      label = new JLabel ("Pushes: " + count);      add (push);      add (label);      setPreferredSize (new Dimension(300, 40));      setBackground (Color.cyan);   }   //*****************************************************************   //  Represents a listener for button push (action) events.   //*****************************************************************   private class ButtonListener implements ActionListener   {      //--------------------------------------------------------------      //  Updates the counter and label when the button is pushed.      //--------------------------------------------------------------      public void actionPerformed (ActionEvent event)      {         count++;         label.setText("Pushes: " + count);      }   }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?