切换JButton setEnabled属性 [英] Toggling JButton setEnabled property

查看:89
本文介绍了切换JButton setEnabled属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个c ++控制台应用程序,我正在尝试为其创建GUI,但是我迷失了所有API,因此我决定改而创建Java GUI,并使用TCP来回发送控制消息.到目前为止,它的工作就像一种魅力,但是有一件我无法工作的事情.我需要根据c ++应用程序的状态启用/禁用一些按钮,所以我的想法是要有一个循环,用于检查传入的消息,然后更新一个整数变量.理想情况下,有两种方法可以更新按钮:

1-要循环检查int变量并更新按钮状态
2-让变量更改触发事件,并使用侦听器
更新按钮

该GUI是使用Netbeans gui构建器创建的,因此jButton是私有的并且具有写保护.

有办法实现吗?

这是代码

Hi, I have a c++ console application and I am trying to create a GUI for it, but I am lost in all the APIs, so I decided do create instead a Java GUI and send control messages back and forth using TCP. So far it works like a charm, but there is one thing I can''t get to work. I need to enable/disable some buttons depending on the state of the c++ app, so my idea is to have a loop which checks for incoming messages, and then updates an integer variable. There would then be ideally two ways to update the buttons:

1 - to have a loop check the int variable and update the button state
2 - to have the variable change trigger an event, and use a listener to
update the button

The GUI was created using Netbeans gui builder, so the jButtons are private and write protected.

Is there a way to implement this?

Here''s the code

public class Telis extends javax.swing.JFrame{

    static Socket echoSocket = null;
    static PrintWriter out = null;
    static BufferedReader in = null;
    static String userInput;
    static Integer input;

    /** Creates new form */
    public Telis() {

                /**
                 initialze socket here
                **/

        initComponents();//I removed the implementation of init components, to reduce message length, basically it sets up the view
    }


    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        out.println("#EFC0");
    }

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
        out.println("#EFC1");
    }

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
        out.println("#EFC2");
    }

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
        out.println("#EFC3");
    }

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
        out.println("#EFC4");
    }

    private void jButton1PropertyChange(java.beans.PropertyChangeEvent evt) {

    }

    public static void main(String args[]) {

        try {
            out.close();
            in.close();
            echoSocket.close();
        } catch (Exception e) {
            System.err.println("Exception: " + e.getMessage());
        }

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Telis().setVisible(true);
            }
        });
        while(true){
                   try{
                       userInput = in.readLine();
                       input = userInput.charAt(0) - 48;
                   }catch (Exception e) {
            System.err.println("Exception: " + e.getMessage());
        }
        }
    }


    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration
}

推荐答案

应该很简单.

在类Telis中,您具有每个按钮的成员变量,因此,在收到输入时,您可以这样更新按钮:
It should be simple.

In the class Telis you have the memeber variables for each of the buttons so when the input is received you can update the buttons thus:
jButton1.setEnabled(true || false);


这篇关于切换JButton setEnabled属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆