gpt4 book ai didi

Java编码效率。 JRadioButton 对象和 ItemListener 交互

转载 作者:行者123 更新时间:2023-11-30 05:52:00 24 4
gpt4 key购买 nike

我最近开始用 Java 编码。我想编写一个窗口,其中包含:

  • 1 帧
  • 1 个容器
  • 2 个 JPanel 对象(确保不混淆面板、容器和框架的对象)
  • 1 卷轴对象
  • 1 个 JTextArea 和 1 个 JTextField
  • 1 个 JButtonGroup 与 3 个 JRadioButton 关联

它的目的就像一个人聊天。在文本字段中写入,提交按钮并将其打印在文本区域中,附加到任何先前的消息中。下一步,我将 3 个单选按钮命名为“User 1”、“User 2”和“User 3”根据他们的选择,他们将打印:user_x.GetName+(String)message;

我的第一次尝试是 ActionListener。 (这是一个原型(prototype)):

    ActionListener updateUserTalking = new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
JTextField tf = textField; //New message
JTextArea ta = textArea; //Previous message to track an history
String str = ta.getText()+"Radio button changed"; //This should print "User_x"
str = str+tf.setText(str+System.lineSeparator());
}
};

我的第二次尝试是 ItemListener。 (这是一个原型(prototype))

    public void itemStateChanged(ItemEvent e) {
updateSystemMessage();

这个updateSystemMessage()调用这个方法:

    ItemListener updateSystemMessage = new ItemListener(){
public void itemStateChanged(ItemEvent e) {
JTextField tf = textField; //New message
JTextArea ta = textArea; //Previous message to track an history
String str = ta.getText()+"RadioButton changed"; //This should print "User_x"
str = str+tf.setText(str+System.lineSeparator());
}
};

最新打印双消息。因为同一个方法是共享的。因此,当选择更改时,有两次交换,因此该方法将被调用两次。我的问题来了:

我知道我可以为每个 JRadioButton 实例创建一种方法。我猜测是否有一种方法可以使一种独特的方法变得可行。其中选定的 JRadioButton 将其名称作为 ActionListener 或 ItemListener 的参数。

我已经尝试过这样的事情:

   private void updateSystemMessage() {
JRadioButton jrb = (JRadioButton)this.bGroup.getSelection();
this.system =jrb.getText();
}

但它不起作用,因为 bGroup.getSelection() 返回一个无法转换为 (JRadioButton) 的 ButtonModel。那么有没有这样的方法呢?或者我必须为每个 JRadioButton 编写一种方法(谁基本上做同样的事情)?

最佳答案

ActionEvent.getSource() 将返回生成的 JRadioButton,因此您可以

ActionListener updateUserTalking = new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
JTextField tf = textField; //New message
JTextArea ta = textArea; //Previous message to track an history
JRadioButton jrb = (JRadioButton) arg0.getSource();
String str = ta.getText()+"Button "+jrb.getLabel();
str = str+tf.setText(str+System.lineSeparator());
}

或者,您可以调用 JRadioButton.setActionCommand("button1"),然后使用 ActionEvent.getActionCommand() 为每个按钮传递唯一的字符串。

JRadioButton user1 = new JRadioButton("User_1");
user1.setActionCommand("User_1");

ActionListener updateUserTalking = new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
JTextField tf = textField; //New message
JTextArea ta = textArea; //Previous message to track an history
String cmd = arg0.getActionCommand();
String str = ta.getText()+"Button "+cmd;
str = str+tf.setText(str+System.lineSeparator());
}

或者,您也可以将 actionCommand 与 ButtonModel 一起使用。

private void updateSystemMessage() {
String actionCommand = this.bGroup.getSelection().getActionCommand();
this.system = actionCommand;
}

关于Java编码效率。 JRadioButton 对象和 ItemListener 交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53750830/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com