我是 Java 新手,因此提出了这个基本问题。
我在整个程序中访问字段(本例中为 jlabel)时遇到问题。我的代码如下:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class accessvariable {
public static void main(String[] args) {
//basic setup of display frame
JFrame frame=new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
JPanel panel=new JPanel();
//text field to take user input
JTextField txt= new JTextField(10);
//adding action listener for text field
txt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (true) { //if condition not checking anything here, just to make it enter the loop
JLabel j =new JLabel("Access successful"); //a jlabel created inside this
System.out.println("inside j : "+j.getText()); //statement to check whether jlabel is accessible from here
}
}
});
System.out.println("outside j : "+j.getText()); //statement to check whether jlabel is accessible from here
//basic setup for final display
panel.add(txt);
frame.getContentPane().add(panel);
frame.setVisible(true);
}
}
错误在这一行:
System.out.println("outside j : "+j.getText());
如果我注释掉这一行,它就可以正常工作。里面的j显示正确。但如果我不评论它,我会收到此错误:
Exception in thread "Exception in thread "main" java.lang.Error: Unresolved compilation problem: j cannot be resolved"
为了纠正这个问题,我将 j 设置为实例变量,如下所示:
private JLabel j;
但是上面生成了一个新错误:
Cannot make a static reference to the non-static field j
我知道问题出在这一行:
System.out.println("outside j : "+j.getText());
如何解决上述问题,以便在正常工作的情况下,在文本字段中输入某些文本时的输出应如下所示:
inside j : Access successful
outside j : Access successful
最佳答案
等等。停止。重新开始。
您的程序只不过是一个静态主方法,这对于“Hello World”类型的程序来说很好,但如果您希望创建更强大的东西,您会想要,不需要 了解面向对象的概念以及如何在 Java 程序中应用它们。例如,对于该项目的开始,您应该至少创建一个具有非静态字段和方法的类,包括 JLabel 的字段(通过该字段您将允许类的非静态方法访问该字段),并且您应该构建 GUI,这意味着将组件添加到容器中,在 main 方法之外,更像是在一些非静态 init()
方法或构造函数中。 main 方法应该只用于创建对象并将它们设置为操作,除此之外别无其他。但同样最重要的是,学习 OOP 概念以及它们与 Java 编程语言的关系。一本不错的书是 Bruce Eckel's "Thinking in Java" 。
现在另一个需要重点关注的概念是范围,包括变量范围。如果您有一个变量,这里是 JLabel 变量,j
,不仅在方法内部埋藏和声明,而且在埋藏在匿名内部 ActionListener 类内部的方法中,这几乎是不可能的以便其他类代码与此变量交互。如果它是一个实例字段,那么它在包含它的类的所有非静态部分中都是可见的。
例如:
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
@SuppressWarnings("serial")
public class AccessVariable extends JPanel {
private static final int PREF_W = 400;
private static final int PREF_H = PREF_W;
private JLabel myLabel = new JLabel();
private JTextField textField = new JTextField(10);
public AccessVariable() {
add(textField);
add(myLabel);
textField.addActionListener(new MyListener());
}
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
private class MyListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String text = textField.getText();
myLabel.setText("Text is: " + text);
textField.requestFocusInWindow();
textField.selectAll();
}
}
private static void createAndShowGui() {
AccessVariable mainPanel = new AccessVariable();
JFrame frame = new JFrame("Access Variable");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
关于java - 无法跨程序访问jlabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32041809/