从导入的子类访问Java父方法
是Java新手.从网上学习.我遇到了代码问题. 我有三堂课. mainclass.java,oneclass.java,twoclass.java. 我在mainclass中导入了oneclass和twoclass.
Am a java newbie. Learning from the web. I am stuck with an issue with my code. I have three class. mainclass.java, oneclass.java, twoclass.java. I imported oneclass and twoclass in mainclass.
oneclass具有下一步"按钮.我想做的是在下一个btn上的onActionEvent,从mainclass调用一个函数,该函数会将oneclass的可见性设置为false,twotwoclass的可见性设置为true. 我正在发布整个代码 mainclass.java
oneclass has a "next" button. What i want to do is onActionEvent on next btn , call a function from mainclass, which would set the visibility of oneclass to false and twoclass to true. Am posting the whole code for mainclass.java
package com.mainclass;
import com.twoframe.twojframes;
import com.secondframe.secondjframe;
public class MainClass {
private static com.secondframe.twoclass panel2;
private static com.twoframe.oneclass panel1;
private static void openPanel1(){
panel1 = new com.twoframe.oneclass();
panel1.setVisible(true);
}
public static void toggleVisibility(){
System.out.println("called from child");
panel2 = new com.secondframe.twoclass();
panel2.setVisible(true);
panel1.setVisible(false);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
openPanel1();
}
});
}
}
oneclass.java
oneclass.java
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
// TODO add your handling code here:
//Basically something like -- mainclass.toggleVisibility();
}
谢谢.
假设父级"是您要扩展的类,并且您调用的方法不是静态的,则应使用以下方法:
Assuming the "parent" is a class you're extending and the method you're calling is NOT static, the following should do the trick:
super.toggleVisibility();
如果它是静态方法-甚至更简单:
If it's a static method - it's even Simpler:
ParentClassName.toggleVisibility();