我在名为[[App的类中有一个TabbedPane,我想在此类中运行一个方法。我从类Login添加了两个带有JPanel的选项卡,一个是空的。这是课程:
public class App {
private static JTabbedPane tabbedPane;
public JPanel mainPanel;
public App(){
tabbedPane.addTab("Login", new Login().mainPanel);
tabbedPane.addTab("test", new JPanel());
changeFocus(0);
}
public void changeFocus(int i){
//CODE HERE
}
}现在,我想从外部类运行名为changeFocus()
的方法。使用以下构造函数向
Login
类添加了一个actionListener:public Login() {
logInButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
App.changeFocus(1);
}
});
}
现在我问为什么这不起作用,并且changeFocus()
必须为静态
。如果我将其更改为static,为什么JTabbedPane不能是静态的并抛出错误。我在名为App的类中有一个TabbedPane,我想在此类中运行一个方法。我从Login类中添加了两个带有JPanel的选项卡,一个空选项卡。这是此类:public class App {private ...App
作为参数传递给Login
的构造函数: