gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 08:40:11 27 4
gpt4 key购买 nike

我在中得到了一些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);
}
}

警告靠近 button1、button2、label 和 textField。我正在使用 Eclipse。它有什么困扰?

附言

我可以生成 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 修饰符,这意味着您可以在类之外使用这些 variable设置获取成员的值变量。因此,它不会引发任何 Warning,因为 eclipse 感觉到这些成员变量可能在 Class 之外使用。

谢谢。

关于java - 字段 'nameOfField' 的值未被使用 - Java Swing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35772467/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com