Java 扩展类时出错
我对Java相当陌生,所以我不知道出了什么问题。我这里有一个代码,当它被执行时,应该“打开”一个窗口一次,但是每当我在Java 扩展类时出错,java,extend,Java,Extend,我对Java相当陌生,所以我不知道出了什么问题。我这里有一个代码,当它被执行时,应该“打开”一个窗口一次,但是每当我在按钮Handler类中扩展类ColoredWordsExperiment,窗口就会无限快地打开,几乎每次都会导致我的计算机崩溃。如果我省略了扩展名,那么它可以正常工作,但是我将无法使用按钮Handler类中的ColoredWordsExperiment类中的对象。。。下面您可以找到代码(我遗漏了一些不重要的东西,否则它会变得太长) - 在父类的构造函数中,您正在创建其中一个But
按钮Handler
类中扩展类ColoredWordsExperiment
,窗口就会无限快地打开,几乎每次都会导致我的计算机崩溃。如果我省略了扩展名,那么它可以正常工作,但是我将无法使用按钮Handler
类中的ColoredWordsExperiment
类中的对象。。。下面您可以找到代码(我遗漏了一些不重要的东西,否则它会变得太长)
-
在父类的构造函数中,您正在创建其中一个ButtonHandler,然后再次运行构造函数代码。
您不应该在构造函数中实例化该类(或者如果您试图使用同名的类,则使用不同的名称)在这种情况下,没有理由扩展
ColoredWordsExperiment
。您只需实现ActionListener
实际上,您正在使用一个附加的方法在其内部初始化另一个ColoredWordsExperiment
实例。这将再次调用构造函数,从而重新创建GUI窗口。有关更多详细信息,请参阅 如果要从
ActionListener
实现中更改ColoredWordsExperiment
中的字段,则需要在构造过程中传递引用
public class ButtonHandler implements ActionListener {
ColoredWordsExperiment coloredWords;
public ButtonHandler(ColoredWordsExperiment coloredWords) {
this.coloredWords = coloredWords;
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Matching")) {
System.out.println("Matching");
coloredWords.label1.setText("Text changed");
}
}
}
在本例中,您还可以选择创建匿名内部类。使用此技术,您可以完全摆脱
ButtonHandler
类。内置
彩色文字体验
而不是
buttonHandler = new ButtonHandler();
button1.addActionListener(buttonHandler);
你可以用
button1.addActionListener(new ActionListener() {
if (e.getActionCommand().equals("Matching")) {
System.out.println("Matching");
label1.setText("Text changed");
}
});
请看好的,我知道什么是巡演问题了。现在我不知道您为什么要扩展它,我只是想回答您的问题(b/c没有必要将动作侦听器扩展到主类)。当您定义类方法时,您没有放置一个public,然后当您放置在main方法下运行它时,它可能会被混淆并生成无限帧。您应该将其更改为:
public class ColoredWordsExperiment {
JFrame frame;
ButtonHandler buttonHandler;
public ColoredWordsExperiment(){//add the public!
frame = new JFrame("Colored Words Experiment");
frame.setSize(1200, 150);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button1 = new JButton("Matching");
label1 = new JLabel("test");
label1.setPreferredSize(new Dimension(90, 40));
JPanel labelContainer = new JPanel();
labelContainer.add(label1);
JPanel buttonContainer = new JPanel();
buttonContainer.add(button1);
frame.add(buttonContainer, BorderLayout.SOUTH);
frame.add(labelContainer, BorderLayout.NORTH);
buttonHandler = new ButtonHandler();
button1.addActionListener(buttonHandler);
}
public static void main(String[] arg) {
new ColoredWordsExperiment();
}
}
此外,将
buttonHandler
变量定义为主类中的buttonHandler类,然后将buttonHandler类扩展到主类之外,这不是一个好主意。这可能会导致循环。您不应该扩展第二个类,也不应该以不同的方式定义按钮句柄。听起来您需要阅读并理解类和对象之间的区别。为了帮助您解决这个问题,我想用一个简化的示例来说明您最初思考代码的方式存在的一个问题:
class A {
int x;
B b = new B();
}
class B extends A {
}
class Main {
public static void main(String args[]) {
A a = new A();
a.x = 42;
a.b.x = 53;
System.out.println(a.x);
System.out.println(a.b.x);
}
}
正如预期的那样,main()
中的对象a
有一个名为x
的字段,我们可以将其设置为42。但是,对象b
在a
内部有自己的名为x
的字段,该字段与对象a
内部的x
字段完全无关
如果你能了解对象的概念,你将很快成为一名优秀的Java程序员。你应该向他解释为什么代码会以这种方式运行OK,但我想在单击“匹配”按钮时更改label1的文本,如果不扩展ButtonHandler中的ColoredWordsExperiment,我该如何做?自label1.setText(“文本更改”);如果不扩展ColoredWordsExperiment,则不起作用。编辑以添加更多详细信息,并解释匿名内部类。两种解决方案都应该考虑到这一点。谢谢!我使用的是第一种方法,因为我不理解第二种方法(实际上看起来更容易,但效果很好)。但是我仍然有一个问题,如果我使用第一个方法,我应该在ColoredWordsExperiment类中的“buttonHandler=new buttonHandler(此处放置的内容);”中作为参数传递什么?您将把ColoredWordsExperiment
实例的引用传递到构造函数中。因此,使用buttonHandler=新buttonHandler(本);
public class ColoredWordsExperiment {
JFrame frame;
ButtonHandler buttonHandler;
public ColoredWordsExperiment(){//add the public!
frame = new JFrame("Colored Words Experiment");
frame.setSize(1200, 150);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button1 = new JButton("Matching");
label1 = new JLabel("test");
label1.setPreferredSize(new Dimension(90, 40));
JPanel labelContainer = new JPanel();
labelContainer.add(label1);
JPanel buttonContainer = new JPanel();
buttonContainer.add(button1);
frame.add(buttonContainer, BorderLayout.SOUTH);
frame.add(labelContainer, BorderLayout.NORTH);
buttonHandler = new ButtonHandler();
button1.addActionListener(buttonHandler);
}
public static void main(String[] arg) {
new ColoredWordsExperiment();
}
}
class A {
int x;
B b = new B();
}
class B extends A {
}
class Main {
public static void main(String args[]) {
A a = new A();
a.x = 42;
a.b.x = 53;
System.out.println(a.x);
System.out.println(a.b.x);
}
}