【问题标题】:Java coding efficiency. JRadioButton objects and ItemListener interactionJava 编码效率。 JRadioButton 对象和 ItemListener 交互
【发布时间】:2019-05-14 00:27:21
【问题描述】:

我最近开始使用 Java 编码。我想编写一个包含以下内容的窗口:

  • 1 帧
  • 1 个容器
  • 2个JPanel对象(确保不会混淆Panels、Container和Frame的对象)
  • 1 卷轴的对象
  • 1 个 JTextArea 和 1 个 JTextField
  • 1 个 JButtonGroup 与 3 个 JRadioButton 关联

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

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

    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。 (这是一个原型)

    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());
    }
};

最新打印一条双重消息。因为相同的方法是共享的。 So when selection is changed there are two commutation so this method will be called twice. 我的问题来了:

我知道我可以为每个已实例化的 JRadioButton 创建一种方法。我在猜测是否有办法使一种独特的方法可行。选择的 JRadioButton 将其名称作为参数提供给 ActionListener 或 ItemListener。

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

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

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

【问题讨论】:

    标签: java performance swing jradiobutton itemlistener


    【解决方案1】:

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

    【讨论】:

    • 谢谢,我会选择第一个,因为我觉得它更接近我的第一个想法(我也会尝试你的替代方案,以获取更多知识)我之前读过, ItemListener 比 ActionPerformed 更好。但没有说明原因。有什么线索吗,先生?
    • 抱歉,不知道。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多