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

📄 checkboxone.java

📁 《A first book of java》by Gary J.Bronson 北大出版社
💻 JAVA
字号:
import java.text.*;  // need this for formatting
import javax.swing.*;
import java.awt.event.*;
import java.awt.Container;  // need this to add controls
import java.awt.*;  // need this for layout manager 
public class CheckBoxOne extends JFrame
{
  private JFrame mainFrame;
  private JButton exitButton;
  private JLabel inLabel;
  private JTextField tinField;
  private JCheckBox boxItalic;
  private JCheckBox boxBold;
  
  public CheckBoxOne() // a constructor
  {
    mainFrame = new JFrame("Check Box Example");

      // create all components
    
    exitButton = new JButton("Exit");
    inLabel = new JLabel("Enter Some Text:");
    tinField = new JTextField(20);
    boxItalic = new JCheckBox("Italic");
    boxBold = new JCheckBox("Bold");
       
      // get the content pane 
    Container c = mainFrame.getContentPane(); 
      // set the layout managare
    c.setLayout(new FlowLayout());
    
      // add the components to the ContentPane 
    c.add(inLabel);
    c.add(tinField);
    c.add(boxItalic);
    c.add(boxBold);
    c.add(exitButton);
	
    
      // create accelerator keys
    boxItalic.setMnemonic('I');
    boxBold.setMnemonic('B');
    exitButton.setMnemonic('x');


    mainFrame.setSize(250,150);

       // define and register window event handler
    mainFrame.addWindowListener(new WindowAdapter()
    {
      public void windowClosing(WindowEvent e) {System.exit(0);}
    });
    
      // create and register the button event handlers
 
    ExitButtonHandler ehandler = new ExitButtonHandler();  // instantiate a handler
    exitButton.addActionListener(ehandler);  // register the handler    

    ChkBoxHandler chandler = new ChkBoxHandler();
    boxItalic.addItemListener(chandler);
    boxBold.addItemListener(chandler);
        
    mainFrame.show();
  } 
    
     // inner classes for the Check Box handlers
   class ChkBoxHandler implements ItemListener
   {
     private int italicFont;
     private int boldFont;

     public void itemStateChanged(ItemEvent e)
     {
      
       if (e.getSource() == boxBold)
         if (e.getStateChange() == e.SELECTED)
           boldFont = Font.BOLD;
         else
           boldFont = Font.PLAIN;
       else
         if (e.getStateChange() == e.SELECTED)
           italicFont = Font.ITALIC;
         else
           italicFont = Font.PLAIN;

       tinField.setFont( new Font( "Courier", italicFont + boldFont, 14));
      
       tinField.repaint();   
     }
   }  // end of inner class         
 
     // inner class for the Exit button event handler
  private class ExitButtonHandler implements ActionListener
  {
    public void actionPerformed(ActionEvent e)
    {
      System.exit(0);
    }
  }  // end of inner class


  public static void main(String args[])
  {
    new CheckBoxOne();  // instantiate a GUI object 
  }

}  // end of class

⌨️ 快捷键说明

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