最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java 入門圖形用戶界面設計之單選按鈕

 更新時間:2022年02月16日 15:18:02   作者:小旺不正經(jīng)  
圖形界面(簡稱GUI)是指采用圖形方式顯示的計算機操作用戶界面。與早期計算機使用的命令行界面相比,圖形界面對于用戶來說在視覺上更易于接受,本篇精講Java語言中關(guān)于圖形用戶界面的單選按鈕

Java程序設計 圖形用戶界面 【九】單選按鈕

單選按鈕 JRadioButton

JRadioButton類

方法 作用
public JRadioButton(Icon icon) 建立一個單選按鈕,并指定圖片
public JRadioButton(Icon icon,boolean selected) 建立一個單選按鈕,并指定圖片和其是否選定
public JRadioButton(String text) 建立一個單選按鈕,并指定其文字,默認不選定
public JRadioButton(String text,boolean selected) 建立一個單選按鈕,并指定文字和是否選定
public JRadioButton(String text,Icon icon,boolean selected) 建立一個單選按鈕,并指定圖片、文字和其是否選定
public void setSelected(boolean b) 設置是否選中
public boolean isSelected() 返回是否被選中
public void setText(String text) 設置顯示文本
public void setIcon(Icon defaultIcon) 設置圖片
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

class MyRadio{
    private JFrame frame = new JFrame("一");
    private Container cont = frame.getContentPane();
    private JRadioButton jrb1 = new JRadioButton("1");
    private JRadioButton jrb2 = new JRadioButton("2");
    private JRadioButton jrb3 = new JRadioButton("3");
    private JPanel pan = new JPanel();
    public MyRadio(){
        pan.setBorder(BorderFactory.createTitledBorder("請選擇"));
        pan.setLayout(new GridLayout(1,3));
        pan.add(this.jrb1);
        pan.add(this.jrb2);
        pan.add(this.jrb3);
        ButtonGroup group = new ButtonGroup();
        group.add(this.jrb1);
        group.add(this.jrb2);
        group.add(this.jrb3);
        cont.add(pan);
        this.frame.setSize(330,80);
        this.frame.setVisible(true);
        this.frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(1);
            }
        });
    }
}
public class Hello {
    public static void main(String[] args) {
        new MyRadio();
    }
}

image-20220213140637190

ButtonGroup group = new ButtonGroup();
group.add(this.jrb1);
group.add(this.jrb2);
group.add(this.jrb3);

將按鈕添加到同一個組中實現(xiàn)單選功能

JRadioButton事件處理

使用ItemListener接口進行事件的監(jiān)聽

方法 作用
void itemStateChanged(ItemEvent e) 當用戶取消或選定某個選項時調(diào)用

ItemEvent類

方法&常量 類型 作用
public static final int SELECTED 常量 選項被選中
public static final int DESELECTED 常量 選項未被選中
public Object getItem() 方法 返回受事件影響的選項
public int getStateChange() 方法 返回選定狀態(tài)的類型(已選擇或已取消)
import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

class MyRadio implements ItemListener{
    private JLabel a = new JLabel("選中");
    private JLabel b = new JLabel("未選中");
    private JFrame frame = new JFrame("一");
    private Container cont = frame.getContentPane();
    private JRadioButton jrb1 = new JRadioButton("A",true);
    private JRadioButton jrb2 = new JRadioButton("B",true);
    private JPanel pan = new JPanel();
    public MyRadio(){
        ButtonGroup group = new ButtonGroup();
        group.add(this.jrb1);
        group.add(this.jrb2);
        jrb1.addItemListener(this);
        jrb2.addItemListener(this);
        pan.setLayout(new GridLayout(1,4));
        pan.add(this.a);
        pan.add(this.jrb1);
        pan.add(this.b);
        pan.add(this.jrb2);
        this.frame.add(pan);
        this.frame.setSize(200,100);
        this.frame.setVisible(true);
        this.frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(1);
            }
        });
    }
    @Override
    public void itemStateChanged(ItemEvent e) {
        if(e.getSource()==jrb2){
            a.setText("未選中");
            b.setText("選中");
        }else {
            b.setText("未選中");
            a.setText("選中");
        }
    }
}
public class Hello {
    public static void main(String[] args) {
        new MyRadio();
    }
}

image-20220213143404542

image-20220213143420956

到此這篇關(guān)于Java 入門圖形用戶界面設計之單選按鈕的文章就介紹到這了,更多相關(guān)Java 單選按鈕內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

绿春县| 新乡县| 巴彦淖尔市| 桐庐县| 余庆县| 泾阳县| 灌南县| 太湖县| 泗洪县| 嵊泗县| 穆棱市| 额尔古纳市| 剑河县| 毕节市| 黄石市| 亳州市| 中阳县| 都安| 清苑县| 台中市| 佛学| 革吉县| 蓬安县| 大理市| 大新县| 高邮市| 佛坪县| 黔西县| 安徽省| 和田县| 周口市| 通河县| 工布江达县| 乐都县| 茶陵县| 伊金霍洛旗| 子长县| 延寿县| 深圳市| 双江| 阳谷县|