簡體   English   中英

給出jRadioButton選項的jButton的ActionEvent

[英]ActionEvent for jButton given a jRadioButton option

所以基本上,我正在嘗試用GUI創建一個簡單的程序,讓你在兩個jRadioButtons作為選擇時做出選擇,其中兩個中的任何一個都以與視覺小說類似的方式導致相應的故事情節。 然而問題是我無法將jButton的想法與jRadioButton的選擇聯系起來。

這個概念是這樣的:在一個框架中,有一個jTextArea顯示形成故事情節的文本字符串,它提供了兩個選項可供選擇,由兩個jRadioButtons表示,然后必須由jButton執行。

到目前為止,我在JLadioButton的ActionListener中放置的唯一邏輯是,一旦點擊jRadioButton而jButton故意為空,就必須禁用另一個邏輯。 任何人都有任何類似的程序或模塊他/她想分享,至少,這個邏輯在程序上說?

我們通常使用RadioButtons在幾個選項中選擇一個,並且一次只能選擇一個按鈕。 要獲得此功能,您需要將按鈕添加到javax.swing.ButtonGroup 這是一個簡單的例子:

 //Fields declared in your main GUI class
 JRadioButton option1,option2;
 JButton goButton; //executes the "Story"

 //Constructor code or place in init() method that constructor calls:
 {
    //initialize buttons and place them in your frame.
    ButtonGroup buttonGroup = new javax.swing.ButtonGroup();
    buttonGroup.add(option1);
    buttonGroup.add(option2);
    buttonGroup1.setSelected(option1.getModel(), true);
    goButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            goButtonActionPerformed(evt);
        }
    });

}

private void goButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
     if(option1.isSelected()) {                
         //place text in your text area
     }
     if(option2.isSelected()) {                
         //place different text in your text area
     }
}                                            

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM