java - 将选定的 JRadioButton 中的文本显示到 JTextArea

标签 java swing user-interface jtextarea jradiobutton

我正在做家庭作业(请不要为我做作业)。我已经完成了 95%。不过,我在最后一点上遇到了麻烦。我需要在 JTextArea 中显示选定的性别。我必须使用 JRadioButton 进行性别选择。

我知道 JRadioButtons 的工作方式不同。我设置了 Action 监听器和助记符。我想我在这里搞砸了。看来我可能需要使用整个组来设置和操作列表器。

非常感谢任何帮助。

这是我的代码(减去我认为不需要的部分,以便其他人无法复制粘贴作业):

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*/ 

最佳答案

我在 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;
        }   
    }

}

关于java - 将选定的 JRadioButton 中的文本显示到 JTextArea,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25961541/

相关文章:

java - 为什么 Swing 线程模型被认为是错误的,它应该如何?

java - JDatePicker 没有出现在 JFrame 上

java - @Autowired 在 SessionFactory 中返回 NullPointerException

JavaFX WebView 无法使用不受信任的 SSL 证书工作

java - 是否可以将jtable模型分成5个不同的模型?

user-interface - 使用 go 的 exp/shiny 时如何绘制底层小部件?

当提供从网络接收到的字符串时,Curses printw() 将仅打印换行符

java - 如何高效添加Java成员(member)按钮

java - 从按元素值排序的多个 vector 调用函数

java - Apache Camel - 发送 JMS 消息时发出警告