为什么我不能在构造函数之外访问 JLabel?
Why can't I access a JLabel outside of the constructor?
所以我在主 class 的构造函数中创建了一个 JLabel,然后,我在 class 中有一个单独的方法来更新它 (updateLabel())。但无法从更新方法中访问标签。如何从方法内部访问它?谢谢
public Difficulty(){
JLabel lblNewLabel = new JLabel((comboBox_9.getSelectedItem().toString()));
lblNewLabel.setBounds(300, 70, 46, 23);
contentPane.add(lblNewLabel);
}
public void updateLabel(Skill skill){
}
在更新Label方法中我想说的是lblNewLabel.setText(skill.toString())
但是我无法访问标签。
您必须在构造函数外部声明它以使其对其他人可见 methods.If 您在构造函数内部声明它,它具有局部作用域而不是全局作用域,这意味着它只能在 constructor.In 一般在方法内声明的所有内容都只能在该方法内访问
For example:
public class Foo{
JLabel label=new JLabel("Global");//this has a global scope
public Foo(){
JLabel label_1=new JLabel("local scope");//this can't be accesed outside //the constructor
label_1.setText("new text");//but inside the constructor you can do what you want
}
}
您需要考虑变量作用域:Java Tutorials - Declaring Member Variables
如果您在构造函数的 范围 内声明一个对象,那么它将无法在其他地方访问,例如您的 updateLabel() 方法。
尝试将 JLabel 声明为字段,例如:
private JLabel label;
public JLabelContainer() {
label = new JLabel();
}
public void updateLabel(String text){
label.setText(text);
}
此处,标签在字段级别声明,在构造函数中实例化,然后在 updateLabel(..)
方法中访问。
要在任何方法中访问变量,它应该在该块的范围内。如果您想访问 class 中任何位置的变量,那么该变量应该具有 public
访问权限,或者至少是 private
实例变量。
在您的例子中,由于 JLabel
在构造函数中具有 local
访问权限,因此该变量的范围会在构造函数结束后立即结束。
对于您的情况,这可能是可行的解决方案之一:
private JLabel lblNewLabel;
public Difficulty(){
lblNewLabel = new JLabel((comboBox_9.getSelectedItem().toString()));
lblNewLabel.setBounds(300, 70, 46, 23);
contentPane.add(lblNewLabel);
}
public void updateLabel(Skill skill){
//access lblNewLabel here.
}
所以我在主 class 的构造函数中创建了一个 JLabel,然后,我在 class 中有一个单独的方法来更新它 (updateLabel())。但无法从更新方法中访问标签。如何从方法内部访问它?谢谢
public Difficulty(){
JLabel lblNewLabel = new JLabel((comboBox_9.getSelectedItem().toString()));
lblNewLabel.setBounds(300, 70, 46, 23);
contentPane.add(lblNewLabel);
}
public void updateLabel(Skill skill){
}
在更新Label方法中我想说的是lblNewLabel.setText(skill.toString())
但是我无法访问标签。
您必须在构造函数外部声明它以使其对其他人可见 methods.If 您在构造函数内部声明它,它具有局部作用域而不是全局作用域,这意味着它只能在 constructor.In 一般在方法内声明的所有内容都只能在该方法内访问
For example:
public class Foo{
JLabel label=new JLabel("Global");//this has a global scope
public Foo(){
JLabel label_1=new JLabel("local scope");//this can't be accesed outside //the constructor
label_1.setText("new text");//but inside the constructor you can do what you want
}
}
您需要考虑变量作用域:Java Tutorials - Declaring Member Variables
如果您在构造函数的 范围 内声明一个对象,那么它将无法在其他地方访问,例如您的 updateLabel() 方法。
尝试将 JLabel 声明为字段,例如:
private JLabel label;
public JLabelContainer() {
label = new JLabel();
}
public void updateLabel(String text){
label.setText(text);
}
此处,标签在字段级别声明,在构造函数中实例化,然后在 updateLabel(..)
方法中访问。
要在任何方法中访问变量,它应该在该块的范围内。如果您想访问 class 中任何位置的变量,那么该变量应该具有 public
访问权限,或者至少是 private
实例变量。
在您的例子中,由于 JLabel
在构造函数中具有 local
访问权限,因此该变量的范围会在构造函数结束后立即结束。
对于您的情况,这可能是可行的解决方案之一:
private JLabel lblNewLabel;
public Difficulty(){
lblNewLabel = new JLabel((comboBox_9.getSelectedItem().toString()));
lblNewLabel.setBounds(300, 70, 46, 23);
contentPane.add(lblNewLabel);
}
public void updateLabel(Skill skill){
//access lblNewLabel here.
}