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

📄 buttonsandboxes.java

📁 程序练习中包括书中实例程序代码和练习中要用到的代码,是压缩文件
💻 JAVA
字号:
package examples.windows;
import javax.swing.*;
import java.awt.*;
import java.util.Enumeration;
/** An example class used to demonstrate the use of 
  * the JToggleButton, JCheckBox, and JRadioButton
  * components
  */
public class ButtonsAndBoxes extends JFrame {
   /** Class constructor method
     * @param titleText Window's title bar text
     */
   public ButtonsAndBoxes( String titleText ) {
      super( titleText );
      addWindowListener( new WindowCloser() );

      JPanel left = new JPanel( new GridLayout( 0, 1 ) );
      left.setBorder(
         BorderFactory.createTitledBorder(
            "Button group" ) );
      ButtonGroup bg = new ButtonGroup();
      bg.add( new JRadioButton( "ribeye" ) );
      bg.add( new JRadioButton( "filet mignon" ) );
      bg.add( new JRadioButton( "T-bone" ) );
      Enumeration e = bg.getElements();
      while( e.hasMoreElements() ) {
         JRadioButton rb = (JRadioButton) e.nextElement();
         rb.setIcon( new ImageIcon( "bulb1.gif" ) );
         rb.setSelectedIcon(
            new ImageIcon( "bulb2.gif" ) );
         left.add( rb );
      }

      JPanel right = new JPanel( new GridLayout( 0, 1 ) );
      right.setBorder(
         BorderFactory.createTitledBorder(
            "Independent check boxes" ) );
      right.add( new JCheckBox( "cake" ) );
      right.add( new JCheckBox( "pie" ) );
      right.add( new JCheckBox( "soft drink" ) );
      right.add( new JCheckBox( "fries" ) );

      JPanel bottom = new JPanel();
      bottom.setBorder(
         BorderFactory.createTitledBorder(
            "Toggle buttons" ) );
      bottom.add( new JToggleButton(
         "burger", new ImageIcon( "burger.gif" ) ) );
      bottom.add( new JToggleButton(
         "hot dog", new ImageIcon( "hotdog.gif" ) ) );
      bottom.add( new JToggleButton(
         "pizza", new ImageIcon( "pizza.gif" ) ) );

      Container cp = getContentPane();
      cp.setLayout( new GridBagLayout() );
      GridBagConstraints c = new GridBagConstraints();
      c.fill = GridBagConstraints.BOTH;
      c.weightx = 1.0;
      c.weighty = 1.0;
      cp.add( left, c );
      c.gridwidth = GridBagConstraints.REMAINDER;
      cp.add( right, c );
      cp.add( bottom, c );
      pack();
      setVisible( true );
   }

   /** The test method for the class
     * @param args not used
     */
   public static void main( String[] args ) {
      new ButtonsAndBoxes(
         "Example Buttons and Check Boxes" );
   }
}

⌨️ 快捷键说明

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