我需要在JPanel
中有一个JMenu
(带有箭头的那个,它可以显示JMenuItem
)。问题是,当我这样做时JMenu
没有激活鼠标滚动... 我不知道如何做到这一点,如果这是可能的。将JMenu添加到JPanel
回答
你必须传递的JPanel布局BorderLayout那么您可以在面板中添加菜单栏:
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.add(menubar, BorderLayout.NORTH);
那么对JMenu的改变呢?用BorderLayout替换FlowLayout会影响大小和位置,但不会影响行为。 – 2013-02-08 11:50:24
我试过这个例子,它的工作原理和正常行为。 – 2013-02-08 11:58:47
是什么例子? 3行代码不是一个例子。 – 2013-02-08 12:16:54
如果你包裹在JMenuBar
您JMenu
,它按预期工作。
这里是一个演示的例子:
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();
}
});
}
}
的结果:
嗨!这不是我想要的样子。我希望JMenu直接出现在面板中(而不是JMenuBar)。 – paranoia25 2013-02-08 12:44:52
@ paranoia25你的问题不清楚。如果你已经放了一个[SSCCE](http://sscce.org),你会直接得到更好的答案。现在关于你的问题,我不会直接将'JMenu'添加到'JPanel'中,而是使用'ActionListener'来添加'JButton',以打开'JPopupMenu'。为了让'JButton'看起来像一个'JMenu',你可能需要做一些定制和访问L&F的默认值。 – 2013-02-08 12:55:44
+1''JToolBar'可能是'JMenuBar'的一个吸引人的备选'容器',如[这里](http://stackoverflow.com/a/14630345/230513)所示。 – trashgod 2013-02-08 15:15:25
这里是另一种方式来解决这个问题,我认为是更接近你想要什么。它涉及扩展JMenuBar
以使它看起来像一个JMenu
对象。
该类包含一个名为menu的对象JMenu
。添加方法被覆盖,所以你要添加到菜单而不是JMenuBar
(你可能需要重写一些更多的添加方法来使这个完美)。
有几个选择与绘画。我不知道你是否想要JMenuBar
的按钮样式外观,所以我包含了一些选项的一些评论来定制,以及JMenuBar
的下划线外观。
这里是按钮的结果看,无边界:
这里是没有按钮的外观和无边框结果:
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);
}
}
嗨!这是更接近的解决方案!但这仍然不是我正在寻找的。我想直接在JPanel中使用JMenu。在您的解决方案中,JMenu仍然位于JMenuBar中,用户必须通过JMenuBar才能看到并访问JMenu。 – paranoia25 2013-03-27 07:08:49
@ paranoia25据我所知,没有办法做到这一点,因为你需要一个JMenuBar的工作。这个解决方案仍然使用JMenuBar,但是你可以通过调用Paint方法使其看起来不存在。 – 2013-04-07 02:08:39
- 1. Java:如何将JMenu添加到JPanel或创建下拉按钮?
- 2. 将JApplet添加到JPanel
- 3. 将jpanel添加到jframe
- 4. 将JList/JScrollPane添加到JPanel
- 5. 将图形添加到JPanel
- 6. 将组件添加到JPanel
- 7. 将图像添加到JPanel
- 8. 将JLayeredPane添加到JPanel
- 9. 将组件添加到JPanel
- 10. 将JPanel添加到JList?
- 11. 将JMenuBar添加到JPanel?
- 12. 将JButton添加到JPanel
- 13. 将JPanel添加到Canvas
- 14. 将JPanel添加到另一个JPanel添加填充
- 15. 添加快捷方式到JMenu的
- 16. 试图将多个JPanel添加到JFrame
- 17. 将JPanel数组添加到容器
- 18. 将NetBeans Designer中的JFrame添加到JPanel
- 19. 将JScrollPane添加到JPanel中的JTextArea
- 20. 将组件添加到jPanel java
- 21. 将JPanel实例动态添加到JScrollPane
- 22. NetBeans:如何将ScrollBar添加到JPanel
- 23. 如何将JTable添加到JPanel
- 24. 将JPanel添加到JApplet的BorderLayout中
- 25. 无法将ActionListener添加到JPanel?
- 26. 如何将JColorChooser添加到contentpane/Jpanel?
- 27. 将多个jPanel添加到jFrame中
- 28. 将JLabel添加到JPanel的问题
- 29. 将JPanel添加到按钮上点击
- 30. 如何将JPanel添加到JFrame十次
你加'JMenu's到一个'JMenuBar',并且调用'JFrame'的'setJMenuBar()'方法 - 我不认为你可以将它添加到'JPanel'。并且确保你的'JMenu'充满了物品! – moonwave99 2013-02-08 11:25:58
嗨,我知道这个解决方案,但它不是我想要的。我真的想要一个JMenu直接进入JPanel ......这是问题所在。 – paranoia25 2013-02-08 12:46:41
然后考虑在JInternalFrames中使用'JDesktopPane',或者定义你自己的'JComponent'。 – moonwave99 2013-02-08 13:08:31