1
添加共享按鈕
大家好,我一直用這個作爲我的指導和我的代碼基礎,我一直在努力:跨越JTabbedPane的
我想要做什麼,是在所有窗格中添加一個共享按鈕。我不想爲每個按鈕聲明一個唯一的按鈕,但是要共享一個按鈕。我首先想到的是改變幀的BoxLayout,只是折騰按鈕將其添加的窗格框架後:
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BoxLayout(frame, BoxLayout.PAGE_AXIS));
//Add content to the window.
//frame.add(new GUI(), BorderLayout.CENTER);
frame.add(new GUI());
//setup Find button
//findButton.setSize(110,55);
findButton.setText("Find");
findButton.setVisible(true);
//add button to frame
frame.add(findButton);
不過,我得到一個運行時錯誤:BoxLayout的不能共享。所以現在我在這裏結束。當我看看爲什麼我收到這個錯誤時,有人能告訴我這是否正確嗎?
謝謝你,你的最終選擇做的伎倆。提到BoxLayout的原因是,因爲這是我試圖解決這個問題。它還展示了我試圖在物理上實現的佈局。我喜歡總是展示我所採取的路線。這可以導致我想要做的更好的想法和/或討論爲什麼它可能不是最好的方法。感謝3個選項。我要走3號門:D。 – whitewolfpgh
我仍然是gui等的新手,並且在一年之內沒有碰到過java。最後,有動力回到它。感謝關於構造函數和getContentPane()的提醒,這是我以前忽略的內容,並且確實需要提交內存。 – whitewolfpgh
@whitewolfpgh:不客氣,很高興幫助。 –