'nameOfField' 字段的值未使用 - Java Swing

The value of the field 'nameOfField' is not used - Java Swing

我在 class 中得到了一些 JButtonJLabelJTextField。 我简单地用 panel.add() 方法初始化它们,然后我在每一个附近都收到了一个 The value of the field 'nameOfField' is not used 警告。

我的class:

public class GuiClass 
{
    private JFrame frame;
    private JButton button1;
    private JButton button2;
    private JLabel label;
    private JTextField textField;
    private JPanel upperPanel;
    private JPanel lowerPanel;

    public GuiClass()
    {
        this.frame = new JFrame("Complex layout GUI example");
        this.upperPanel = new JPanel();
        this.lowerPanel = new JPanel();

        this.upperPanel.add(this.label = new JLabel("Enter your password:"));
        this.upperPanel.add(this.textField = new JTextField(20)); // Size of the textField
        this.lowerPanel.add(this.button1 = new JButton("Cancel"));
        this.lowerPanel.add(this.button2 = new JButton("Login"));

        this.frame.setLayout(new GridLayout(2,1));

        this.frame.add(this.upperPanel);
        this.frame.add(this.lowerPanel);

        this.frame.pack();
        this.frame.setVisible(true);
    }
}

警告在按钮 1、按钮 2、标签和文本字段附近。 我正在使用 Eclipse。有什么困扰?

PS

我可以生成 getter 和 setter,警告显然消失了,但我想从中学习 Java 编程我的错误是什么(如果有任何)。

谢谢。

private JFrame frame;
private JButton button1;
private JButton button2;
private JLabel label;               //All Fields are private 
private JTextField textField;
private JPanel upperPanel;
private JPanel lowerPanel;

Here All refrence Variable that u made are private. It means all member variables have Scope of this Class Only. So, You Should use all of these refrence variables with in this Class. Otherwise eclipse will show a warning. However these warnings are not a error but Some smart IDE like eclipse and Intelli j Idea will warn you if any unused variables you have declared.

现在谈谈你的第二个论点。这是

Generate getter's and setter's and the warnings obviously gone ?

如果生成getterssetters。这些具有 public 修饰符,这意味着您可以在 Class 之外使用这些 variablesetget 成员 variables 的值。因此,它不会引发任何 Warning 因为 eclipse 感觉这些成员变量可以在 Class.

之外使用

谢谢。