有点新手问题,但我正在努力让这个JMenu栏出现在拆分窗格上方。有人可以帮我解释一下我做错了吗?据我了解,我添加了JMenu及其下拉菜单。
非常感谢任何解决方案并帮助理解我的问题。
以下是我的代码:
public class JavaAssignmentPanel {
JMenuBar setupMenu() {
JMenuBar menuBar = new JMenuBar(); //menubar
JMenu menu1 = new JMenu("Menu"); //menu
menuBar.add(menu1); //add menu to gui
JMenuItem menuItem1 = new JMenuItem("Item 1", KeyEvent.VK_1); //create drop down menu
menu1.add(menuItem1); //adds drop down menu to gui
//execute code when selected
menuItem1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// panel.showText("Example1 text - normally read from file");
}
});
return menuBar;
}
public static void main(String[] args) throws FileNotFoundException {
window window = new window();
}
private static class window extends JFrame {
public window() throws FileNotFoundException {
JPanel leftScrollPane = new JPanel();
JPanel rightPane = new JPanel();
JSplitPane splitPane;
this.setVisible(true);
this.setSize(400, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
splitPane = new JSplitPane();
splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
splitPane.setDividerSize(10);
splitPane.setDividerLocation(100);
splitPane.setLeftComponent(leftScrollPane);
splitPane.setRightComponent(rightPane);
splitPane.setOneTouchExpandable(true);
splitPane.setDividerLocation(200);
Dimension minimumSize = new Dimension(100, 50);
leftScrollPane.setSize(400, 400);
splitPane.setPreferredSize(new Dimension(400, 200));
splitPane.setLeftComponent(leftScrollPane);
splitPane.setRightComponent(rightPane);
this.add(splitPane);
}
}
}
答案 0 :(得分:1)
所以我可能只是完全失明,但我看不到你在班级的任何其他地方调用setupMenu方法。您需要告诉程序您正在向jframe添加菜单栏...
答案 1 :(得分:1)
您必须在JMenuBar
。
JFrame
请参阅代码,您可以将setupMenu
方法设置为静态并在窗口类中调用它,如下所示:
this.setJMenuBar(setupMenu());
代码:
public class JavaAssignmentPanel {
// making this method as static
static JMenuBar setupMenu() {
JMenuBar menuBar = new JMenuBar(); // menubar
JMenu menu1 = new JMenu("Menu"); // menu
menuBar.add(menu1); // add menu to gui
JMenuItem menuItem1 = new JMenuItem("Item 1", KeyEvent.VK_1); // create
// drop
// down
// menu
menu1.add(menuItem1); // adds drop down menu to gui
// execute code when selected
menuItem1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// panel.showText("Example1 text - normally read from file");
}
});
return menuBar;
}
public static void main(String[] args) throws FileNotFoundException {
window window = new window();
}
private static class window extends JFrame {
public window() throws FileNotFoundException {
JPanel leftScrollPane = new JPanel();
JPanel rightPane = new JPanel();
JSplitPane splitPane;
this.setVisible(true);
this.setSize(400, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// calling setupMenu method to set the JMenuBar in JFrame
this.setJMenuBar(setupMenu());
splitPane = new JSplitPane();
splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
splitPane.setDividerSize(10);
splitPane.setDividerLocation(100);
splitPane.setLeftComponent(leftScrollPane);
splitPane.setRightComponent(rightPane);
splitPane.setOneTouchExpandable(true);
splitPane.setDividerLocation(200);
Dimension minimumSize = new Dimension(100, 50);
leftScrollPane.setSize(400, 400);
splitPane.setPreferredSize(new Dimension(400, 200));
splitPane.setLeftComponent(leftScrollPane);
splitPane.setRightComponent(rightPane);
this.add(splitPane);
}
}}
答案 2 :(得分:1)
您定义方法setupMenu()
,返回JMenuBar
。但必须将该菜单栏添加到窗口中。
可以通过
来修复this.setJMenuBar(setupMenu());
作为window()
构造函数中的最后一个命令(即this.add(splitPane);
之后。
答案 3 :(得分:0)
我查看了您的代码并进行了以下更改:
为setupMenu()
添加了修饰符public static JMenuBar setupMenu() { ... }
通过调用JFrames构造函数中的this.setJMenuBar(setupMenu());
将您的MenuBar添加到JFrame
我还将JFrame的大小增加到1280x720以查看更改。
This是我所做的所有更改的课程。
我还鼓励你总是根据Java Code Conventions命名你的课程,这是在Camel Case中,以大写字母(Uppper Camel Case)开头。这使您的代码更易于阅读。