JMenuBarとJPanelがあります。JMenuBarをJPanelに追加したいと思います。どうすればいいですか?
5 に答える
JPanelにBorderLayoutを使用して、JMenuBarをパネルのNORTH領域に配置できます。
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.add(menubar, BorderLayout.NORTH);
JMenuBarはJComponentであり、他のJComponentと同じようにコンテナーに追加できます。
JMenuBarsは、setJMenuBarメソッドを使用してJFrameに設定されます。
それらの使用方法については、次のチュートリアルを参照してください。
http://download.oracle.com/javase/tutorial/uiswing/components/menu.html
私も試してみましたがJMenuItem
、Jmenu
にJmenuBar
追加されませんでしたJPanel
。JFrame
ただし、のレイアウトをnullとして宣言し、インスタンスで使用setBounds(x, y, width, height)
してJMenuBar
からメニューバーをに追加すると、その感覚を得ることができますJFrame
。
パネルにjDesktopPaneを配置してから、それにメニューバーを追加してみてください。以下の例ではタブ付きペインを使用していますが、パネルでも同じように機能するはずです。
JDesktopPane desktopPane = new JDesktopPane();
tabbedPane.addTab("New tab", null, desktopPane, null);
JMenuBar menuBar_1 = new JMenuBar();
menuBar_1.setBounds(0, 0, 441, 21);
desktopPane.add(menuBar_1);
別の解決策がありますが、NetBeansの「その他のコンポーネント」にJMenuBarを追加する必要があります(十分です)。JPanelを作成してから、外側のJPanel全体を埋める別のJPanelを内側に追加します(子と呼びます)。コントロールを子パネルに配置します。次に、JMenuBarを追加しますが、NetBeansはそれを「その他のコンポーネント」に配置します。ソースを編集し、「initComponents」を呼び出した後、ctorで次の関数を呼び出します。
public static void setJPanelMenuBar(JPanel parent, JPanel child, JMenuBar menuBar) {
parent.removeAll();
parent.setLayout(new BorderLayout());
JRootPane root = new JRootPane();
parent.add(root, BorderLayout.CENTER);
root.setJMenuBar(menuBar);
root.getContentPane().add(child);
parent.putClientProperty("root", root); //if you need later
}
たとえば、ctorは次のようになります。
public MyPanel() {
initComponents();
setJPanelMenuBar(this, child, myMenuBar);
}
私のために働きます。JInternalFrameのソースコードを見てアイデアを得ました。子JPanelをJRootPane()に置き換えてから、子をルートペインのコンテンツペインに配置するだけです。