【從零開始學 Java 程式設計】JRadioButton 元件

【從零開始學 Java 程式設計】 線上教學課程目錄,使用 Java 程式語言,開發應用程式。

JRadioButton

JRadioButton 單選按鈕元件,可以提供很多選項,讓使用者單選

範例

學習如何使用 ButtonGroup 將三個 JRadioButton 變成一個單選群組,當使用者點選其中一個 JRadioButton 選項,下方 JLabel 元件,顯示剛選擇的項目名稱

關鍵程式碼

private JLabel jLabel;

public void addComponentsToPane(Container pane) {

    pane.setLayout(new BorderLayout());

    Box box = new Box(BoxLayout.Y_AXIS);

    ButtonGroup buttonGroup = new ButtonGroup();

    JRadioButton rbSpider = new JRadioButton("蜘蛛");
    rbSpider.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            updateChoice(rbSpider.getText());
        }
    });
    rbSpider.setAlignmentX(Component.CENTER_ALIGNMENT);
    buttonGroup.add(rbSpider);
    box.add(rbSpider);

    JRadioButton rbCockroach = new JRadioButton("蟑螂");
    rbCockroach.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            updateChoice(rbCockroach.getText());
        }
    });
    rbCockroach.setAlignmentX(Component.CENTER_ALIGNMENT);
    buttonGroup.add(rbCockroach);
    box.add(rbCockroach);

    JRadioButton rbfly = new JRadioButton("蒼蠅");
    rbfly.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            updateChoice(rbfly.getText());
        }
    });
    rbfly.setAlignmentX(Component.CENTER_ALIGNMENT);
    buttonGroup.add(rbfly);
    box.add(rbfly);

    jLabel = new JLabel("請選擇...");
    jLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
    jLabel.setBorder(BorderFactory.createEmptyBorder(30, 0, 0, 0));
    box.add(jLabel);
    box.setBorder(BorderFactory.createEmptyBorder(30, 0, 0, 0));
    pane.add(box, BorderLayout.CENTER);
}

private void updateChoice(String text) {
    jLabel.setText("你選擇了: " + text);
}

完整程式碼

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

public class DemoJRadioButton {
    private JLabel jLabel;

    public void addComponentsToPane(Container pane) {

        pane.setLayout(new BorderLayout());

        Box box = new Box(BoxLayout.Y_AXIS);

        ButtonGroup buttonGroup = new ButtonGroup();

        JRadioButton rbSpider = new JRadioButton("蜘蛛");
        rbSpider.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                updateChoice(rbSpider.getText());
            }
        });
        rbSpider.setAlignmentX(Component.CENTER_ALIGNMENT);
        buttonGroup.add(rbSpider);
        box.add(rbSpider);

        JRadioButton rbCockroach = new JRadioButton("蟑螂");
        rbCockroach.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                updateChoice(rbCockroach.getText());
            }
        });
        rbCockroach.setAlignmentX(Component.CENTER_ALIGNMENT);
        buttonGroup.add(rbCockroach);
        box.add(rbCockroach);

        JRadioButton rbfly = new JRadioButton("蒼蠅");
        rbfly.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                updateChoice(rbfly.getText());
            }
        });
        rbfly.setAlignmentX(Component.CENTER_ALIGNMENT);
        buttonGroup.add(rbfly);
        box.add(rbfly);

        jLabel = new JLabel("請選擇...");
        jLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
        jLabel.setBorder(BorderFactory.createEmptyBorder(30, 0, 0, 0));
        box.add(jLabel);
        box.setBorder(BorderFactory.createEmptyBorder(30, 0, 0, 0));
        pane.add(box, BorderLayout.CENTER);
    }

    private void updateChoice(String text) {
        jLabel.setText("你選擇了: " + text);
    }

    public DemoJRadioButton() {
        JFrame frame = new JFrame("HKT線上教室");
        // 獲取螢幕解析度
        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setSize(dimension.width / 4, dimension.height / 4);

        //設定視窗顯示在螢幕畫面中間位置
        int x = (int) ((dimension.getWidth() - frame.getWidth()) / 2);
        int y = (int) ((dimension.getHeight() - frame.getHeight()) / 2);
        frame.setLocation(x, y);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設定關閉可以關掉程式
        //在 Pane 畫面中加入元件
        addComponentsToPane(frame.getContentPane());
//        frame.pack();
        frame.setVisible(true);
    }

    //最一開始程式進入點
    public static void main(String[] args) {
        //使用 invokeLater 確保 UI 在排程執行緒內
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new DemoJRadioButton();
            }
        });
    }
}

參考資料


那這次的課程就介紹到這邊囉~
順帶一提,KT 線上教室,臉書粉絲團,會不定期發佈相關資訊,不想錯過最新資訊,不要忘記來按讚,加追蹤喔!也歡迎大家將這套課程分享給更多人喔。
我們下次再見囉!!!掰掰~

這個網誌中的熱門文章

16天記下7000單字

2023 最新入門零基礎 Kotlin教學【從零開始學 Kotlin 程式設計】Kotlin 教學課程目錄 (Android Kotlin, IntelliJ IDEA, Android Studio, Android APP 開發教學)

2022 最新入門零基礎 Flutter教學 【Flutter 程式設計入門實戰 30 天】Flutter 教學課程目錄 (IntelliJ IDEA 開發教學)

nano 文字編輯器

【從零開始學 Flutter 程式設計】SharedPreferences 設定檔資料存取