我正在阅读"Head first Java"一书,并开始了第12章。
当我尝试创建方法时changeButtonText()
我无法从button
访问任何类方法。
这是为什么呢?我在这段代码中做错了什么?
import javax.swing.*;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame();
JButton button = new JButton("Click Me");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(button);
frame.setSize(300,300);
frame.setVisible(true);
}
public void changeButtonText(){
button.setText("I've been clicked");
}
}
无法访问变量的原因是因为它的作用域(关于变量作用域的教程:https://www.baeldung.com/java-variable-scope(。
在main
方法中声明变量JButton button
,因此它无法在其外部的任何位置访问,即使在main
调用自身的方法中也是如此。
若要使changeButtonText
知道button
变量存在,必须将其作为此方法的参数传递:
import javax.swing.*;
public class Main {
public static void main(String[] args) {
JButton button = new JButton("Click Me");
changeButtonText(button);
}
public static void changeButtonText(JButton button){
button.setText("I've been clicked");
}
}
我还在changeButtonText
方法前面添加了static
关键字,因为main
也是一个静态方法。例如,请查看此链接以获取有关差异的更多详细信息:https://www.geeksforgeeks.org/static-methods-vs-instance-methods-java/
当然,这是一个范围问题,可以通过在全局级别定义变量并在需要的地方初始化来克服。 像——
public class Main {
private static JButton button;
public static void main(String[] args) {
JFrame frame = new JFrame();
button = new JButton("Click Me");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(button);
frame.setSize(300,300);
frame.setVisible(true);
Main.changeButtonText();
}
public static void changeButtonText(){
button.setText("I've been clicked");
}
}
你的代码和概念有几点问题,你似乎没有完全掌握。这些概念是this.
和scopes
的。
public void changeButtonText(){
button.setText("I've been clicked");
}
首先,我们需要谈谈作用域。作用域是在{ /* here */ }
之间声明的内容,即两个大括号。从作用域内,您可以访问和声明局部和全局对象、变量、成员等。
因此,下面是一个使用本地范围的示例(您所做的(:
public void myFunction(){
JButton button = new Jbutton();
}
如果我继续尝试访问范围之外的按钮,它将不知道button
是什么。因为它不是在该本地范围内声明的。
public void myFunction(){
JButton button = new Jbutton();
}
public static void main(String[] args) {
button.setText("hello"); //error
// compiler thinks: what's `button` referring to here? its not declared in this scope.
}
全局类范围:
JButton button = new JButton(); //declaring button in global class scope
public void myFunction(){
this.button.setText("hello"); // will access the globally declared variable, object, member called `button`.
//^ notice the usage of `this`.
//`this` will look outside of the local functions scope for publically declared class-members.
}
要解决您的特定问题,您可以将函数设为静态并传入JButton
对象,或者您可以在全局类作用域中声明您的按钮,如下所示:
public class Main {
JButton button = new JButton("Click Me"); //global class scope
public static void main(String[] args) { //local scope of main start {
Main myClass = new Main(); //instantiate class
myClass.changeButtonText(); //access class-member function
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(this.button); // this.
frame.setSize(300,300);
frame.setVisible(true);
} // local scope of main ends }
public void changeButtonText(){
this.button.setText("I've been clicked"); //access global class members using `this` keyword
}
}