2014-09-21 12 views
0

我正在對我的HOMEWORK(請不要爲我做我的工作)。我已經有95%完成了。儘管如此,我仍然遇到了麻煩。我需要在JTextArea中顯示選定的性別。我必須使用JRadioButton進行性別選擇。顯示文本從選定的JRadioButton到JTextArea

據我所知,JRadioButtons的工作方式不同。我設置了動作監聽器和助記符。我想我在這裏搞亂了。似乎我可能需要使用整個團隊來設置和行動lister。

任何幫助,非常感謝。

這裏是我有我的代碼(即我不認爲需要這樣其他人不能複製粘貼功課減去部分):

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


public class LuisRamosHW3 extends JFrame { 

private JLabel WelcomeMessage; 

private JRadioButton jrbMale = new JRadioButton("Male"); 

private JRadioButton jrbFemale = new JRadioButton("Female"); 

public LuisRamosHW3(){ 

setLayout(new FlowLayout(FlowLayout.LEFT, 20, 30)); 


JPanel jpRadioButtons = new JPanel(); 
jpRadioButtons.setLayout(new GridLayout(2,1)); 
jpRadioButtons.add(jrbMale); 
jpRadioButtons.add(jrbFemale); 
add(jpRadioButtons, BorderLayout.AFTER_LAST_LINE); 


ButtonGroup gender = new ButtonGroup(); 
gender.add(jrbMale); 
jrbMale.setMnemonic(KeyEvent.VK_B); 
jrbMale.setActionCommand("Male"); 
gender.add(jrbFemale); 
jrbFemale.setMnemonic(KeyEvent.VK_B); 
jrbFemale.setActionCommand("Female"); 

//Set defaulted selection to "male" 
jrbMale.setSelected(true); 

//Create Welcome button 
JButton Welcome = new JButton("Submit"); 
add(Welcome); 

Welcome.addActionListener(new SubmitListener()); 
WelcomeMessage = (new JLabel(" ")); 
add(WelcomeMessage); 


} 
class SubmitListener implements ActionListener{ 
@Override 
public void actionPerformed(ActionEvent e){ 

String FirstName = jtfFirstName.getText(); 
String FamName = jtfLastName.getText(); 
Object StateBirth = jcbStates.getSelectedItem(); 
String Gender = gender.getActionCommand(); /*I have tried different 
variations the best I do is get one selection to print to the text area*/ 

WelcomeMessage.setText("Welcome, " + FirstName + " " + FamName + " a " 
+ gender.getActionCommmand + " born in " + StateBirth); 
} 
} 
} /*Same thing with the printing, I have tried different combinations and just can't seem to figure it out*/ 
+0

不知道,但我想應該是這樣_getText JRadioButton類的()_或類似的方法。您可能需要使用JRadioButton的動作偵聽器,如_onSelectListener_。最後用提取的字符串設置JTextField。根據你的要求,我只告訴你該做什麼和怎麼做,而不是完全說出正確的東西(其實我也不知道應該做什麼)。這應該是正確的流程。祝你好運 – Nabin 2014-09-21 16:54:41

+1

@Nabin:不,不要在JRadioButton上使用ActionListener,因爲重要的選擇時間是按下提交按鈕的時間。原始的海報有它的權利 - 使用ButtonGroup對象,但他首先需要使其可見,給它的類作用域。 – 2014-09-21 17:06:20

回答

2

建議:

  • 讓你ButtonGroup變量,性別,一個字段(在類中聲明),並且不會在構造函數中聲明它,這會將其範圍限制在構造函數中。它必須在整個班級中可見。
  • 確保你給你的JRadioButton的actionCommands。 JRadioButtons不像JButton那樣,如果你將一個String傳遞給它的構造函數,這不會自動設置JRadioButton的文本,所以你必須在代碼中自己做這件事。
  • 您必須先通過getSelection()
  • 從ButtonGroup對象中獲取所選的ButtonModel然後在獲取其actionCommand文本之前檢查選定的模型是否爲空。

例如

import java.awt.BorderLayout; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.KeyEvent; 

import javax.swing.*; 

public class RadioButtonEg extends JPanel { 
    public static final String[] RADIO_TEXTS = {"Mon", "Tues", "Wed", "Thurs", "Fri"}; 

    // *** again declare this in the class. 
    private ButtonGroup buttonGroup = new ButtonGroup(); 
    private JTextField textfield = new JTextField(20); 

    public RadioButtonEg() { 
     textfield.setFocusable(false); 
     JPanel radioButtonPanel = new JPanel(new GridLayout(0, 1)); 
     for (String radioText : RADIO_TEXTS) { 
     JRadioButton radioButton = new JRadioButton(radioText); 
     radioButton.setActionCommand(radioText); // **** don't forget this 
     buttonGroup.add(radioButton); 
     radioButtonPanel.add(radioButton); 
     } 

     JPanel bottomPanel = new JPanel(); 
     bottomPanel.add(new JButton(new ButtonAction("Press Me", KeyEvent.VK_P))); 

     setLayout(new BorderLayout()); 
     add(radioButtonPanel, BorderLayout.CENTER); 
     add(bottomPanel, BorderLayout.PAGE_END); 
     add(textfield, BorderLayout.PAGE_START); 
    } 

    private class ButtonAction extends AbstractAction { 
     public ButtonAction(String name, int mnemonic) { 
     super(name); 
     putValue(MNEMONIC_KEY, mnemonic); 
     } 

     @Override 
     public void actionPerformed(ActionEvent e) { 
     ButtonModel model = buttonGroup.getSelection(); 
     if (model == null) { 
      textfield.setText("No radio button has been selected"); 
     } else { 
      textfield.setText("Selection: " + model.getActionCommand()); 
     } 

     } 
    } 

    private static void createAndShowGui() { 
     RadioButtonEg mainPanel = new RadioButtonEg(); 

     JFrame frame = new JFrame("RadioButtonEg"); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     frame.getContentPane().add(mainPanel); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGui(); 
     } 
     }); 
    } 
} 
+0

非常感謝。這應該讓我朝着正確的方向前進。 – 2014-09-21 17:07:55

+0

@LuisRamos:不客氣,祝你好運! – 2014-09-21 17:08:17

3

我已經做了類似樣的問題上JTable我們得到來自無線電組的選擇,然後採取相應的行動。想到分享解決方案。

在這裏,我已經使用動作監聽器對單選按鈕進行了分組,每個單選按鈕都會有一個與之關聯的動作命令。當用戶點擊一個單選按鈕時,隨後會觸發一個動作,在我們取消選擇其他單選按鈕選擇並使用新選擇刷新文本區域時生成一個事件。

enter image description here

import java.awt.BorderLayout; 
import java.awt.EventQueue; 
import java.awt.FlowLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JRadioButton; 
import javax.swing.JTextArea; 

public class RadioBtnToTextArea { 
    public static void main(String[] args) { 
     Runnable r = new Runnable() { 

      @Override 
      public void run() { 
       new RadioBtnToTextArea().createUI(); 
      } 
     }; 

     EventQueue.invokeLater(r); 
    } 

    private void createUI() { 
     JFrame frame = new JFrame(); 

     JPanel panel = new JPanel(new BorderLayout()); 
     JPanel radioPnl = new JPanel(new FlowLayout(FlowLayout.LEFT)); 
     JPanel textPnl = new JPanel(); 


     JRadioButton radioMale = new JRadioButton(); 
     JRadioButton radioFemale = new JRadioButton(); 

     JTextArea textArea = new JTextArea(10, 30); 

     ActionListener listener = new RadioBtnAction(radioMale, radioFemale, textArea); 

     radioPnl.add(new JLabel("Male")); 
     radioPnl.add(radioMale); 
     radioMale.addActionListener(listener); 
     radioMale.setActionCommand("1"); 

     radioPnl.add(new JLabel("Female")); 
     radioPnl.add(radioFemale); 
     radioFemale.addActionListener(listener); 
     radioFemale.setActionCommand("2"); 

     textPnl.add(textArea); 

     panel.add(radioPnl, BorderLayout.NORTH); 
     panel.add(textPnl, BorderLayout.CENTER); 


     frame.add(panel); 
     frame.setVisible(true); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 
} 


class RadioBtnAction implements ActionListener { 

    JRadioButton maleBtn; 
    JRadioButton femaleBtn; 
    JTextArea textArea; 

    public RadioBtnAction(JRadioButton maleBtn, 
           JRadioButton femaleBtn, 
            JTextArea textArea) { 
     this.maleBtn = maleBtn; 
     this.femaleBtn = femaleBtn; 
     this.textArea = textArea; 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 

     int actionCode = Integer.parseInt(e.getActionCommand()); 

     switch (actionCode) { 
     case 1: 
      maleBtn.setSelected(true); 
      femaleBtn.setSelected(false); 
      textArea.setText("Male"); 
      break; 
     case 2: 
      maleBtn.setSelected(false); 
      femaleBtn.setSelected(true); 
      textArea.setText("Female"); 

      break; 
     default: 
      break; 
     } 
    } 

} 
+1

謝謝。我最終做了非常類似的工作。它最終超過了我認爲會需要的。它最終解決了。 – 2014-09-21 21:11:17