Swing-jradiobutton

提供:Dev Guides
移動先:案内検索

SWING-JRadioButtonクラス

前書き

クラス JRadioButton は、ラジオボタンの実装です。選択または選択解除できるアイテムであり、ユーザーにその状態を表示します。

クラス宣言

以下は javax.swing.JRadioButton クラスの宣言です-

public class JRadioButton
   extends JToggleButton
      implements Accessible

クラスコンストラクター

Sr.No. Constructor & Description
1

JRadioButton()

テキストが設定されていない、最初は選択されていないラジオボタンを作成します。

2

JRadioButton(Action a)

指定されたアクションからプロパティが取得されるラジオボタンを作成します。

3

JRadioButton(Icon icon)

テキストを指定せずに、指定した画像で最初に選択されていないラジオボタンを作成します

4

JRadioButton(Icon icon, boolean selected)

指定された画像と選択状態でラジオボタンを作成しますが、テキストは作成しません。

5

JRadioButton(String text, boolean selected)

指定されたテキストと選択状態でラジオボタンを作成します。

6

JRadioButton(String text, Icon icon)

指定されたテキストと画像を持ち、最初は選択されていないラジオボタンを作成します。

7

JRadioButton(String text, Icon icon, boolean selected)

指定されたテキスト、画像、および選択状態を持つラジオボタンを作成します。

クラスメソッド

Sr.No. Method & Description
1

AccessibleContext getAccessibleContext()

このJRadioButtonに関連付けられているAccessibleContextを取得します。

2

String getUIClassID()

このコンポーネントをレンダリングするL&Fクラスの名前を返します。

3

protected String paramString()

このJRadioButtonの文字列表現を返します。

4

void updateUI()

UIプロパティを現在のルックアンドフィールの値にリセットします。

継承されるメソッド

このクラスは、次のクラスからメソッドを継承します-

  • javax.swing.AbstractButton
  • javax.swing.JToggleButton
  • javax.swing.JComponent
  • java.awt.Container
  • java.awt.Component
  • java.lang.Object

JRadioButtonの例

たとえば、 D:/> SWING> com> finddevguides> gui> の任意のエディターを使用して、次のJavaプログラムを作成します。

SwingControlDemo.java

package com.finddevguides.gui;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SwingControlDemo {
   private JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel;

   public SwingControlDemo(){
      prepareGUI();
   }
   public static void main(String[] args){
      SwingControlDemo  swingControlDemo = new SwingControlDemo();
      swingControlDemo.showRadioButtonDemo();
   }
   private void prepareGUI(){
      mainFrame = new JFrame("Java Swing Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));

      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }
      });
      headerLabel = new JLabel("", JLabel.CENTER);
      statusLabel = new JLabel("",JLabel.CENTER);
      statusLabel.setSize(350,100);

      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);
   }
   private void showRadioButtonDemo(){
      headerLabel.setText("Control in action: RadioButton");

      final JRadioButton radApple = new JRadioButton("Apple", true);
      final JRadioButton radMango = new JRadioButton("Mango");
      final JRadioButton radPeer = new JRadioButton("Peer");

      radApple.setMnemonic(KeyEvent.VK_C);
      radMango.setMnemonic(KeyEvent.VK_M);
      radPeer.setMnemonic(KeyEvent.VK_P);

      radApple.addItemListener(new ItemListener() {
         public void itemStateChanged(ItemEvent e) {
            statusLabel.setText("Apple RadioButton: "
               + (e.getStateChange()==1?"checked":"unchecked"));
         }
      });
      radMango.addItemListener(new ItemListener() {
         public void itemStateChanged(ItemEvent e) {
            statusLabel.setText("Mango RadioButton: "
               + (e.getStateChange()==1?"checked":"unchecked"));
         }
      });
      radPeer.addItemListener(new ItemListener() {
         public void itemStateChanged(ItemEvent e) {
            statusLabel.setText("Peer RadioButton: "
               + (e.getStateChange()==1?"checked":"unchecked"));
         }
      });

     //Group the radio buttons.
      ButtonGroup group = new ButtonGroup();

      group.add(radApple);
      group.add(radMango);
      group.add(radPeer);

      controlPanel.add(radApple);
      controlPanel.add(radMango);
      controlPanel.add(radPeer);

      mainFrame.setVisible(true);
   }
}

コマンドプロンプトを使用してプログラムをコンパイルします。 D:/> SWING に移動して、次のコマンドを入力します。

D:\SWING>javac com\finddevguides\gui\SwingControlDemo.java

エラーが発生しない場合、コンパイルが成功したことを意味します。 次のコマンドを使用してプログラムを実行します。

D:\SWING>java com.finddevguides.gui.SwingControlDemo

次の出力を確認します。

Swing JRadioButton