2013-08-07 41 views
0

我試圖讓有4個方格,使一個大正方形和一個按鈕下方是整個廣場延伸的佈局。搖擺:需要一個Foursquare和底部按鈕佈局

我試圖GridLayout,我創建了廣場完美,但按鈕沒有在屏幕拉伸。

我也試過GridBagLayout但有間距和方形小,這是不好的。

PS:按鈕被BUTTON1,按鈕2,BUTTON3,將Button4,爲方形,並開始用於底部按鈕。

而且我想方是400×400。

我與GridLayout代碼:

this.setLayout(new GridLayout(3,2)); 




     button1 = new JButton(); 
     //button1.setBackground(Color.green); 
     button1.setBounds(0, 0, 200, 200); 

     button2 = new JButton(); 
     //button2.setBounds(0, 200, 200, 200); 
     button2.setBackground(Color.red); 

     button3 = new JButton(); 
     //button3.setBackground(Color.yellow); 
     button3.setBounds(200, 0, 200, 200); 

     button4 = new JButton(); 
     //button4.setBounds(200, 200, 200, 200); 
     button4.setBackground(Color.blue); 

     start = new JButton("Start"); 
     //start.setBounds(300, 300, 100, 100); 

     this.add(button1); 
     this.add(button2); 
     this.add(button3); 
     this.add(button4); 
     this.add(start); 

回答

1

我不能問問題的評論,但我猜你想有一個正方形使用網格佈局,然後把由四個按鍵較長的按鈕伸出低於?

[1] [2]
[3] [4]
[開始]

對我來說,我將完成與此(喜歡這個?):

//assuming this code is in a class that extends JFrame 
this.setLayout(new BorderLayout()); 

JPanel pnlSquare = new JPanel(new GridLayout(2 , 2)); 
button1.setPreferredSize(new Dimension(200 , 200)); 
pnlSquare.add(button1); 
button2.setPreferredSize(new Dimension(200 , 200)); 
pnlSquare.add(button2); 
button3.setPreferredSize(new Dimension(200 , 200)); 
pnlSquare.add(button3); 
button4.setPreferredSize(new Dimension(200 , 200)); 
pnlSquare.add(button4); 
this.add(pnlSquare , BorderLayout.CENTER); 

this.add(start , BorderLayout.SOUTH); 

讓我知道這是否可以。另外,如果這是一個學校項目,你最好確保你可以解釋代碼,或者你可能因爲抄襲而陷入困境。使用組件佈局

+2

你可能想有一個讀[我應該避免使用setPreferred |最大|?在的Java Swing方法的minimumSize(http://stackoverflow.com/questions/7229226/我應該避免使用setpreferredmaximumminimumsize-methods-in-java-swi) – MadProgrammer

+0

有沒有辦法滿足廣場必須400×400的要求沒有它?感謝格式編輯和鏈接 - 學到了新東西! – user2570465

+1

是的,這太可怕了。基本上,你需要重寫'getPreferredSize'方法並返回你想要的結果。通常我會避免這樣的混亂,但這就是我:P – MadProgrammer

2

的Try ...的BorderLayoutGridLayout組合應該給你你需要的結果。

enter image description here

import java.awt.BorderLayout; 
import java.awt.EventQueue; 
import java.awt.GridBagLayout; 
import java.awt.GridLayout; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class FourSquare { 

    public static void main(String[] args) { 
     new FourSquare(); 
    } 

    public FourSquare() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JPanel squarePane = new JPanel(new GridLayout(2, 2)); 
       JPanel controlPane = new JPanel(new BorderLayout()); 

       squarePane.add(new JButton("1")); 
       squarePane.add(new JButton("2")); 
       squarePane.add(new JButton("3")); 
       squarePane.add(new JButton("4")); 

       controlPane.add(squarePane); 
       controlPane.add(new JButton("Bottom"), BorderLayout.SOUTH); 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(controlPane); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

}