2011-12-04 41 views
2

的JPanel元素我試圖符合以下各種網格尺寸和使用GridBagLayout的

image pane

而實施的GridBagLayout。 GBL是我知道我可以得到不同大小元素的唯一方式。我知道我可以做一些像上面的圖片,但我不知道如何用GBL來做。我也準備好提出一個更好的主意。

+2

BTW - 對作出了巨大的截圖提示,請參閱(http://meta.stackexchange.com/questions/99734/how-do- [如何創建截圖?] i-create-a-screenshot-to-illustrations-a-post) –

回答

3

Player GUI

import java.awt.*; 
import javax.swing.*; 
import javax.swing.border.*; 

class PlayerGui { 

    public static void main(String[] args) { 
     JPanel gui = new JPanel(new BorderLayout()); 
     gui.setBorder(new BevelBorder(BevelBorder.RAISED)); 

     JPanel north = new JPanel(new GridLayout(0,1,5,5)); 
     north.add(new JLabel("Player Name", SwingConstants.CENTER)); 

     JPanel tfConstrain = new JPanel(new FlowLayout(FlowLayout.CENTER)); 
     tfConstrain.add(new JTextField(18)); 

     north.add(tfConstrain); 

     gui.add(north, BorderLayout.NORTH); 

     JPanel center = new JPanel(new GridLayout(0,1,10,10)); 
     center.add(new JButton("On This Machine")); 
     center.add(new JButton("Netowrk Based")); 
     center.add(new JButton("Main Menu")); 
     center.setBorder(new EmptyBorder(40,70,40,70)); 

     gui.add(center, BorderLayout.CENTER); 

     JOptionPane.showMessageDialog(null, gui); 
    } 
} 
+0

也請考慮使用垃圾桶概述的BoxLayout。在做佈局和使用嵌套佈局(將佈局放入其他佈局內)時,我傾向於使用常規「技巧包」。我很懶。 ;) –