2016-04-16 11 views
-1

私は、私が帽子として作っている小さなゲーム用のGUIを作成しています。 GridBagLayoutを使用して1つのフレームに配置しようとしている4つのJPanelがあります。GridBagLayoutでJPanelsをフォーマットする

それは現在、次のようになります。 Current GUI Logic Error enter image description here

しかし、私はそれがこのと設計がより似ているしたいと思います:フレームのSorry for Bad Paint Skills enter image description here

コード:

public OverlordFrame() 
{ 
    buttonPanel = new ButtonPanel(); 
    invPanel = new InventoryPanel(); 
    statPanel = new PlayerStatsPanel(invPanel); 
    monstPanel = new MonsterPanel(); 

    this.setLayout(new GridBagLayout()); 
    GridBagConstraints c = new GridBagConstraints(); 

    c.ipadx = 10; 
    c.ipady = 50; 


    c.gridx = 0; 
    c.gridy = 0; 
    c.weightx = .1; 
    c.weighty = .4; 
    c.gridheight = 2; 
    c.fill = GridBagConstraints.VERTICAL; 
    c.anchor = GridBagConstraints.LINE_START; 
    this.add(invPanel, c); 


    c.gridx = 1; 
    c.gridy = 1; 
    c.weightx = .4; 
    c.weighty = .3; 
    c.gridwidth = 2; 
    c.gridheight = 0; 
    c.fill = GridBagConstraints.NONE; 
    c.anchor = GridBagConstraints.CENTER; 
    this.add(buttonPanel, c); 


    c.gridx = 2; 
    c.gridy = 0; 
    c.weightx = .1; 
    c.weighty = .4; 
    c.gridwidth = 0; 
    c.anchor = GridBagConstraints.LINE_END; 
    this.add(statPanel, c); 


    c.gridx = 1; 
    c.weightx = .4; 
    c.weighty = .4; 
    c.anchor = GridBagConstraints.CENTER; 
    this.add(monstPanel, c); 


    this.setSize(1440, 810); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    this.setVisible(true); 
} 

注:左の空白はInventoryPanelです。これは適切な場所にあるもので、ButtonPanel、wh ichは6つのボタンですが、悪い絵の絵のように行かなければなりません。下の4つのボタンの下にある白い部分は、モンスターの場所に移動し、右のJLabelsは右上に表示されます。助けてくれてありがとう

+0

あなたの2枚の画像は「ドンです同じように見える、それはリンゴのようなものです私にはオレンジ。どうか明らかにしてください。 –

+0

@HovercraftFullOfEelsそれは問題です、私はペイント写真のように見えるようにしようとしていますが、私は働くようにフォーマットを得ることができません –

答えて

1

私はあなたのコードを調べて、それがなぜ機能していないのか理解していません。私は本当に時間がありませんが、下の例のようなものは、

Example

public class TestPane extends JPanel { 

    public TestPane() { 
     setLayout(new GridBagLayout()); 
     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.gridx = 0; 
     gbc.gridy = 0; 
     gbc.gridheight = 2; 
     gbc.weightx = 0.3; 
     gbc.weighty = 1; 
     gbc.fill = GridBagConstraints.BOTH; 
     add(makePane(Color.BLUE), gbc); 

     gbc.gridx = 1; 
     gbc.gridy = 0; 
     gbc.gridheight = 1; 
     gbc.weightx = 0.4; 
     gbc.weighty = 0.5; 
     add(makePane(Color.MAGENTA), gbc); 

     gbc.gridx = 2; 
     gbc.weightx = 0.1; 
     add(makePane(Color.YELLOW), gbc); 

     gbc.gridx = 1; 
     gbc.gridy = 1; 
     gbc.gridwidth = 2; 
     gbc.weightx = 0.7; 
     gbc.weighty = 0.5; 
     gbc.fill = GridBagConstraints.BOTH; 
     add(makePane(Color.CYAN), gbc); 
    } 

    protected JPanel makePane(Color color) { 
     JPanel panel = new JPanel(); 
     panel.setBackground(color); 
     return panel; 
    } 

    @Override 
    public Dimension getPreferredSize() { 
     return new Dimension(200, 200); 
    } 

} 

を探しているが、私はおそらく同じ効果を生成するBorderLayoutの化合物のシリーズを使用するように誘惑されるだろうと言ったが、それは私だけ

+0

ありがとうございました! –

+0

@LarryKruzこれが役に立つとわかったら、回答を受け入れてください。 –