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

📄 chap12-3.txt

📁 JAVA 学习资源
💻 TXT
字号:
// 程序12-3
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class testEventBoxRadio {
    JPanel   jp1,jp2;         // 定义两个面板
    JFrame  frame;
    Container  contentPane;
    JTextField  field1,field2;              // 两个文本框
    JCheckBox backGroundBox,foreGroundBox;       // 两个复选框
    JRadioButton backGroundRadio,foreGroundRadio; // 两个单选按钮
    ButtonGroup  radioGroup;      // 单选按钮组
    
    // 定义一个处理JCheckBox事件的内隐类,它实现了ItemListener接口
    private class BoxEventHandler implements ItemListener{
        private Color defaultBackGround,defaultForeGround;	// 两个颜色变量
        
	// 实现itemStateChanged( )方法
        public void itemStateChanged(ItemEvent e) { 
            if(e.getSource( )==backGroundBox)
                if(e.getStateChange( )==ItemEvent.SELECTED)
                    defaultBackGround=Color.blue;
                else
                    defaultBackGround=Color.white; 
            
            if(e.getSource( )==foreGroundBox)
                if(e.getStateChange( )==ItemEvent.SELECTED)
                    defaultForeGround=Color.YELLOW;
                else
                    defaultForeGround=Color.BLACK; 

                // 设置field1文本框的前景色和背景色
            field1.setForeground(defaultForeGround); 	
            field1.setBackground(defaultBackGround); 
        }        
    }
    
 	  // 定义一个处理JRadioButton事件的内隐类,它实现了ItemListener接口
    private class RadioEventHandler implements ItemListener{
        private Color defaultBackGround,defaultForeGround;	// 两个颜色变量
        
            // 实现itemStateChanged( )方法
        public void itemStateChanged(ItemEvent e) { 
            if(e.getSource( )==backGroundRadio)
                    defaultBackGround=Color.BLUE;        
            else
                defaultBackGround=Color.white;
        
            if(e.getSource( )==foreGroundRadio)
                defaultForeGround=Color.BLACK;
            else
                defaultForeGround=Color.YELLOW;
        
		// 设置field2文本框的前景和背景
            field2.setForeground(defaultForeGround); 	
            field2.setBackground(defaultBackGround); 
        }        
    }    
    
    public testEventBoxRadio( ){    // 构造函数
        frame=new subJFrame("testEventBoxRadio"); // 定义一个框架
        contentPane=frame.getContentPane( );	// 获取框架的内容格
        contentPane.setLayout(new FlowLayout( ));
    
        jp1=new JPanel( );       // 定义一个面板
        field1=new JTextField("field1: Watch the foreground and background change");
        
        jp1.add(field1);
        backGroundBox=new JCheckBox(" backGround ");
        foreGroundBox=new JCheckBox(" foreGround ");
        contentPane.add(field1);
        contentPane.add(backGroundBox);
        contentPane.add(foreGroundBox);    
       
        jp2=new JPanel( );   // 定义一个面板
        field2=new JTextField("feild2: Watch the foreground and background change");

        backGroundRadio=new JRadioButton(" backGround ");
        foreGroundRadio=new JRadioButton(" foreGround ");
        contentPane.add(field2);
        contentPane.add(backGroundRadio);
        contentPane.add(foreGroundRadio); 
        
        radioGroup=new ButtonGroup( );
        radioGroup.add(backGroundRadio);
        radioGroup.add(foreGroundRadio);       // 将两个radio按钮构成一个组
        
            // 将面板jp1放在内容格的北边,jp2放在南边
        contentPane.add(jp1,BorderLayout.NORTH); 	
        contentPane.add(jp2,BorderLayout.SOUTH); 
    
       	// 生成一个监听者,并将其注册为2个JCheckBox对象的监听者
        BoxEventHandler Boxhandler=new BoxEventHandler( );
        backGroundBox.addItemListener(Boxhandler);
        foreGroundBox.addItemListener(Boxhandler);
        
            // 生成一个监听者,并注册为JRadioButton对象的监听者
        RadioEventHandler Radiohandler=new RadioEventHandler( );
        backGroundRadio.addItemListener(Radiohandler);
        foreGroundRadio.addItemListener(Radiohandler);

        frame.setSize(400,200);
        frame.show( );
    } 
    
    public static void main(String args[ ]){
        testEventBoxRadio  obj=new testEventBoxRadio( );
    }
}

⌨️ 快捷键说明

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