【问题标题】:How to check an event in another method in Java swing?java - 如何在Java swing中的另一种方法中检查事件?
【发布时间】:2013-01-17 18:19:12
【问题描述】:

在我的项目中,我想在服务器中选择一个文件并在 java swing 中发送给客户端。在此,我必须在客户的单选按钮中发送给我喜欢的客户。但我不知道如何检查在发送按钮中单击了单选按钮。因为我必须检查在发送按钮方法中单击了单选按钮。

我的单选按钮代码

jRadioButton1.setText("One");
jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
  public void actionPerformed(java.awt.event.ActionEvent evt) {
    jRadioButton1ActionPerformed(evt);
  }
});
}


private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {
  JOptionPane.showMessageDialog(null, "You Selected Button 1");
}

对于发送按钮......

    jButton1.setText("SEND");
    jButton1.addActionListener(new java.awt.event.ActionListener() 
    {
        public void actionPerformed(java.awt.event.ActionEvent evt) 
        {
            jButton1ActionPerformed(evt);
        }
    });

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) 
    {

    jRadioButton1ActionPerformed(evt);
    Object k=evt.getSource();
    System.out.println(k);
}

如何检查发送操作方法中的单选按钮是否被点击?

【问题讨论】:

    标签: java swing actionlistener method-call


    【解决方案1】:

    假设您的不同 UI 组件在同一个类中并声明为实例成员,您可以在类中的任何位置简单地引用它们。所以你可以写:

    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            if (jRadioButton1.isSelected()) {
                System.out.println("radio button selected");
            } else {
                System.out.println("radio button NOT selected");
            }
        }
    });
    

    【讨论】:

    • 不是我的朋友,谢谢。我会尽力给出意见。 :D
    • 我相信 OP 只是想在单击 Send 按钮时检查单选按钮的状态。
    • @MarkoTopolnik 我明白你的意思......建议的代码没有意义。
    • @assylias 我做了同样的事情,但是当我点击发送按钮时没有执行任何操作。那么问题出在哪里?
    • @MuralidharanT 我想我误解了你的问题。查看我的编辑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-05
    • 2012-03-16
    • 2017-10-14
    相关资源
    最近更新 更多