2015-04-08 188 views
1

所以我一直在尋找其他解決方案來解決這個問題,但似乎他們都沒有幫助。有人能幫我嗎?java - 在位置設置按鈕和TextAreas

代碼:

public static void main(String[] args) { 

    JFrame frame = new JFrame(); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    // Container cont = frame.getContentPane(); 
    JPanel buttonpanel = new JPanel(); 
    JButton[] button = new JButton[12]; 
    JTextArea score1, score2; 
    score1 = new JTextArea(100, 200); 
    score2 = new JTextArea(100, 200); 

    frame.setSize(800, 600); 
    frame.setVisible(true); 
    score1.setLayout(null); 
    score1.setBackground(Color.BLUE); 
    score2.setLayout(null); 
    score2.setBackground(Color.RED); 

    score1.setLocation(0, 100); 
    score2.setLocation(700, 100); 

    frame.add(score1); 

    for (int i = 0; i < button.length/2; i++) { 
     button[i].setBounds(100 * (i + 1), 100, 100, 100); 
     button[i].setBackground(Color.GRAY); 
     buttonpanel.add(button[i]); 
    } 

    frame.add(buttonpanel); 

    frame.add(score2); 

    // frame.add(panel); 
} 

這給了我完全窗口藍色。

+1

此外,如果您嘗試配置GUI,請顯示一張圖片,顯示您想要實現的內容。 99%的時間,最好的解決方案是不要像你這樣做。不要使用絕對定位,因爲在99%的時間內製作GUI是一種可怕的方式。 –

+0

雖然null佈局和'setBounds()'對於Swing新手來說似乎是創建複雜GUI的最簡單和最好的方式,但是更多的Swing GUI會創建使用它們時遇到的更嚴重的困難。它們不會在GUI大小調整時調整組件的大小,它們是增強或維護的皇室女巫,當它們放在滾動窗格中時它們會完全失敗,在所有平臺或屏幕分辨率與原始視圖不同時,它們看起來會非常糟糕。 –

回答

3

您看到全部爲藍色,因爲您正在向JFrame的contentPane添加score1,一個全藍色的JTextArea,一個使用BorderLayout的容器,這使得JTextArea填充contentPane,只剩下藍色。由於使用了一個填充了空值的JButton數組而導致的NullPointerException,因此不會添加任何其他內容。一旦你解決了這個問題(你從來沒有告訴過我們這個問題),由於同樣的問題,你只會看到紅色。

您無疑已閱讀的最佳解決方案是使用佈局管理器來創建GUI的最佳優勢。雖然null佈局和setBounds()對於Swing新手來說似乎是創建複雜GUI的最簡單和最好的方法,但更多的Swing GUI會創建使用它們時遇到的更嚴重的困難。它們不會在GUI大小調整時調整組件的大小,它們是增強或維護的皇室女巫,當它們放在滾動窗格中時它們會完全失敗,在所有平臺或屏幕分辨率與原始視圖不同時,它們看起來會非常糟糕。

其他問題:

  • 你對你的JFrame 調用setVisible(true)前添加任何東西它是倒退。您想在之後添加所有內容,使其在之後生效,以便它在顯示時顯示在JFrame中。
  • 您正在設置JTextArea的佈局,因爲您永遠不想將這些佈局用作其他組件的容器。
  • 也明白,這不是做你認爲它在做什麼:JTextArea(100, 200);。你沒有創建一個100×200像素的JTextArea,而是100個×200 這是一個令人驚訝的巨大的 JTextArea。在使用不熟悉的組件時,您將需要閱讀文檔和API。
  • 您聲明,"So I've been looking at other solutions to this problem, but it seems that none of them help at all."可能「其他解決方案」建議您使用佈局管理器,而不是說他們根本沒有「幫助」,您應該努力學習如何使用這些工具,因爲其他人解決方案正確。
  • 是的,你正在嘗試在尚未創建的JButton數組中使用JButton。理解一個引用類型的數組,例如JButton數組最初只填充空引用,類似於空的紙箱蛋。就像在你把雞蛋放進去之前你不能使用紙盒中的任何雞蛋一樣,你不能在你的雞蛋中放入JButtons,在for循環中,你通過數組迭代,在第一行創建的每個JButton的項目:button[i] = new JButton("Something");

你可以找到鏈接到鞦韆教程和這裏其他Swing資源:Swing Info

例如,你能告訴我哪一個更容易調試,你的代碼,或此代碼:

import java.awt.BorderLayout; 
import java.awt.GridLayout; 
import javax.swing.*; 

public class Foo2 extends JPanel { 
    private static final String[] BUTTON_TEXTS = { "A", "B", "C", "D", "E", "F", 
     "G", "H", "I", "J", "K", "l" }; 
    private static final int GAP = 3; 
    private JTextArea textArea1 = new JTextArea(20, 30); 
    private JTextArea textArea2 = new JTextArea(20, 30); 

    public Foo2() { 
     JPanel textAreaGrid = new JPanel(new GridLayout(1, 0, GAP, GAP)); // gridlayout 1 row 
     textAreaGrid.add(new JScrollPane(textArea1)); 
     textAreaGrid.add(new JScrollPane(textArea2)); 

     JPanel buttonPanel = new JPanel(new GridLayout(2, 0, GAP, GAP)); // 2 rows 
     for (String btnText : BUTTON_TEXTS) { 
     buttonPanel.add(new JButton(btnText)); 
     } 

     setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP)); 
     setLayout(new BorderLayout(GAP, GAP)); // main GUI uses border layout 
     add(textAreaGrid, BorderLayout.CENTER); 
     add(buttonPanel, BorderLayout.PAGE_END); 
    } 

    private static void createAndShowGui() { 
     Foo2 mainPanel = new Foo2(); 

     JFrame frame = new JFrame("Foo2"); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     frame.getContentPane().add(mainPanel); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGui(); 
     } 
     }); 
    } 
} 

其中爲顯示:

enter image description here

+0

對不起,我對Java還不是很瞭解。這就像我第二次和我現在的第一次3年。所以我可以理解我的錯誤,並去解決很多事情(謝謝你)。所以我在我想讓窗口類似於遊戲「Mancala」的那個部分。它不是使用坑,而是按鈕。我有側坑或目標顯示出來,但按鈕沒有顯示在文本框之間。請提示? – Xephos

+0

Nvmd,我設法不使用SetLayout(null),因爲它以某種方式阻止我組織?無論如何,現在如何把按鈕和文本區域放在另一個textarea下面? – Xephos

+0

我修好了!謝謝你的幫助! – Xephos