24h購物| | PChome| 登入
2008-03-04 01:59:36| 人氣136| 回應0 | 上一篇 | 下一篇

NOTE 02

推薦 0 收藏 0 轉貼0 訂閱站台

import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;

public class RadioButtonFrame extends JFrame
{
private JTextField textField;
private Font plainFont;
private Font boldFont;
private Font italicFont;
private Font boldItalicFont;
private JRadioButton plainJRadioButton;
private JRadioButton boldJRadioButton;
private JRadioButton italicJRadioButton;
private JRadioButton boldItalicJRadioButton;
private ButtonGroup radioGroup;

public RadioButtonFrame()
{
super( "RadioButton Test" );
setLayout( new FlowLayout() );

textField = new JTextField( "Watch the font style change" , 25);
add( textField);

plainJRadioButton = new JRadioButton( "Plain" , true );
boldJRadioButton = new JRadioButton( "Bold" , false);
italicJRadioButton = new JRadioButton( "Italic" , false );
boldItalicJRadioButton = new JRadioButton( "Bold/Italic" , false);
add( plainJRadioButton );
add( boldJRadioButton );
add( italicJRadioButton );
add( boldItalicJRadioButton );

radioGroup = new ButtonGroup();
radioGroup.add( plainJRadioButton );
radioGroup.add( boldJRadioButton );
radioGroup.add( italicJRadioButton );
radioGroup.add( boldItalicJRadioButton );

plainFont = new Font( "Serif" , Font.PLAIN , 14 );
boldFont = new Font( "Serif" , Font.BOLD , 14 );
italicFont = new Font ( "Serif" , Font.ITALIC , 14 );
boldItalicFont = new Font( "Serif" , Font.BOLD + Font.ITALIC , 14 );
textField.setFont( plainFont );

plainJRadioButton.addItemListener(
new RadioButtonHandler( plainFont) );
boldJRadioButton.addItemListener(
new RadioButtonHandler( boldFont) );
italicJRadioButton.addItemListener(
new RadioButtonHandler( italicFont) );
boldItalicJRadioButton.addItemListener(
new RadioButtonHandler( boldItalicFont) );
}

private class RadioButtonHandler implements ItemListener
{
private Font font;

public RadioButtonHandler( Font f )
{
font = f;
}

public void itemStateChanged( ItemEvent event )
{
textField.setFont( font );
}
}
}

------

import javax.swing.JFrame;

public class RadioButtonTest
{
public static void main( String args[] )
{
RadioButtonFrame radioButtonFrame = new RadioButtonFrame();
radioButtonFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
radioButtonFrame.setSize( 300 , 100 );
radioButtonFrame.setVisible( true );
}
}

台長: 娃娃♡°

您可能對以下文章有興趣

人氣(136) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 數位資訊(科技、網路、通訊、家電) | 個人分類: 程式設計⊙ˇ⊙ |
此分類下一篇:note 03
此分類上一篇:NOTE 01

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文