第一部份:
(檔名存為:RadioButtonFrame.java)

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("班代:花小麟", 25 );
      add( textField );

      plainJRadioButton = new JRadioButton("正常", true );
      boldJRadioButton = new JRadioButton("粗體", false );
      italicJRadioButton = new JRadioButton("斜體", false );
      boldItalicJRadioButton = new JRadioButton( "粗斜體", 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 );

      }

   }

}

---------------------------------------------------------------------------------------
第二部份
(檔名存為:RadioButtonTest.java)

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 );

   }

}

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 a25561970 的頭像
    a25561970

    萬花筒

    a25561970 發表在 痞客邦 留言(0) 人氣()