[英]Adding Swing components from another class
I am learning java, and I am trying to add a menu bar to my frame from another class (practicing dividing code into multiple classes to better organize the program). 我正在学习java,我正在尝试从另一个类添加一个菜单栏到我的框架(练习将代码分成多个类来更好地组织程序)。
Here is a sample of my code: 以下是我的代码示例:
public class MainApp {
public static void main(String[] args) {
// Create window
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setSize(600, 400);
// Create main panel
JPanel content = new JPanel();
content.setLayout(new BorderLayout());
frame.add(content);
//Create menu bar
menubar menu = new menubar();
content.add(menu.menuBar(), BorderLayout.NORTH);
//Other stuff...
} // Ends main method
} // Ends MainApp class
And the menubar class: 菜单栏类:
public class menubar {
public static void menuBar(){
JMenuBar menu = new JMenuBar();
JMenu file = new JMenu("File");
JMenuItem clear = new JMenuItem("New");
JMenuItem exit = new JMenuItem("Exit");
JMenu help = new JMenu("Help");
JMenuItem about = new JMenuItem("About");
JMenuItem instructions = new JMenuItem("Instructions");
} // Ends method menuBar
} // Ends class menubar
I use eclipse, and in the line: 我使用eclipse,并在行中:
content.add(menu.menuBar(), BorderLayout.NORTH);
the "add" is underlined, and as a result I am not able to compile the code. “add”加下划线,因此我无法编译代码。
I have been searching for a way to resolve this, and as far as I can tell this should work. 我一直在寻找解决这个问题的方法,据我所知,这应该有效。
Any help is appreciated. 任何帮助表示赞赏。
Thank you 谢谢
Josh 玩笑
Note that your menuBar() method is a void type hence no value was returned, while the add() method of content (JPanel) you used requires two parameters which are (JComponent type [Note that JMenuBar is a subclass of JComponent] , int [For Layouting Purposes] ) 请注意,您的menuBar()方法是一个void类型,因此没有返回任何值,而您使用的内容的add()方法(JPanel)需要两个参数(JComponent类型[注意JMenuBar是JComponent的子类] ,int [用于布局目的] )
content.add(menu.menuBar(), BorderLayout.NORTH);
Well a quick fix of your code is below: 快速修复您的代码如下:
public class menubar {
public static JMenuBar menuBar(){
JMenuBar menu = new JMenuBar();
JMenu file = new JMenu("File");
JMenuItem clear = new JMenuItem("New");
JMenuItem exit = new JMenuItem("Exit");
JMenu help = new JMenu("Help");
JMenuItem about = new JMenuItem("About");
JMenuItem instructions = new JMenuItem("Instructions");
return menu;
} // Ends method menuBar
} // Ends class menubar
My advice for you is to use the concept of inheritance (Extending menubar class to JMenuBar so that your class can act like JMenuBar) when dealing with GUI in Java rather than depending on the concept of composition. 我的建议是在Java中处理GUI而不是依赖于组合的概念时,使用继承的概念(将菜单栏类扩展到JMenuBar,以便您的类可以像JMenuBar一样工作)。 You can follow also the above post with regards to setting the JMenuBar:
关于设置JMenuBar,您也可以关注上面的帖子:
frame.setJMenuBar(menu.menuBar());
If you are novice in Java Programming, you need to start practicing the Java Coding standards especially the proper naming of the Class and methods. 如果您是Java编程的新手,则需要开始练习Java编码标准,尤其是对类和方法的正确命名。 The first letter of the Class's name should be capitalized while your names of the method should have at least a verb on it.
Class的名字的第一个字母应该大写,而你的方法名称至少应该有一个动词。 :)
:)
The method menuBar
has a void
return type so is not applicable for the add
method. 方法
menuBar
具有void
返回类型,因此不适用于add
方法。 so you would need 所以你需要
content.add(menubar.menuBar(), BorderLayout.NORTH);
while returning menu
from the menuBar
method 从
menuBar
方法返回menu
Aside: setJMenuBar
is used to set a JMenuBar
for a JFrame
. 另外:
setJMenuBar
用于为JFrame
设置JMenuBar
。
frame.setJMenuBar(menu.menuBar());
Instead of: 代替:
content.add(menu.menuBar(), BorderLayout.NORTH);
I think you mean this: 我想你的意思是:
content.add(menubar.menuBar(), BorderLayout.NORTH);
...but that still won't work because the return type of this method is void
. ...但是仍然无法正常工作,因为此方法的返回类型
void
。 It needs to be JMenuBar
. 它需要是
JMenuBar
。
I think you: 我觉得你:
new menubar()
if menubar.menubar()
is a static
function menubar.menubar()
是static
函数,则不想创建new menubar()
public static JMenuBar menubar() { ... }
, not void public static JMenuBar menubar() { ... }
,而不是无效 menubar.menubar()
to return menu
otherwise, that method was just an expensive no-op menubar.menubar()
return menu
否则该方法只是一个昂贵的无操作 content.add(menu.menuBar(), ...)
is underlined because you can't use a void
method as an argument to another method. content.add(menu.menuBar(), ...)
下划线,因为您不能将void
方法用作另一个方法的参数。
Also, you should always refer to a static field or method using the class, not an instance. 此外,您应始终使用类而不是实例引用静态字段或方法。 And class names should start with a capital letter (
Menu
). 类名应以大写字母(
Menu
)开头。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.