Java 初学者问题。无法访问主方法之外的类实例方法?
我正在阅读《头先Java》一书,并开始阅读第12章。当我试图创建方法Java 初学者问题。无法访问主方法之外的类实例方法?,java,Java,我正在阅读《头先Java》一书,并开始阅读第12章。当我试图创建方法changebuttonext()时,我无法从button访问任何类方法 为什么会这样?我在这个代码中做错了什么 import javax.swing.*; 公共班机{ 公共静态void main(字符串[]args){ JFrame=新JFrame(); JButton按钮=新JButton(“单击我”); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.
changebuttonext()
时,我无法从button
访问任何类方法
为什么会这样?我在这个代码中做错了什么
import javax.swing.*;
公共班机{
公共静态void main(字符串[]args){
JFrame=新JFrame();
JButton按钮=新JButton(“单击我”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(按钮);
框架。设置尺寸(300300);
frame.setVisible(true);
}
public void changebuttonext(){
setText(“我被点击了”);
}
}
无法访问变量的原因是因为变量的范围(关于变量范围的教程:)
您在main
方法中声明变量JButton button
,因此它在它之外的任何地方都无法访问,即使在main
调用自身的方法中也是如此
要使changebuttonext
意识到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");
}
}
我还在
changebuttonext
方法前面添加了static
关键字,因为main
也是一个静态方法。例如,请查看此链接以了解有关差异的更多详细信息:您的代码和概念中有一些错误,您似乎没有完全理解。这些概念是this.
和scopes
public void changeButtonText(){
button.setText("I've been clicked");
}
首先我们需要讨论范围。范围是在{/*here*/}
之间声明的内容,即两个花括号。在一个范围内,您可以访问和声明本地和全局对象、变量、成员等
下面是一个使用局部作用域的示例(您所做的):
如果我继续尝试访问范围外的按钮,它将不知道按钮是什么。因为它不是在本地范围内声明的
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
}
}
当然,这是一个范围问题,可以通过在全局级别定义变量并根据需要进行初始化来解决。差不多-
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");
}
}
您没有定义任何类实例变量,也没有实例化一个对象来使用它们。@Abra这实际上就是书中的代码在第355页上写的方式。也许我是想推断,当我添加changebuttonext方法时,我需要自动进行这些更改?方法
changebuttonext()
没有出现在《第一个Java》一书第355页的代码中。@Abra changebuttonext()是作者在第357页的“changeIt()”的名字。前面的代码与第页上显示的代码相同355@NMard请阅读我的答案,如果有什么需要我进一步澄清的,请告诉我:)谢谢。
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");
}
}