java 将 JMenu 添加到 JPanel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14771437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding a JMenu to a JPanel
提问by paranoia25
I need to have a JMenu
(the one with the arrow on right which can display JMenuItem
) in a JPanel
. The problem is that when I do that the JMenu
is not activate on mouse rollover...
I don't know how to do that and if it's possible.
我需要有一个JMenu
(具有在其上可以显示右侧的箭头JMenuItem
a)中JPanel
。问题是,当我这样做时,JMenu
鼠标悬停时不会激活......我不知道如何做到这一点,如果可能的话。
回答by Guillaume Polet
If you wrap your JMenu
in a JMenuBar
, it works as expected.
如果你将你的包裹JMenu
在 a 中JMenuBar
,它会按预期工作。
Here is a demo example:
这是一个演示示例:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
public class TestMenus {
private JMenuBar createMenuBar(String name, int depth) {
JMenuBar bar = new JMenuBar();
bar.add(createMenu(name, depth));
return bar;
}
private JMenu createMenu(String name, int depth) {
JMenu menu = new JMenu(name);
for (int i = 0; i < 5; i++) {
if (depth > 0) {
menu.add(createMenu("sub-" + name, depth - 1));
}
}
for (int i = 0; i < 5; i++) {
menu.add(createMenuItem("Menu item " + (i + 1)));
}
return menu;
}
private JMenuItem createMenuItem(String name) {
final JMenuItem jMenuItem = new JMenuItem(name);
jMenuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(jMenuItem, "Successfully pressed a menu item");
}
});
return jMenuItem;
}
protected void initUI() {
JFrame frame = new JFrame(TestMenus.class.getSimpleName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMenuBar("Root menu", 3));
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TestMenus().initUI();
}
});
}
}
An the result:
结果:
回答by Michael Peterson
Here is another way to solve this which I think is closer to what you want. It involves extending JMenuBar
to give it the look of a JMenu
Object.
这是解决这个问题的另一种方法,我认为它更接近你想要的。它涉及扩展JMenuBar
以赋予它JMenu
对象的外观。
The class contains a JMenu
Object called menu. The add methods are overridden so you are adding to menu instead of the JMenuBar
(You may have to override a few more add methods to make this perfect).
该类包含一个JMenu
名为 menu的对象。add 方法被覆盖,因此您将添加到菜单而不是JMenuBar
(您可能需要覆盖更多的 add 方法以使其完美)。
There are a few options with painting. I wasn't sure if you wanted the button style look of the JMenuBar
, so I included a few comments on some options to customize that, as well as the underline look of the JMenuBar
.
绘画有几种选择。我不知道,如果你想要的按钮样式的样子JMenuBar
,所以我包括一些选项的一些意见,以自定义,以及的下划线的样子JMenuBar
。
Here is the result of the button look with no border:
这是没有边框的按钮外观的结果:
Here is the result with no button look and no border:
这是没有按钮外观和边框的结果:
import java.awt.*;
import javax.swing.*;
public class JPanelMenu extends JMenuBar{
public static void main(String[] args) {
JFrame f = new JFrame("Menu Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenu jmenu = new JMenu("J Menu");
jmenu.add(new JMenuItem("Menu Item"));
JPanelMenu m = new JPanelMenu("Menu");
m.add(jmenu);
m.add(new JMenuItem("Menu Item 1"));
m.add(new JMenuItem("Menu Item 2"));
JPanel background = new JPanel();
background.add(m);
f.setContentPane(background);
f.pack();
f.setVisible(true);
}
//This is the JMenu that is shown
private JMenu menu;
public JPanelMenu(String title) {
super();
menu = new JMenu(title);
super.add(menu);
}
@Override
public Component add(Component comp) {
//You add the the JMenu instead of the JMenuBar
return menu.add(comp);
}
@Override
public JMenu add(JMenu c) {
//You add the the JMenu instead of the JMenuBar
return (JMenu) menu.add(c);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
//Include these two lines to remove the button look
//Or remove this method to keep the button look
//g.setColor(getBackground());
//g.fillRect(0, 0, getWidth(), getHeight());
}
@Override
protected void paintBorder(Graphics g) {
//Remove this line to remove the underline look
//when you remove the button look
//An alternative is to you setBorderPainted(false);
//when you create the object or in the constructor
//Or remove this method to keep the border
//super.paintBorder(g);
}
}
回答by Vuk Vasi?
You must pass a BorderLayoutin JPanel layout then you can add menu bar in panel:
您必须在 JPanel 布局中传递一个BorderLayout然后您可以在面板中添加菜单栏:
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.add(menubar, BorderLayout.NORTH);