2017-09-15 10 views
-1

私はもともとメインスクリーンにBorderLayoutを使用していますが、ユーザーが開始すると、左と20の80%のゲーム画面になるようにGridBagLayoutに変換します右の%メニュー、スコアなど。GridBagLayoutはJPanelの位置を期待どおりに設定していません

| --- GameScreen --- |スコア|

しかし、スコア画面のサイズを変更できないか、まったく正しく表示されないため、この問題が発生することがあります。これは私が現在使っているコードです。

public Launcher(String title) { 
    super(title); 
    this.setResizable(false); 
    this.setDefaultCloseOperation(EXIT_ON_CLOSE); 
    cp = getContentPane(); 
    // cp.setSize((int)(1920*0.5), (int)(1080*0.5)); 
    cp.setLayout(new BorderLayout()); 
    panel = new JPanel(); 
    panel.setBackground(Color.YELLOW); 
    // panel.setSize(this.getWidth()/4, this.getHeight()); 
    panel.add(startButton); 
    panel.add(pauseButton); 
    panel.add(quitButton); 
    setSize(1920, 1100); 
    cp.add(panel, BorderLayout.SOUTH); 
    this.getContentPane().setBackground(Color.GREEN); 
    //ActionListenerStuff for buttons 
    . 
    . 
    . 

これは、updateGuiメソッドを呼び出すACtionListenerMethodです。

else if (e.getSource() == startButton) { 

       cp.remove(panel); 

       panel.setSize(1200, getHeight()); 

       state = STATE.RUNNING; 
       updateState(); 
       updateGui(); 
       createGameLoop(); 
    } 

これはレイアウトを変更してJPanelsを追加する方法です。

private void updateGui() { 
    cp.setLayout(new GridBagLayout()); 
    GridBagConstraints layoutConstraints = new GridBagConstraints(); 
    layoutConstraints.fill = GridBagConstraints.VERTICAL; 
    layoutConstraints.weighty = 1; 
    layoutConstraints.weightx = 0.8; 
    cp.add(house, layoutConstraints); 

    layoutConstraints.weighty = 1; 
    layoutConstraints.weightx = 0.2; 
    cp.add(panel, layoutConstraints); 
} 

それは家 "のJPanelが80%であることで終わるが、「パネル」変数は、前のBorderLayoutから下の境界にまだあります。しかし、なぜか分からない。

十分な情報であれば、私は完全にはわかりませんし、必要な情報を投稿することは喜んでできます。

編集:layoutConstraints.gridy = 1を追加しようとしました。しかしそれはどちらもうまくいかなかった。

private void updateGui() { 
    cp.setLayout(new GridBagLayout()); 
    GridBagConstraints layoutConstraints = new GridBagConstraints(); 
    layoutConstraints.fill = GridBagConstraints.VERTICAL; 
    layoutConstraints.weighty = 1; 
    layoutConstraints.weightx = 0.8; 
    house.setMinimumSize(new Dimension(1600, 1080)); 
    cp.add(house, layoutConstraints); 

    GridBagConstraints layoutConstraints2 = new GridBagConstraints(); 
    layoutConstraints2.fill = GridBagConstraints.VERTICAL; 
    layoutConstraints2.weighty = 1; 
    layoutConstraints2.weightx = 0.2; 
    layoutConstraints.gridy = 1; 
    cp.add(panel, layoutConstraints2); 
} 

private void updateState() { 
    if (roomGenerator == null) { 
     roomGenerator = new RoomGenerator(); 
    } 
    roomGenerator.updateRoom(); 

    Room room = roomGenerator.getRoom(); 
    house = (House) roomGenerator.getRoom(); 
    house.setSize(300, getHeight()); 
    this.getContentPane().add(house, BorderLayout.CENTER); 

    startButton.setVisible(false); 

    // this fuckery puts the buttons in the House JPanel 
    // house.add(pauseButton); 
    // house.add(quitButton); 
} 

これは私が現在持っているものです。これは表示されているものです。 The drawing should be the "80%" portion and the yellow should be the 20%.

編集2:Camickrの変更を使用して、私はthisになります。

これは正しい方向の一歩ですが、私が知る限り、家の大きさを正しく設定していません。

private void updateGui() { 

    cp.add(panel, BorderLayout.LINE_END); 
    panel.setVisible(true); 
    cp.revalidate(); 

} 

private void updateState() { 
    if (roomGenerator == null) { 
     roomGenerator = new RoomGenerator(); 
    } 
    roomGenerator.updateRoom(); 

    Room room = roomGenerator.getRoom(); 
    house = (House) roomGenerator.getRoom(); 
    house.setSize(1500, getHeight()); 
    this.getContentPane().add(house, BorderLayout.CENTER); 

    startButton.setVisible(false); 

} 
+0

cp.add(panel、layoutConstraints)の新しいGridBagConstraintsレイアウト制約を作成しようとしましたか?そこに同じ参照を置くのではなく? – fane89

+0

しました。私はより多くの情報で投稿を編集しようとしています。 – CJon428

+0

ここでhttps://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html同じ参照を使用しないことをお勧めします。 – fane89

答えて

0

誰かが不思議だったら私はそれを理解しました。私はこのようにしました...

cp.setLayout(new GridBagLayout()); 
    GridBagConstraints layoutConstraints = new GridBagConstraints(); 
    layoutConstraints.fill = GridBagConstraints.BOTH; 
    layoutConstraints.gridx = 0; 
    layoutConstraints.gridy = 0; 
    layoutConstraints.weighty = 1; 
    layoutConstraints.weightx = 0.8; 
    house.setMinimumSize(new Dimension(1500, 1080)); 
    cp.add(house, layoutConstraints); 

    GridBagConstraints layoutConstraints2 = new GridBagConstraints(); 
    layoutConstraints2.fill = GridBagConstraints.BOTH; 
    layoutConstraints2.weighty = 1; 
    layoutConstraints2.weightx = 0.2; 
    layoutConstraints2.gridx = 1; 
    layoutConstraints2.gridy = 0; 
    cp.add(dataPanel, layoutConstraints2); 

    dataPanel.setVisible(true); 
    cp.revalidate(); 
関連する問題