以适当的方式更新jLabel和jTextField

时间:2012-02-02 16:46:01

标签: java swing jlabel jtextfield

我发现了几个关于这个问题的问题并且花了一个小时,但仍然无法自己解决我的问题。我想要做的只是打开一个表单并使用setText()更改jLabel1和jTextField1的值。

Main.java:

package defaultpackage;

public class Main {
    public  void main(String args[]) {

        new InfoWindow().setVisible(true);
        InfoWindow.Refresh("a", "b");
        }
}

InfoWindow.java:

package defaultpackage;

public class InfoWindow extends javax.swing.JFrame {
    public static void Refresh(String label, String textfield) {
        jLabel1.setText(label);
        jTextField1.setText(textfield);
    }
    public InfoWindow() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("jLabel1");

        jTextField1.setText("jTextField1");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    public static void main(String args[]) {

        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(InfoWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(InfoWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(InfoWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(InfoWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

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

            public void run() {
                new InfoWindow().setVisible(true);
            }
        });

    }
    // Variables declaration - do not modify
    public static javax.swing.JLabel jLabel1;
    public static javax.swing.JTextField jTextField1;
    // End of variables declaration
}

窗口中的值不会更新。我错过了什么?

2 个答案:

答案 0 :(得分:2)

main课程中的Main - 方法缺少static关键字,因此您无法运行此关键字。

我认为您确实运行了main InfoWindow方法,该方法不会调用Refresh


更多要点:

  • 您的组件(JLabelJTextField)不应声明为static。您无法在窗口实例之间共享它们 这会迫使你Refresh非静态
  • 更新Event Dispatch Thread
  • 中的字段
  • Java方法以小写字母开头(refresh而不是Refresh

这就是你的Main课程的样子:

public class Main {
    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                InfoWindow infoWindow = new InfoWindow();
                infoWindow.setVisible(true);
                infoWindow.refresh("a", "b");
            }
        });
    }
}

答案 1 :(得分:2)

  1. 更新事件调度线程中的组件
  2. 确保您在Refresh
  3. 的实例上调用名称不佳的InfoWindow方法

    我建议你使Refresh成为非静态方法并遵循Java命名约定。还可以通过SwingUtilities.invokeLater在EDT中创建和修改组件。