2017-02-21 42 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