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

📄 jradiobuttondemo.java

📁 精通Java核心技术源代码
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class JRadioButtonDemo extends JFrame 
{
  	   static String plain = "Plain";
      static String bold = "Bold";
      static String italic = "Italic";
      static String boldItalic = "Bold/Italic";
    
      JTextField textField;
      JRadioButton plainButton,boldButton,italicButton,boldItalicButton;

      public JRadioButtonDemo() 
      {
         super( "JRadioButtonDemo" );
		
         // 获取content pane并设置布局管理器
         Container container = getContentPane();
         container.setLayout( new FlowLayout() );
		
          // 创建文本框 
  	       textField = new JTextField( "Welcome to Java World!",22 );
          container.add( textField );
		
          // 创建 radio buttons
          plainButton = new JRadioButton( plain );
          plainButton.setSelected(true);
          container.add( plainButton );

          boldButton = new JRadioButton( bold );
          container.add( boldButton );
        
          italicButton = new JRadioButton( italic );
          container.add( italicButton );
        
          boldItalicButton = new JRadioButton( boldItalic );
          container.add( boldItalicButton );
        
          // 为radio buttons注册事件处理器
          EventHandler handler = new EventHandler();
          plainButton.addItemListener( handler );
          boldButton.addItemListener( handler );
          italicButton.addItemListener( handler );
          boldItalicButton.addItemListener( handler );
        
          // 创建按钮组
          ButtonGroup radioButtonGroup = new ButtonGroup();
          radioButtonGroup.add( plainButton );
          radioButtonGroup.add( boldButton );
          radioButtonGroup.add( italicButton );
          radioButtonGroup.add( boldItalicButton );
                
          setSize( 280,100 );
          setVisible( true );
      }
    
      // 用于事件处理的内部类
      private class EventHandler implements ItemListener
      {
         // 处理radioButton 事件
         public void itemStateChanged( ItemEvent event )
         {
             // 判断plainButton 是否被选中
             if ( event.getSource() == plainButton ) 
    	   		textField.setFont( new Font( "Serif",Font.PLAIN,14 ) );
    		   
             // 判断boldButton是否被选中
             else if ( event.getSource() == boldButton ) 
    	      	textField.setFont( new Font( "Serif",Font.BOLD,14 ) );
    		   
             // 判断italicButton是否被选中
             else if ( event.getSource() == italicButton ) 
    	   		textField.setFont( new Font( "Serif",Font.ITALIC,14 ) );
    		   
             // 判断boldItalicButton是否被选中
             else if ( event.getSource() == boldItalicButton ) 
        		textField.setFont( new Font( "Serif",Font.BOLD + Font.ITALIC,14 ) ); 
          }
      }

      public static void main(String s[]) 
      {
           JRadioButtonDemo radioButtonDemo = new JRadioButtonDemo();
           radioButtonDemo.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      }
  }

⌨️ 快捷键说明

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