2015-09-19 52 views
0

我正在構建一個小遊戲/練習的界面。在這個界面中,我得到了一個頂部部分,女巫是一個JPanel,它佔用了窗口的寬度和一定的高度。在它的正下方是另一個部分(JPanel再次),假設在左邊對齊,並在第一部分下面是Wright。如何在JFrame中正確對齊兩個JPanel

我一直在努力使界面看起來像我想要的。我嘗試了兩件事,但失敗了:

第一個是我有GameView類,它擴展了JFrame,我爲這兩個部分創建了一個JPanel,並直接將它們添加到JFrame中,但似乎他們只是結束了彼此。黑色部分是第一位的,紅色是第二個:

The result of my first method

第二件事我想是把這兩個JPanel的所謂容器中的其它的JPanel裏面,但還是不明白我的想。第一部分是完美的,但第二個要堅持走了,我想有兩個部分之間沒有空格:到

The result of my second method

我如何能堅持第二部分(紅色)離開,兩部分之間沒有空間?這裏是我的課程的代碼:

package game;

import java.awt.Color; 

import javax.swing.GroupLayout; 
import javax.swing.GroupLayout.Alignment; 
import javax.swing.JButton; 
import javax.swing.JCheckBox; 
import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

    public class GameView extends JFrame //implements MouseListener 
    { 
     private GameNumView numberPanel; 
     private JLabel butLabel; 
     private JLabel progresLabel; 
     private JButton nextButton; 
     private JButton giveUpButton; 
     private JButton resetButton; 
     private JCheckBox findMeanCheckBox; 
     private JCheckBox noiseCheckBox; 

     public GameView() 
     { 
      initUI(); 
     } 

     public void initUI() 
     { 
      setTitle("Sommurai"); 

      setSize(800, 350); 

      setLocationRelativeTo(null); 

      butLabel = new JLabel("97"); 
      progresLabel = new JLabel("Somme: 90 (2)"); 

      nextButton = new JButton("NEXT"); 
      giveUpButton = new JButton("GIVE UP"); 
      resetButton = new JButton("RESET"); 

      findMeanCheckBox = new JCheckBox("Find Mean"); 
      noiseCheckBox = new JCheckBox("Noise"); 

      createLayout(butLabel, progresLabel, nextButton, giveUpButton, resetButton, findMeanCheckBox, noiseCheckBox); 

      setDefaultCloseOperation(EXIT_ON_CLOSE); 
     } 

     private void createLayout(JComponent... arg) 
     { 
      JPanel container = new JPanel(); 

      numberPanel = new GameNumView(800, 120); 

      JPanel buttonsPanel = new JPanel(); 
      GroupLayout gl = new GroupLayout(buttonsPanel); 
      buttonsPanel.setLayout(gl); 

      gl.setAutoCreateContainerGaps(true); 

      GroupLayout.SequentialGroup hGroup = gl.createSequentialGroup(); 

      GroupLayout.SequentialGroup vGroup = gl.createSequentialGroup(); 

      hGroup.addGroup(gl.createParallelGroup() 
        .addComponent(arg[0]) 
        .addComponent(arg[1]) 
        .addComponent(arg[2]) 
        .addComponent(arg[3]) 
        .addComponent(arg[4]) 
        .addComponent(arg[5]) 
        .addComponent(arg[6])); 

      gl.setHorizontalGroup(hGroup); 

      vGroup.addGroup(gl.createParallelGroup(Alignment.BASELINE) 
        .addComponent(arg[0])); 
      vGroup.addGroup(gl.createParallelGroup(Alignment.BASELINE) 
        .addComponent(arg[1])); 
      vGroup.addGroup(gl.createParallelGroup(Alignment.BASELINE) 
        .addComponent(arg[2])); 
      vGroup.addGroup(gl.createParallelGroup(Alignment.BASELINE) 
        .addComponent(arg[3])); 
      vGroup.addGroup(gl.createParallelGroup(Alignment.BASELINE) 
        .addComponent(arg[4])); 
      vGroup.addGroup(gl.createParallelGroup(Alignment.BASELINE) 
        .addComponent(arg[5])); 
      vGroup.addGroup(gl.createParallelGroup(Alignment.BASELINE) 
        .addComponent(arg[6])); 

      gl.setVerticalGroup(vGroup); 

    buttonsPanel.setBackground(Color.RED);// 

      container.add(numberPanel); 
      container.add(buttonsPanel); 
      add(container); 
     } 
    } 

GameNumView是其它類,它是一個JPanel

+1

[如何使用網格佈局(https://docs.oracle.com/javase/tutorial/uiswing/layout/grid.html ),[如何使用GridBagLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html)或[如何使用BorderLayout](https://docs.oracle.com/javase /tutorial/uiswing/layout/border.html) – MadProgrammer

回答

-1

我同意使用BorderLayout的,但設置的BorderLayout的框架。

this.setLayout(new BorderLayout()); 

然後在北部和中部地區設置兩個Jpanel

this.add(panel1, BorderLayout.NORTH); 
this.add(panel2, BorderLayout.CENTER); 
+0

您已接受此答案?爲什麼不是我的?你要求一個堅持中央面板的左側。這不是左邊... – feltersnach

0

代替組佈局的使用BorderLayout的()。使紅色面板BorderLayout.LEFT和其他BorderLayout.CENTER。

例子:

JPanel container = new JPanel(); 
JFrame frame = new JFrame(); 
frame.add (container, BorderLayout.LEFT);