【发布时间】:2013-03-14 13:20:22
【问题描述】:
我正在尝试使 JMenu 的弹出窗口居中,这样我就可以在 JPanel 上使用它并且它不会消失。这是一些演示我正在尝试做的代码:
import javax.swing.*;
public class Menu extends JMenu{
public static void main(String[] args) {
JFrame f = new JFrame("Menu Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
menuBar.add(new Menu());
JPanel background = new JPanel();
background.add(menuBar);
f.setContentPane(background);
f.setSize(250, 100);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public Menu() {
super("I'm a Menu");
add(new JMenuItem("Can This Popup be Centered?"));
add(new JMenuItem("Not To the Right?"));
}
}
这是当前的输出
这就是我想要(或接近)的东西
如果有比使用 JMenu 更好的方法,请告诉我。 谢谢。
【问题讨论】:
-
小心:非标准的用户界面容易混淆用户
-
@kleopatra 我正在做一个类似于 Microsoft Paint 的项目,我正在使用菜单显示一个用于设置宽度的弹出窗口,这很像 Paint 的做法。
标签: java swing alignment center jmenu