设置 JMenu,我正在尝试在布局框顶部创建一个不是组合框的菜单



这就是我过去只设置菜单的方法,我想在完成应用程序的其余部分之前弄清楚这一点。

import java.awt.*;
import java.awt.Event.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
import javax.swing.event.*;
public class Layout_Manager extends JPanel{
//instance variables for panels
private JPanel panel1;
private JPanel panel2;
private JPanel panel3;
private JPanel panel4;
private JPanel panel5;
private JPanel panel6;
//instance variables for help and shapes tab
private JMenu shapesMenu;
private JMenu helpMenu;
private JMenuBar Bar1;
private JMenuItem menuItem;
private JMenuItem CircleMenuItem;
private JMenuItem RectangleMenuItem;
private JMenuItem SquareMenuItem;
private JMenuItem LineMenuItem;
Layout_Manager(){
//set up panel
panel1 =new JPanel(new FlowLayout(FlowLayout.LEFT));
//Create MenuBar and add menu to it
Bar1 = new JMenuBar();
shapesMenu = new JMenu("Shapes");
CircleMenuItem = new JMenuItem("Circle");
CircleMenuItem.setAccelerator(KeyStroke.getKeyStroke(ActionEvent.CTRL_MASK,KeyEvent.VK_C));
RectangleMenuItem = new JMenuItem("Rectangle");
RectangleMenuItem.setAccelerator(KeyStroke.getKeyStroke(ActionEvent.CTRL_MASK, KeyEvent.VK_R)); 
SquareMenuItem = new JMenuItem("Square");
SquareMenuItem.setAccelerator(KeyStroke.getKeyStroke(ActionEvent.CTRL_MASK,KeyEvent.VK_S));
LineMenuItem = new JMenuItem("Line");
LineMenuItem.setAccelerator(KeyStroke.getKeyStroke(ActionEvent.CTRL_MASK,KeyEvent.VK_L));
shapesMenu.add(CircleMenuItem,RectangleMenuItem);
shapesMenu.add(SquareMenuItem,LineMenuItem);
JMenu add = Bar1.add(shapesMenu);
panel1.add(Bar1);
panel2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
panel3= new JPanel(new FlowLayout(FlowLayout.LEFT));
panel4= new JPanel(new FlowLayout(FlowLayout.LEFT));
panel5 = new JPanel(new GridLayout(5,1));
panel5.add(panel1);
panel5.add(panel2);
panel5.add(panel3);
panel5.add(panel4);
add(panel5);
}           
}

我添加了其他面板,目的是让其他面板帮助塑造布局管理器,但我不太明白为什么它不创建菜单????

这也是我的测试人员,我遵循了以前使用过的行之有效的例子,这就是为什么这会让我陷入循环。


import javax.swing.*;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author ethubbard
*/
public class GUI_ShapeTester {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
JFrame frame = new JFrame("Layout Manager");
Layout_Manager layout = new Layout_Manager();
frame.add(layout);
frame.pack();
frame.setVisible(true);
frame.setLocation(null);
}
}

我想这就是我创建GUI所要做的,并且只有顶部的菜单栏,很明显我缺少了另一个组件,但我不知道是什么。

您没有正确构建菜单/菜单项。此外,应使用setJMenuBar()方法将JMenuBar添加到JFrame。请参阅以下Layout_Manager构造函数的更正代码:

Layout_Manager(){
//set up panel
panel1 =new JPanel(new FlowLayout(FlowLayout.LEFT));
//Create MenuBar and add menu to it
Bar1 = new JMenuBar();
shapesMenu = new JMenu("Shapes");
CircleMenuItem = new JMenuItem("Circle");
CircleMenuItem.setAccelerator(KeyStroke.getKeyStroke(ActionEvent.CTRL_MASK,KeyEvent.VK_C));
RectangleMenuItem = new JMenuItem("Rectangle");
RectangleMenuItem.setAccelerator(KeyStroke.getKeyStroke(ActionEvent.CTRL_MASK, KeyEvent.VK_R)); 
SquareMenuItem = new JMenuItem("Square");
SquareMenuItem.setAccelerator(KeyStroke.getKeyStroke(ActionEvent.CTRL_MASK,KeyEvent.VK_S));
LineMenuItem = new JMenuItem("Line");
LineMenuItem.setAccelerator(KeyStroke.getKeyStroke(ActionEvent.CTRL_MASK,KeyEvent.VK_L));
shapesMenu.add(CircleMenuItem);     // corrected
shapesMenu.add(RectangleMenuItem);  // corrected
shapesMenu.add(SquareMenuItem);     // corrected
shapesMenu.add(LineMenuItem);       // corrected
Bar1.add(shapesMenu);               // corrected
panel2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
panel3= new JPanel(new FlowLayout(FlowLayout.LEFT));
panel4= new JPanel(new FlowLayout(FlowLayout.LEFT));
panel5 = new JPanel(new GridLayout(5,1));
panel5.add(panel1);
panel5.add(panel2);
panel5.add(panel3);
panel5.add(panel4);        
add(panel5);      
JFrame frame = new JFrame("Layout Manager");   //create JFrame
frame.add(this);
frame.setJMenuBar(Bar1);                       // add menu bar built earlier
frame.pack();
frame.setVisible(true);
}  

然后,从GUI_ShapeTester类中,您可以简单地创建一个Layout_Manager对象,例如:

public static void main(String[] args) {
new Layout_Manager();
} 

最新更新