英文:
How can I outsource the method of a menu to a separate class?
问题
昨天我开始使用Java Swing编写程序。之前我只在Java中编写过一些小的控制台程序。
我将程序的主菜单放在了与主方法不同的类中。现在我想为选项菜单添加一个按钮,并将其执行的代码放到一个单独的类中,这样在主菜单类中我只需要调用选项菜单的方法。
但在这一步我卡住了。如果我直接在主菜单类中编写选项菜单的代码,一切都正常。但是一旦我将选项菜单的方法外包到一个单独的类中,我就无法调用外包的方法。
也许你可以帮助我。
下面是点击选项按钮时要执行的代码:
public class CPP_Frame extends JFrame {
JButton SettingsButton = new JButton("Options");
SettingsButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CPP_Frame OptionsMenu = new CPP_Frame();
OptionsMenu.OptionsMenuLoader();
}
});
}
以下是在"OptionsMenuLoader"方法中将会被调用的选项菜单类中的代码:
public class Options_Menu extends JFrame {
protected void OptionsMenuLoader() {
CPP_Frame OptionsMenu = new CPP_Frame();
OptionsMenu.setSize(new Dimension(400, 500));
OptionsMenu.setVisible(true);
OptionsMenu.setTitle("Options");
OptionsMenu.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
我在哪里出错了,或者我尝试编写代码的方式是否可行。我希望我已经足够详细地解释了我的问题。提前感谢。
英文:
Yesterday I started Coding a program in Java Swing. Before I coded only small console programs in java.
I put the main menu of the program in a different class than the main method. Now I wanted to add a button for an options menu and also put the code it executes into a class of its own, so that in the main menu class I only have to call the options menu method.
And here I get stuck. If I write the code for the options menu directly in the class of the main menu everything works. But as soon as I outsource the method of the options menu to a separate class, I cannot call the outsourced method.
Perhaps you can help me.
Here is the code that shall be performed when hitting the options button:
public class CPP_Frame extends JFrame {
JButton SettingsButton = new JButton("Options");
SettingsButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CPP_Frame OptionsMenu = new CPP_Frame();
OptionsMenu.OptionsMenuLoader();
}
});
}
And that´s the code inside the Options Menu-class that shall be called by the 'OptionsMenuLoader':
public class Options_Menu extends JFrame {
protected void OptionsMenuLoader() {
CPP_Frame OptionsMenu = new CPP_Frame();
OptionsMenu.setSize(new Dimension(400, 500));
OptionsMenu.setVisible(true);
OptionsMenu.setTitle("Options");
OptionsMenu.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
Where do I mistaken or is the way I try to write the code even possible.
I hope I´ve explained my problem detailed enough.
Thanks in Advance.
答案1
得分: 0
我在这里看到了一些潜在问题,并且对于 Swing 我的知识有限,如果我理解有误,请纠正我,我只是从编程角度来看待这个问题。
首先,OptionsMenuLoader()
被声明为受保护的方法。这意味着它只能被同一类中的方法调用,或者从它派生的类调用。
因此,要调用 OptionsMenuLoader
,它要么必须在与 actionPerformed
覆盖相同的类中,要么 CPP_Frame 必须继承 Options_Menu,但从您的结构来看,这可能是错误的。
如果在 Swing 格式内可能的话,解决方案之一是将 'OptionsMenuLoader()' 设为公共方法:
public void OptionsMenuLoader(){
...
}
其次,我不确定为什么这不会产生错误,您在 OptionsMenu
上调用 OptionsMenuLoader()
,虽然看起来是正确的,但您将 OptionsMenu
声明为 CPP_Frame
而不是 Options_Menu
类型。这应该行不通,因为 CPP_Frame
没有名为 OptionsMenuLoader()
的方法。
我认为您的目标是创建一个包含 Options 菜单的 CPP 窗体。当另一个 CPP 窗体中的按钮被点击时,创建该菜单。
在这种情况下,您将不得不在操作时创建一个 Options_Menu
,而不是一个 CPP Frame。然后,您可以在这个新菜单上调用 OptionsMenu.OptionsMenuLoader()
方法。
public class CPP_Frame extends JFrame {
JButton SettingsButton = new JButton("Options");
SettingsButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Options_Menu OptionsMenu = new Options_Menu();
OptionsMenu.OptionsMenuLoader();
}
});
}
public class Options_Menu extends JFrame {
public void OptionsMenuLoader() {
CPP_Frame OptionsMenu = new CPP_Frame();
OptionsMenu.setSize(new Dimension(400, 500));
OptionsMenu.setVisible(true);
OptionsMenu.setTitle("Options");
OptionsMenu.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
英文:
I can see a couple potential issues here, and I'm not overly knowledgeable about swing so correct me if I am wrong, I'm just looking at this from a programming point of view
Firstly OptionsMenuLoader()
is declared as a protected method. This means if can only be called by methods in the same class as it, or classes that are derived from it.
Therefor to call OptionsMenuLoader
it must either be in the same class as the actionPerformed
override, or CPP_Frame would have to inherit Options_Menu, which from your structure I imagine would be wrong.
If this is possible with-in the format of a swing one solution would be to make 'OptionsMenuLoader()' public
public void OptionsMenuLoader(){
...
}
Secondly i'm not sure why this does not produce an error, you call OptionsMenuLoader()
on OptionsMenu
, while this looks right you declared the OptionsMenu
as a CPP_Frame
rather than as an Options_Menu
type. This shouldn't work because CPP_Fram
e has no such method OptionsMenuLoader()
.
I think what you are aiming to do is create a CPP frame that contains an Options Menu. Which is created when the button in a different CPP Frame is clicked.
In this case you would have to create an Options_Menu
on action, not a CPP Frame. You can then call the OptionsMenu.OptionsMenuLoader()
method on this new menu.
public class CPP_Frame extends JFrame {
JButton SettingsButton = new JButton("Options");
SettingsButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Options_Menu OptionsMenu = new Options_Menu();
OptionsMenu.OptionsMenuLoader();
}
});
}
public class Options_Menu extends JFrame {
public void OptionsMenuLoader() {
CPP_Frame OptionsMenu = new CPP_Frame();
OptionsMenu.setSize(new Dimension(400, 500));
OptionsMenu.setVisible(true);
OptionsMenu.setTitle("Options");
OptionsMenu.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论