我正在學習使用java編程的GUI,並嘗試修改現有程序以在框架頂部添加新的菜單欄。學習Java GUI -
主要方法如下。 MainPanel類擴展JPanel幷包含程序的主要組件(一個基本遊戲)。
public static void main(String[] args) {
JFrame frame = new JFrame("Sokuban");
MainPanel panel = new MainPanel();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
}
我不知道我是否應該添加一個新JPanel,將其添加到JFrame,然後內的添加按鈕?或者在現有面板或框架內創建一個JMenuBar,然後使用BorderLayout.NORTH進行排列?
只是玩弄的東西我對谷歌發現,我已經分開審訊以下片段(還沒有把所有的代碼):
JMenuBar menuBar = new JMenuBar();
frame.add(new Button("Button"), BorderLayout.SOUTH);
panel.BorderLayout.SOUTH;
JPanel frame2 = new JPanel();
window.add(frame2, BorderLayout.NORTH);
JButton b1 = new JButton();
frame2.setSize(500,500);
b1.setSize(400,400);
b1.setVisible(true);
b1.setText("Button");
frame2.add(b1);
frame2.setVisible(true);
我不知道我應該去哪個方向。任何指針非常感謝!
看看[如何使用菜單](https://docs.oracle.com/javase/tutorial/uiswing/components/menu.html)。 –