[英]Is it possible to move JMenuBar without adding it in a JPanel
我在Java應用程序中遇到了JMenuBar位置的問題。
我實際上正在使用文章中的 ComponentResizer.java代碼。
調整大小的一切都很好,除了我的應用程序的北部區域(未修飾的JFrame)及其角落(北區),因為JMenuBar阻止我從該區域調整大小。
是否有一個解決方案或者可能是一個黑客將JMenuBar向下移動一點或者在北部地區啟用Resizing?
我還使用setJMenuBar()
方法將JMenuBar添加到我的應用程序的北部區域。
碼:
public class MyApp extends JFrame {
private MyApp frame;
private JMenuBar menuBar;
public static void main(String[] args) {
frame = new MyApp();
frame.setUndecorated(true);
frame.setVisible(true);
}
public MyApp(){
initComponents();
}
private void initComponents(){
setTitle("MediaForm");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 673, 482);
menuBar = new JMenuBar();
setJMenuBar(menuBar);
}
}
我沒有測試過這個,但是JRootPane
是一個JComponet
,它可能會向它添加一個EmptyBorder
,允許JMenuBar
被偏移。
如果做不到這一點,您可能需要實現自己的JRootPane
來管理JMenuBar
和內容窗格的布局
更新測試
public class TestMenuFrame extends JFrame {
public TestMenuFrame() throws HeadlessException {
setTitle("Test");
getRootPane().setBorder(new EmptyBorder(10, 10, 10, 10));
JMenuBar mb = new JMenuBar();
mb.add(new JMenu("Test"));
setJMenuBar(mb);
setSize(100, 100);
getContentPane().setBackground(Color.RED);
setLocationRelativeTo(null);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new TestMenuFrame().setVisible(true);
}
}
JMenuBar擴展了JComponent,因此您可以將它放在任何放置普通組件的位置。 請注意,如果將用戶置於意外位置,可能會讓用戶感到困惑。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.