加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

在开始活动之前,需要单击两次Java jButton

发布时间:2020-12-14 19:21:43 所属栏目:Java 来源:网络整理
导读:我正在Netbeans 7.1中构建一个GUI,它代表了医院设备项目的输液泵.按jButton1应显示某个jLabel,表示输液正在运行.再次按jButton1应该停止它.我唯一的问题是按钮需要在第一次工作之前点击两次.在那之后,它完全符合我希望它的工作方式.我在这个问题上搜索了多个

我正在Netbeans 7.1中构建一个GUI,它代表了医院设备项目的输液泵.按jButton1应显示某个jLabel,表示输液正在运行.再次按jButton1应该停止它.我唯一的问题是按钮需要在第一次工作之前点击两次.在那之后,它完全符合我希望它的工作方式.我在这个问题上搜索了多个线程,但其他解决方案似乎不起作用.谁能帮我?提前致谢!

  package infusion;
  public class Infusion {
    static Interface screen;

    public Infusion(){

        screen = new Interface();
        screen.setVisible(true);
        screen.jLabel3.setVisible(true);
        screen.jButton1.setVisible(true);
    }    

    public static void main(String[] args) {
        // TODO code application logic here
        Infusion infusion = new Infusion();
    }
   }

这是用户界面的代码(主要由Netbeans生成). jButton1ActionPerformed的实现是我调整过的唯一代码.

package infusion;

    public class Interface extends javax.swing.JFrame {
        boolean pump1Running = false;
        /**
         * Creates new form Interface
         */
        public Interface() {
            initComponents();
        }


        /**
         * This method is called from within the constructor to initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is always
         * regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // 
最佳答案
问题:pump1Running = false,所以当你点击按钮时,它会将标签和图像设置为与之相同的东西.然后将其切换为true,然后它可以切换到另一个标签/图像.

        if (pump1Running){
            jLabel3.setIcon(new ImageIcon(getClass().getResource("/Pauze.png")));
            jLabel3.setText("Infusion Paused");
            jButton1.setText("Start Infusion");
            pump1Running = !pump1Running;
        }
        else {//if (pump1Running == true){
            jLabel3.setIcon(new ImageIcon(getClass().getResource("/Pauze.png")));
            jLabel3.setText("Running infusion");
            jButton1.setText("Stop Infusion");
            pump1Running = !pump1Running;
        } 

这会切换pump1Running来回运行并解决问题.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读