lightbulbcontrols.java

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

JAVA
74
字号
//********************************************************************
//  LightBulbControls.java       Author: Lewis/Loftus
//
//  Represents the control panel for the LightBulb program.
//********************************************************************

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class LightBulbControls extends JPanel
{
   private LightBulbPanel bulb;
   private JButton onButton, offButton;

   //-----------------------------------------------------------------
   //  Sets up the lightbulb control panel.
   //-----------------------------------------------------------------
   public LightBulbControls (LightBulbPanel bulbPanel)
   {
      bulb = bulbPanel;

      onButton = new JButton ("On");
      onButton.setEnabled (false);
      onButton.setMnemonic ('n');
      onButton.setToolTipText ("Turn it on!");
      onButton.addActionListener (new OnListener());

      offButton = new JButton ("Off");
      offButton.setEnabled (true);
      offButton.setMnemonic ('f');
      offButton.setToolTipText ("Turn it off!");
      offButton.addActionListener (new OffListener());

      setBackground (Color.black);
      add (onButton);
      add (offButton);
   }

   //*****************************************************************
   //  Represents the listener for the On button.
   //*****************************************************************
   private class OnListener implements ActionListener
   {
      //--------------------------------------------------------------
      //  Turns the bulb on and repaints the bulb panel.
      //--------------------------------------------------------------
      public void actionPerformed (ActionEvent event)
      {
         bulb.setOn (true);
         onButton.setEnabled (false);
         offButton.setEnabled (true);
         bulb.repaint();
      }
   }

   //*****************************************************************
   //  Represents the listener for the Off button.
   //*****************************************************************
   private class OffListener implements ActionListener
   {
      //--------------------------------------------------------------
      //  Turns the bulb off and repaints the bulb panel.
      //--------------------------------------------------------------
      public void actionPerformed (ActionEvent event)
      {
         bulb.setOn (false);
         onButton.setEnabled (true);
         offButton.setEnabled (false);
         bulb.repaint();
      }
   }
}

⌨️ 快捷键说明

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