简体   繁体   English

如何在JFrameMenu子菜单中并行放置两个元素?

[英]How can I put two elements in parallel in a JFrameMenu submenu?

If I have the common JFrameMenu with "File","Edit","View"... let's say I want to have a sub-menu with a JTextField and a button to do something. 如果我具有带有“文件”,“编辑”,“视图”的通用JFrameMenu ...假设我想要一个带有JTextField的子菜单和一个用于执行某些操作的按钮。 How can I put both things in parallel? 如何将两者并行?

The only thing I've been able to do is put them one after another or put a separator. 我唯一能做的就是将它们一个接一个地放置或放置一个分隔符。

To explain better what I want I've drawn this, in red what I've been able to do just adding elements to the menu and in green what I want: 为了更好地解释我想要的内容,我用红色绘制了我只需要向菜单中添加元素的内容,用绿色绘制了我想要的内容:

Thank you. 谢谢。

You can, in fact, simply put a JPanel in the menu: 实际上,您可以将JPanel放入菜单中:

JTextField textField;
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);

JMenu fileMenu = new JMenu("File");
menuBar.add(fileMenu);

JMenuItem menuItem1 = new JMenuItem("Menu Item above");
fileMenu.add(menuItem1);

JSeparator separatorAbove = new JSeparator();
fileMenu.add(separatorAbove);
JPanel panel = new JPanel();
fileMenu.add(panel);

textField = new JTextField();
panel.add(textField);
textField.setColumns(10);

JButton button = new JButton("New button");
panel.add(button);

JSeparator separatorBelow = new JSeparator();
fileMenu.add(separatorBelow);

JMenuItem menuItem2 = new JMenuItem("Menu Item below");
fileMenu.add(menuItem2);

which produces this: 产生这个:

输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM