从另一个类中添加Swing组件 [英] Adding Swing components from another class
问题描述
我正在学习java,我正试图从另一个类中添加一个菜单栏到我的框架中(练习将代码分成多个类,以更好地组织程序)。
这里是我的代码示例:
public class MainApp {
public static void main (String [] args){
//创建窗口
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setSize(600,400);
//创建主面板
JPanel content = new JPanel();
content.setLayout(new BorderLayout());
frame.add(content);
//创建菜单栏
menubar menu = new menubar();
content.add(menu.menuBar(),BorderLayout.NORTH);
//其他东西...
} //结束main方法
} //结束MainApp类
和菜单类:
public class menubar {
public static void menuBar(){
JMenuBar menu = new JMenuBar();
JMenu文件= 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);
} //结束方法menuBar
} //结束类别menubar
使用eclipse,并在行中:
content.add(menu.menuBar(),BorderLayout.NORTH);
add加下划线,因此我无法编译代码。 / p>
我一直在寻找一种方法来解决这个问题,就我可以告诉这应该工作。
谢谢
Josh
请注意,您的 menuBar()方法是无效类型,因此没有返回任何值,您使用的内容(JPanel)的add()方法需要两个参数,即(JComponent类型 [注意JMenuBar是JComponent的子类] 布局目的] )
content.add(menu.menuBar(),BorderLayout.NORTH) ;
您的代码快速修复如下:
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;
} //结束方法menuBar
} //结束类别menubar
建议你使用继承的概念(扩展菜单类到JMenuBar,使你的类可以像JMenuBar)处理GUI的Java而不是依赖于组合的概念。你也可以关注以上关于设置JMenuBar的帖子:
frame.setJMenuBar(menu.menuBar());
如果你是Java编程的新手,你需要开始练习Java编码标准,特别是正确的命名的类和方法。类名的第一个字母应大写,而方法的名称应至少有一个动词。 :)
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).
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:
content.add(menu.menuBar(), BorderLayout.NORTH);
the "add" is underlined, and as a result I am not able to compile the code.
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])
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. You can follow also the above post with regards to setting the 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. 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. :)
这篇关于从另一个类中添加Swing组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!