2017-02-21 60 views
-1

我遇到GridBagLayout問題& GridBagConstraints在GUI中我開始構建。我必須要有圖片,其中一種目前GUI的狀態,&是GUI所需狀態之一。我一直在試圖達到所需的狀態,但一直無法:(這裏是代碼,&我還會附上上面提到的2張圖片,而且,我正在格式化第一個或第第二個複選框,但我一直無法弄清楚是什麼問題無法正確使用GridBagLayout進行佈局

驅動程序類:

import javax.swing.SwingUtilities; 

public class Driver { 
    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       new TheFrame(); 
      } 

     }); 
    } 
} 

JFrame類:

import javax.swing.JFrame; 
import javax.swing.JCheckBox; 
import javax.swing.JLabel; 
import java.awt.GridBagLayout; 
import java.awt.GridBagConstraints; 
import java.awt.Insets; 

public class TheFrame extends JFrame { 

    //Declarations 
    private GridBagConstraints gbc; 
    private String myString; 
    private JLabel selectionLab; 
    private JCheckBox defconLevel1; 
    private JCheckBox defconLevel2; 
    private JCheckBox defconLevel3; 
    private JCheckBox defconLevel4; 
    private JCheckBox defconLevel5; 

    public TheFrame() { 
     super("DEFCON DEACTIVATOR"); 
     this.setSize(500,500); 
     this.setVisible(true); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     this.getContentPane().setLayout(new GridBagLayout()); 

     //Initialization 
     gbc = new GridBagConstraints(); 
     selectionLab = new JLabel("Please Select DECON Level"); 
     defconLevel1 = new JCheckBox("DEFCON 1"); 
     defconLevel2 = new JCheckBox("DEFCON 2"); 
     defconLevel3 = new JCheckBox("DEFCON 3"); 
     defconLevel4 = new JCheckBox("DEFCON 4"); 
     defconLevel5 = new JCheckBox("DEFCON 5"); 

     //Configuration 


     //Add to contentPane 
     //ROW 1 
     gbc.gridx = 0; 
     gbc.gridy = 0; 
     gbc.anchor = gbc.NORTH; 
     gbc.weighty = 1; 
     gbc.insets = new Insets(0,0,0,0); 
     this.getContentPane().add(selectionLab); 

     //ROW 2 
     gbc.gridx = 0; 
     gbc.gridy = 1; 
     gbc.anchor = gbc.NORTH; 
     gbc.weighty= 1; 
     gbc.insets = new Insets(0,0,0,0); 
     this.getContentPane().add(defconLevel1,gbc); 
     gbc.gridx = 1; 
     gbc.gridy = 1; 
     gbc.anchor = gbc.NORTH; 
     gbc.weighty= 1; 
     gbc.insets = new Insets(0,0,0,0); 
     this.getContentPane().add(defconLevel2,gbc); 
     gbc.gridx = 2; 
     gbc.gridy = 1; 
     gbc.anchor = gbc.NORTH; 
     gbc.weighty= 1; 
     gbc.insets = new Insets(0,0,0,0); 
     this.getContentPane().add(defconLevel3,gbc); 
     gbc.gridx = 3; 
     gbc.gridy = 1; 
     gbc.anchor = gbc.NORTH; 
     gbc.weighty= 1; 
     gbc.insets = new Insets(0,0,0,0); 
     this.getContentPane().add(defconLevel4,gbc); 
     gbc.gridx = 4; 
     gbc.gridy = 1; 
     gbc.anchor = gbc.NORTH; 
     gbc.weighty= 1; 
     gbc.insets = new Insets(0,0,0,0); 
     this.getContentPane().add(defconLevel5,gbc); 
    } 
} 

更新的代碼:

驅動程序類

import javax.swing.SwingUtilities; 

public class Driver { 

    //Declarations 
    private static SelectionPanel selectionPanel; 
    private static HeaderPanel headerPanel; 
    private static TheFrame frame = new TheFrame(selectionPanel,headerPanel); 


// public Driver() { 
//  
// } 

    public static void main(String[] args) { 

     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new Driver(); 
      } 
     }); 
    } 
} 

TheFrame類

import javax.swing.JFrame; 
import java.awt.GridBagLayout; 
import java.awt.GridBagConstraints; 
import java.awt.Insets; 

public class TheFrame extends JFrame { 

    //Declarations 
    private GridBagConstraints gbc; 
    private SelectionPanel selectionPanel; 
    private HeaderPanel headerPanel; 

    public TheFrame(SelectionPanel selectionPanel, HeaderPanel headerPanel) { 
     super("DEFCON DEACTIVATOR"); 
     this.selectionPanel = selectionPanel; 
     this.headerPanel = headerPanel; 

     //Initialization 
     gbc = new GridBagConstraints(); 
     selectionPanel = new SelectionPanel(); 
     headerPanel = new HeaderPanel(); 
     this.getContentPane().setLayout(new GridBagLayout()); 

     //Configuration 


     //Add to contentPane 
     gbc.anchor = gbc.NORTH; //Content-Pane GLOBAL 
     gbc.insets = new Insets(0,0,0,0); //Content-Pane GLOBAL 
     gbc.weightx = 0; //Content-Pane GLOBAL 

     gbc.weighty = 0.05; 
     gbc.gridx = 0; 
     gbc.gridy = 0; 
     this.getContentPane().add(headerPanel,gbc); 
     gbc.weighty = 1; 
     gbc.gridx = 0; 
     gbc.gridy = 1; 
     this.getContentPane().add(selectionPanel,gbc); 

     //Finalize JFrame Last Steps Configurations 
     this.setSize(500,500); 
     this.setVisible(true); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 
} 

SelectionPanel類

import java.awt.Insets; 
import javax.swing.JCheckBox; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 

public class SelectionPanel extends JPanel { 

    //Declarations 
    private JCheckBox defconLevel1; 
    private JCheckBox defconLevel2; 
    private JCheckBox defconLevel3; 
    private JCheckBox defconLevel4; 
    private JCheckBox defconLevel5; 
    private GridBagConstraints gbc; 

    public SelectionPanel() { 

     //Initializations 
     defconLevel1 = new JCheckBox("DEFCON 1"); 
     defconLevel2 = new JCheckBox("DEFCON 2"); 
     defconLevel3 = new JCheckBox("DEFCON 3"); 
     defconLevel4 = new JCheckBox("DEFCON 4"); 
     defconLevel5 = new JCheckBox("DEFCON 5"); 
     gbc = new GridBagConstraints(); 

     //Configuration 
     this.setLayout(new GridBagLayout()); 

     //Add 
     //ROW 1 
     gbc.insets = new Insets(0,0,0,0); //Content-Pane Global 
     //gbc.anchor = gbc.EAST; //Makes Elements chain-follow each other 
     gbc.gridy = 0; 

     gbc.gridx = 0; 
     this.add(defconLevel1,gbc); 
     gbc.gridx = 1; 
     this.add(defconLevel2,gbc); 
     gbc.gridx = 2; 
     this.add(defconLevel3,gbc); 
     gbc.gridx = 3; 
     this.add(defconLevel4,gbc); 
     gbc.gridx = 4; 
     this.add(defconLevel5,gbc); 
    } 
} 

HeaderPanel類

import javax.swing.JPanel; 
import javax.swing.JLabel; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 

public class HeaderPanel extends JPanel { 
    private JLabel headerLab; 
    private GridBagConstraints gbc; 

    public HeaderPanel() { 
     //Initialize 
     headerLab = new JLabel("PLEASE SELECT DEFCON LEVEL"); 
     gbc = new GridBagConstraints(); 

     //Configure 
     this.setLayout(new GridBagLayout()); 

     //Add 
     gbc.gridx = 0; 
     gbc.gridy = 0; 
     this.add(headerLab,gbc); 
    } 

} 

當前圖像:

enter image description here

祝願設計:

enter image description here

更新圖片:

enter image description here

+0

你想讓所有的複選框在窗口中水平居中嗎? – VGR

+0

嗨VGR,我遇到的問題是均勻合併「DEFCON 1」複選框與其餘複選框。如果仔細觀察,可以發現「DEFCON 1」複選框相對於其他複選框具有額外的左側間距。這是我面臨的問題。謝謝你的迴應:) – chromechris

回答

3

該標籤的約束還需要:

gbc.gridwitdh = 5; 

這將允許標籤佔據與5個複選框相同的水平空間,允許每個複選框顯示在其自己的列中。

在添加其他組件之前,您需要登錄reset the gridwidth to 1

另一種選擇可能是在面板上使用Titled Border。然後你可以使用FlowLayout來添加所有的複選框。這是一個更簡單的解決方案,因爲您不必擔心所有的GridBagConstraints。

編輯:

閱讀從Swing教程中的部分上How to Use GridBagLayout有關的所有約束的信息。

你需要做的是實際使用的標籤,否則設置gridwidth不會做任何的約束作爲默認constrainsts將使用的第一件事:

//this.getContentPane().add(selectionLab); 
add(selectionLab, gbc); 

它仍然不會看起來是正確的因爲你會再需要了解正確的價值觀有以下限制使用:

  1. 沉重
  2. weightx

我會讓你一次玩一個約束來看看當你改變約束時會發生什麼。教程鏈接將幫助您瞭解不同的值。

+0

嗯,我試過這個,但它似乎並沒有改變這種情況。我遇到的問題是在「DEFCON 1」和「DEFCON 2」jcheckbox之間創建的額外空間。額外的間距位於「DEFCON 1」jcheckbox右側,如我共享的「當前圖片」圖片所示。感謝您的回答,但我感謝您的幫助:) – chromechris

+1

@ user42076,請參閱編輯。 – camickr

+0

謝謝!我相信我的問題也來自我根據我提供的JFrame佈局在我的JFRame中設置JPanel的方式。我決定創建一個包含我的頭文件的獨特JPanel。我將在問題中添加代碼併發布新結果的圖像。 – chromechris