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

前端之家收集整理的这篇文章主要介绍了在开始活动之前,需要单击两次Java jButton前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在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")
        // SEOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setBackground(new java.awt.Color(255,255,255));
            setPreferredSize(new java.awt.Dimension(800,562));
            setResizable(false);

            jPanel1.setPreferredSize(new java.awt.Dimension(800,540));

            javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0,800,Short.MAX_VALUE)
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0,540,Short.MAX_VALUE)
            );

            jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Pauze.png"))); // NOI18N
            jLabel3.setText("Infusion Paused");

            jButton1.setText("Start infusion");
            jButton1.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });

            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,layout.createSequentialGroup()
                    .addContainerGap(36,Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,layout.createSequentialGroup()
                            .addComponent(jButton1)
                            .addGap(18,18,18)
                            .addComponent(jLabel3,javax.swing.GroupLayout.PREFERRED_SIZE,189,javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(jSeparator1,javax.swing.GroupLayout.Alignment.TRAILING,649,javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jPanel1,javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,layout.createSequentialGroup()
                    .addGap(0,22,Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(51,51,51)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jButton1,64,javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel3,59,javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(18,18)
                            .addComponent(jSeparator1,10,javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(jPanel1,javax.swing.GroupLayout.PREFERRED_SIZE)))
            );

            pack();
        }// 
最佳答案
问题: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来回运行并解决问题.

原文链接:https://www.f2er.com/java/438198.html

猜你在找的Java相关文章