Java 如何将JMenuBar添加到JTabbedPane?
我需要创建一个Java 如何将JMenuBar添加到JTabbedPane?,java,swing,user-interface,jtabbedpane,jmenubar,Java,Swing,User Interface,Jtabbedpane,Jmenubar,我需要创建一个JMenuBar,它将显示在JTabbedPane下,名为shapes。当我更改选项卡时,菜单栏将不会显示在其他选项卡中。你可以从图片中看出我的意思 我创建了一个菜单栏,并将其添加到JFrame,但这不是我的意思。如果你不明白一点,让我解释一下 public class Gui extends JPanel { JFrame frame; JTabbedPane tabbedPane; JMenuBar menuBar; JMenu createS
JMenuBar
,它将显示在JTabbedPane
下,名为shapes
。当我更改选项卡时,菜单栏将不会显示在其他选项卡中。你可以从图片中看出我的意思
我创建了一个菜单栏,并将其添加到JFrame
,但这不是我的意思。如果你不明白一点,让我解释一下
public class Gui extends JPanel {
JFrame frame;
JTabbedPane tabbedPane;
JMenuBar menuBar;
JMenu createShapes, display, help;
JMenuItem RandomShapes, Rectangle, Circle, Square;
public void createFrame() {
frame = new JFrame();
frame.setSize(1000, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.setVisible(true);
frame.setFocusable(false);
setVisible(true);
setSize(1000, 600);
}
public void createMenus() {
menuBar = new JMenuBar();
//frame.setJMenuBar(menuBar);
createShapes = new JMenu("CreateShapes");
menuBar.add(createShapes);
display = new JMenu("Display");
menuBar.add(display);
help = new JMenu("Help");
menuBar.add(help);
RandomShapes = new JMenuItem("Random Shapes");
createShapes.add(RandomShapes);
Rectangle = new JMenuItem("Rectangle");
createShapes.add(Rectangle);
Circle = new JMenuItem("Circle");
createShapes.add(Circle);
Square = new JMenuItem("Square");
createShapes.add(Square);
}
public Gui() {
createFrame();
createMenus();
frame.add(this);
//===frames part
tabbedPane = new JTabbedPane();
frame.getContentPane().add(tabbedPane);
JPanel gui2 = new JPanel();
tabbedPane.addTab("Shapes", this);
tabbedPane.addTab("Images", gui2);
//===endframespart
}
}
另一个选项卡是图像
形状选项卡图像
实际上,您不能将菜单栏设置为JTabbedPane。 您需要在JTabbedPane的一个选项卡中添加JInternalFrame,然后 您可以调用JInternalFrame的setJMenuBar 下面是一个简单的例子:
JInternalFrame jInternalFrame = new JInternalFrame();
jMenuBar = new javax.swing.JMenuBar();
jMenu1 = new JMenu("Save");
jMenu2 = new JMenu("Open");
jMenuBar.add(jMenu1);
jMenuBar.add(jMenu2);
jInternalFrame.setJMenuBar(jMenuBar);
tabbedPane.addTab("tab3", jInternalFrame);
1.错误:变量名必须以小写开头!2.错误:“createMenus”方法未在任何地方调用!更优雅的解决方案是在更改选项卡时替换JMenubar(连接到JFrame)。