⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 threepanels.java

📁 程序练习中包括书中实例程序代码和练习中要用到的代码,是压缩文件
💻 JAVA
字号:
package examples.windows;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/** An example class used to demonstrate the basics of 
  * creating components such as panels, arranging
  * components using layout objects, and nesting
  * components inside each other.
  */
public class ThreePanels extends JFrame {
   private JPanel upper, middle, lower;
   private JTextField text;
   private JButton black, red, green;
   private JCheckBox disable;
   /** Class constructor method
     * @param titleText Window's title bar text
     */
   public ThreePanels( String titleText ) {
      super( titleText );
      addWindowListener( new WindowAdapter() {
            /** End the program when the user
              * closes the window
              */
            public void
            windowClosing( WindowEvent e ) {
               ThreePanels.this.dispose();
               System.exit( 0 );
            }

         }
      );
      upper = new JPanel();
      upper.setBorder(
         BorderFactory.createTitledBorder(
         "Sample text" ) );
      upper.setLayout( new BorderLayout() );
      text = new JTextField( 
         "Change the color of this text" );
      upper.add( text, BorderLayout.CENTER );
      middle = new JPanel();
      middle.setBorder(
         BorderFactory.createTitledBorder(
         "Text color control" ) );
      middle.setLayout( new FlowLayout( 
         FlowLayout.CENTER ) );
      black = new JButton( "Black",
                  new ColorIcon( Color.black ) );
      black.addActionListener(
         new ButtonListener( Color.black ) );
      middle.add( black );
      red = new JButton( "Red",
                  new ColorIcon( Color.red ) );
      red.addActionListener(
         new ButtonListener( Color.red ) );
      middle.add( red );
      green = new JButton( "Green",
                  new ColorIcon( Color.green ) );
      green.addActionListener(
         new ButtonListener( Color.green ) );
      middle.add( green );
      lower = new JPanel();
      lower.setLayout( new FlowLayout( 
         FlowLayout.RIGHT ) );
      disable = new JCheckBox( "Disable changes" );
      disable.addItemListener( new ItemListener() {
            /** Disable and enable the buttons
              */
            public void
            itemStateChanged( ItemEvent e ) {
               boolean enabled
                  = ( e.getStateChange()
                      == ItemEvent.DESELECTED );
               black.setEnabled( enabled );
               red.setEnabled( enabled );
               green.setEnabled( enabled );
            }
         }
      );
      lower.add( disable );
      Container cp = getContentPane();
      cp.add( upper, BorderLayout.NORTH );
      cp.add( middle, BorderLayout.CENTER );
      cp.add( lower, BorderLayout.SOUTH );
      pack();
      setVisible( true );
   }
   /** The class representing the button event
     * listeners
     */
   class ButtonListener implements ActionListener {
      private Color c;
      /** Class constructor
        * @param c the color for this button
        */
      public ButtonListener( Color c ) {
         this.c = c;
      }
      /** Respond to the action events
        * @param e The click event
        */
      public void actionPerformed( ActionEvent e ) {
         text.setForeground( c );
      }
   }
   /** The class representing the colored icons on
     * the buttons
     */
   class ColorIcon implements Icon {
      private Color c;
      private static final int DIAMETER = 10;
      /** Class constructor
        * @param c the color for this button
        */
      public ColorIcon( Color c ) {
         this.c = c;
      }
      /** Paint the color icon with a black border
        * @param cp the component holding the icon
        * @param g the graphics context for the icon
        * @param x the x draw start position
        * @param y the y draw start position
        */
      public void paintIcon( Component cp, Graphics g,
                             int x, int y ) {
         g.setColor( c );
         g.fillOval( x, y, DIAMETER, DIAMETER );
         g.setColor( Color.black );
         g.drawOval( x, y, DIAMETER, DIAMETER );
      }
      /** Get the icon's height
        * @return the height of the icon
        */
      public int getIconHeight() {
         return DIAMETER;
      }
      /** Get the icon's width
        * @return the width of the icon
        */
      public int getIconWidth() {
         return DIAMETER;
      }
   }
   /** The test method for the class
     * @param args not used
     */
   public static void main( String[] args ) {
      new ThreePanels( "Three Panels Sample" );
   }
}

⌨️ 快捷键说明

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