?? chap12-3.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 + -