我无法在使用网络bean时修改标签中的值

时间:2013-09-04 07:23:38

标签: java swing api user-interface

我是Net Beans的新手。当我尝试运行这个时,我得到一个错误,就像非静态变量无法从静态上下文引用。请帮我解决。但是当我在主要内部声明Label时,我能够获得输出(在eclipse中)。 net beans不允许更改变量声明。

import java.net.InetAddress;

public class Test extends javax.swing.JFrame {

    public Test() {
        initComponents();
    } // 

    private void initComponents() {
        l1 = new javax.swing.JLabel();
        name = new javax.swing.JLabel();
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        l1.setText("System Name:");
        name.setText("jLabel2");
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                .addComponent(l1, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 21, Short.MAX_VALUE))
                );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(109, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(l1, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(102, 102, 102))
                );
        pack();
    }// 

    public static void main(String args[]) {
        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(test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
// 
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Test().setVisible(true);
            }
        });
        try {
            ip = InetAddress.getLocalHost();
            name.setText(ip.getHostName());
        } catch (Exception e) {
        }
    }
    InetAddress ip;
// Variables declaration - do not modify 
    private javax.swing.JLabel l1;
    private javax.swing.JLabel name;
// End of variables declaration 
}

2 个答案:

答案 0 :(得分:1)

这基本上告诉您正在尝试从静态上下文中引用一些非静态变量。

看看你main方法。它被解析为static,它不依赖于要调用的类的给定实例。

public static void main(String args[]) {
    /*...*/
    try {
        ip = InetAddress.getLocalHost();
        name.setText(ip.getHostName());
    } catch (Exception e) {
    }
}

InetAddress ip;
// Variables declaration - do not modify 
private javax.swing.JLabel l1;
private javax.swing.JLabel name;

但是您尝试访问的变量不是(static),它们会在声明它们的类的特定实例(AKA实例变量)上进行中继。

按照目前的情况,你唯一的选择就是将有问题的代码移动到类的构造函数或其他一些你可以调用的非静态方法中......

public Test() {
    initComponents();
    try {
        ip = InetAddress.getLocalHost();
        name.setText(ip.getHostName());
    } catch (Exception e) {
    }
} // 

花点时间阅读Understanding Instance and Class Members

您可能还希望阅读Code Conventions for the Java Programming Language

答案 1 :(得分:0)

此错误与netbeans无关。相反,当你从静态方法引用非静态变量时,你得到这个。

因此,对于简短修复,您可以使变量保持静态。对于长时间的修复,不要从main方法引用它们,而是使用main中的代码创建单独的方法,并从main调用该方法,如new ClassName()。methodName();在这种情况下,不需要声明变量static。