GridBagLayout,当我放置组件时,如何阻止屏幕伸展?

时间:2014-08-05 20:27:57

标签: java swing gridbaglayout

我使用GridBagLayout并且我非常喜欢它,但是当我放置和移动我的组件时,我的屏幕拉伸有问题。我认为这与我的插图有关。我使用插图来移动和调整我的组件大小,我猜它在编码世界中是一个很大的禁忌,但我不知道如何以另一种方式做到这一点。我试图制作一个有人可以填写的表格,我希望它看起来整洁。

这是我的计划图片: enter image description here

正如您所看到的,名称文本字段比其他任何内容都延伸得更远,而且当我使用insets修改我的JComboBox时,这种拉伸变得越来越差。

这是我的代码:

 vframe = new JFrame("MES Banking App");
 vpanel = new JPanel();
 titlelabel = new JLabel("Verification Page");
 namelabel = new JLabel("Name: ");
 namefield = new JTextField(20);
 birthdaylabel = new JLabel("Birth date: ");
 dayDD = new JLabel("Day:");
 daysArray = new String[] {"1","2","3","4","5","6","7","8","9", "10", "11", "12", "13", "14","15","16","17","18"};//31
 BDday = new JComboBox(daysArray);
 monthDD = new JLabel("Month:");
 monthsArray = new String[] {"1","2","3","4","5","6","7","8","9","10","11","12"}; //12 months
 BDmonth = new JComboBox(monthsArray);



//title section
 vpanel.setLayout(new GridBagLayout());
 grid = new GridBagConstraints();
 grid.fill = GridBagConstraints.PAGE_START;
 grid.weightx = 0;
 grid.gridx = 0;
 grid.gridy = 0;
 vpanel.add(titlelabel,grid);

//name section
grid.fill = GridBagConstraints.HORIZONTAL;
grid.gridx = 0;
grid.gridy = 1;
grid.insets = new Insets(10,0,0,0);
vpanel.add(namelabel,grid);//adds name label
grid.gridx = 1;
grid.gridy = 1;
grid.insets = new Insets(0,0,0,0);
vpanel.add(namefield,grid); //adds name text field

//birthday section
grid.fill = GridBagConstraints.HORIZONTAL;
grid.gridx = 0;
grid.gridy = 2;
grid.insets = new Insets(0,0,0,0);
vpanel.add(birthdaylabel,grid); //adds bday label
grid.gridx = 1;
grid.gridy = 2;
vpanel.add(dayDD,grid); //add day label

grid.fill = GridBagConstraints.HORIZONTAL;
grid.gridx = 1;
grid.gridy = 2;
grid.insets = new Insets(0,30,0,350);//sets day drop down
vpanel.add(BDday,grid);

grid.fill = GridBagConstraints.HORIZONTAL;
grid.gridx = 1;
grid.gridy = 2;
grid.insets = new Insets(0,100,0,250);//month label
vpanel.add(monthDD,grid);

grid.fill = GridBagConstraints.HORIZONTAL;
grid.gridx = 1;
grid.gridy = 2;
grid.insets = new Insets(0,150,0,250);
vpanel.add(BDmonth,grid);



 vframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 vframe.getContentPane().add(vpanel);  

 vframe.pack();
 vframe.setVisible(true); 

有人可以演示如何以不拉伸屏幕的方式操纵组件吗?编辑我的代码既可以解释也可以用文字解释,但我非常直观,代码片段最适合我。

谢谢

2 个答案:

答案 0 :(得分:3)

您正在使用GridBagLayout,特别是GridBagConstraints错误,因为您正在将组件放在彼此之上。您需要确保不要为同一组件提供相同的gridx和gridy位置。使用GridWidth拉出组件,但为下一个组件重新设置它。不要为此目的使用插图。请注意,为每个组件使用新的GridBagConstraints通常会更好。我经常为此目的创建一个GridBagConstraints创建方法。

如,

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.*;

public class LayoutFoo2 {

   private static JFrame vframe;
   private static JPanel vpanel;
   private static JLabel titlelabel;
   private static JLabel namelabel;
   private static JTextField namefield;
   private static JLabel birthdaylabel;
   private static JLabel dayDD;
   private static String[] daysArray;
   private static JComboBox BDday;
   private static JLabel monthDD;
   private static String[] monthsArray;
   private static JComboBox BDmonth;
   private static GridBagConstraints grid;

   public static void main(String[] args) {
      vframe = new JFrame("MES Banking App");
      vpanel = new JPanel();
      titlelabel = new JLabel("Verification Page");
      namelabel = new JLabel("Name: ");
      namefield = new JTextField(20);
      birthdaylabel = new JLabel("Birth date: ");
      dayDD = new JLabel("Day:");
      daysArray = new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9",
            "10", "11", "12", "13", "14", "15", "16", "17", "18" };// 31
      BDday = new JComboBox(daysArray);
      monthDD = new JLabel("Month:");
      monthsArray = new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9",
            "10", "11", "12" }; // 12 months
      BDmonth = new JComboBox(monthsArray);

      // title section
      vpanel.setLayout(new GridBagLayout());
      grid = new GridBagConstraints();
      grid.fill = GridBagConstraints.CENTER;
      grid.insets = new Insets(5, 5, 5, 5);
      grid.weightx = 1.0;
      grid.weighty = 1.0;
      grid.fill = GridBagConstraints.HORIZONTAL;
      grid.gridx = 0;
      grid.gridy = 0;
      grid.gridwidth = 5;
      grid.gridheight = 1;
      vpanel.add(titlelabel, grid);

      // name section
      grid.fill = GridBagConstraints.HORIZONTAL;
      grid.gridwidth = 1;
      grid.gridx = 0;
      grid.gridy = 1;
      vpanel.add(namelabel, grid);// adds name label
      grid.gridx = 1;
      grid.gridy = 1;
      grid.gridwidth = 4;
      vpanel.add(namefield, grid); // adds name text field

      // birthday section
      grid.fill = GridBagConstraints.HORIZONTAL;
      grid.gridx = 0;
      grid.gridy = 2;
      grid.gridwidth = 1;
      vpanel.add(birthdaylabel, grid); // adds bday label
      grid.gridx = 1;
      grid.gridy = 2;
      vpanel.add(dayDD, grid); // add day label

      grid.fill = GridBagConstraints.HORIZONTAL;
      grid.gridx = 2;
      grid.gridy = 2;
      //!! grid.insets = new Insets(0, 30, 0, 350);// sets day drop down
      vpanel.add(BDday, grid);

      grid.fill = GridBagConstraints.HORIZONTAL;
      grid.gridx = 3;
      grid.gridy = 2;
      //!! grid.insets = new Insets(0, 100, 0, 250);// month label
      vpanel.add(monthDD, grid);

      grid.fill = GridBagConstraints.HORIZONTAL;
      grid.gridx = 4;
      grid.gridy = 2;
      //!! grid.insets = new Insets(0, 150, 0, 250);
      vpanel.add(BDmonth, grid);

      vframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      vframe.getContentPane().add(vpanel);

      vframe.pack();
      vframe.setVisible(true);
   }
}

因此,使用两种方法来简化事情:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.*;

@SuppressWarnings("serial")
public class LayoutFoo2 extends JPanel {

   private static final Insets DEFAULT_INSETS = new Insets(5, 5, 5, 5);
   private static final double DEFAULT_WEIGHTX = 1.0;
   private static final double DEFAULT_WEIGHTY = 1.0;
   private static JFrame vframe;
   private static JPanel vpanel;
   private static JLabel titlelabel;
   private static JLabel namelabel;
   private static JTextField namefield;
   private static JLabel birthdaylabel;
   private static JLabel dayDD;
   private static String[] daysArray;
   private static JComboBox BDday;
   private static JLabel monthDD;
   private static String[] monthsArray;
   private static JComboBox BDmonth;
   private static GridBagConstraints grid;

   public static void main(String[] args) {
      vframe = new JFrame("MES Banking App");
      vpanel = new JPanel();
      titlelabel = new JLabel("Verification Page");
      namelabel = new JLabel("Name: ");
      namefield = new JTextField(20);
      birthdaylabel = new JLabel("Birth date: ");
      dayDD = new JLabel("Day:");
      daysArray = new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9",
            "10", "11", "12", "13", "14", "15", "16", "17", "18" };// 31
      BDday = new JComboBox(daysArray);
      monthDD = new JLabel("Month:");
      monthsArray = new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9",
            "10", "11", "12" }; // 12 months
      BDmonth = new JComboBox(monthsArray);

      // title section
      vpanel.setLayout(new GridBagLayout());
      grid = createGbc(0, 0, 5, 1);
      grid.fill = GridBagConstraints.CENTER;
      titlelabel.setHorizontalTextPosition(JLabel.CENTER);
      vpanel.add(titlelabel, grid);

      // name section
      grid = createGbc(0, 1);
      vpanel.add(namelabel, grid);// adds name label

      grid = createGbc(1, 1, 4, 1);
      vpanel.add(namefield, grid); // adds name text field

      // birthday section
      grid = createGbc(0, 2);
      vpanel.add(birthdaylabel, grid); // adds bday label
      grid = createGbc(1, 2);
      vpanel.add(dayDD, grid); // add day label


      grid = createGbc(2, 2);
      vpanel.add(BDday, grid);

      grid = createGbc(3, 2);
      vpanel.add(monthDD, grid);
      grid = createGbc(4, 2);
      vpanel.add(BDmonth, grid);

      vframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      vframe.getContentPane().add(vpanel);

      vframe.pack();
      vframe.setVisible(true);
   }

   public static GridBagConstraints createGbc(int x, int y, int width, int height) {
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.gridx = x;
      gbc.gridy = y;
      gbc.gridwidth = width;
      gbc.gridheight = height;

      // default set ups
      gbc.insets = DEFAULT_INSETS;
      gbc.weightx = DEFAULT_WEIGHTX;
      gbc.weighty = DEFAULT_WEIGHTY;
      gbc.fill = GridBagConstraints.HORIZONTAL;

      return gbc;
   }

   public static GridBagConstraints createGbc(int x, int y) {
      return createGbc(x, y, 1, 1);
   }
}

答案 1 :(得分:1)

Hovercraft速度更快,我的GridBagLayout解决方案只清除多个示例 duplicities:

package com.zetcode;

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class GridBagBankingApp extends JFrame {

    public GridBagBankingApp() {

        initUI();

        setTitle("MES Banking App");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private void initUI() {

        JLabel titlelabel = new JLabel("Verification Page");
        JLabel namelabel = new JLabel("Name: ");
        JTextField namefield = new JTextField(10);
        JLabel birthdaylabel = new JLabel("Birth date: ");
        JLabel dayDD = new JLabel("Day:");
        String[] daysArray = new String[]{"1", "2", "3", "4", "5", 
            "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", 
            "16", "17", "18"};// 31
        JComboBox BDday = new JComboBox(daysArray);
        JLabel monthDD = new JLabel("Month:");
        String[] monthsArray = new String[]{"1", "2", "3", "4", "5", 
            "6", "7", "8", "9", "10", "11", "12"}; // 12 months
        JComboBox BDmonth = new JComboBox(monthsArray);

        //title section
        setLayout(new GridBagLayout());
        GridBagConstraints grid = new GridBagConstraints();
        grid.fill = GridBagConstraints.CENTER;
        grid.insets = new Insets(5, 5, 5, 5);   
        grid.gridwidth = 5;
        add(titlelabel, grid);

        //name section
        grid.fill = GridBagConstraints.HORIZONTAL;
        grid.gridwidth = 1;
        grid.gridy = 1;
        add(namelabel, grid);

        grid.gridx = 1;
        grid.gridwidth = 4;
        add(namefield, grid); 

        //birthday section
        grid.gridx = 0;
        grid.gridy = 2;
        grid.gridwidth = 1;
        add(birthdaylabel, grid); 

        grid.gridx = 1;
        add(dayDD, grid); 

        grid.gridx = 2;
        add(BDday, grid);

        grid.gridx = 3;
        add(monthDD, grid);

        grid.gridx = 4;
        add(BDmonth, grid);

        pack();
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                GridBagBankingApp ex = new GridBagBankingApp();
                ex.setVisible(true);
            }
        });
    }
}

使用GridBagLayout经理代替MigLayout。与创建的MigLayout不同 一次完整的网格,GridBagLayout我们必须定义每个单元格 个别。这很烦人且容易出错。

MigLayout更便携。在GridBagLayout's解决方案中,我们有 以下列方式定义insets:

grid.insets = new Insets(5, 5, 5, 5);  

在某个分辨率上可能没问题。在其他人看来,这是不正确的。 (它可能 太小,破坏了我们的整个布局。)MigLayout之间产生了差距 屏幕分辨率和DPI无关的组件。出于这个原因, 我们应该避免使用GridBagLayout并使用MigLayoutGroupLayout 管理者。

package com.zetcode;

import java.awt.EventQueue;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import net.miginfocom.swing.MigLayout;


public class MigLayoutBankingApp extends JFrame {

    public MigLayoutBankingApp() {

        initUI();
        setTitle("MES Banking App");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
    }

    private void initUI() {

        JPanel pnl = new JPanel(new MigLayout());

        JLabel titleLbl = new JLabel("Verification Page");
        JLabel nameLbl = new JLabel("Name: ");
        JTextField nameField = new JTextField(10);
        JLabel bdLbl = new JLabel("Birth date: ");
        JLabel dayDD = new JLabel("Day:");
        String[] daysArray = new String[]{"1", "2", "3", "4", "5", 
            "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", 
            "16", "17", "18" };
        JComboBox BDday = new JComboBox(daysArray);
        JLabel monthDD = new JLabel("Month:");
        String[] monthsArray = new String[]{"1", "2", "3", "4", "5", 
            "6", "7", "8", "9", "10", "11", "12"}; // 12 months
        JComboBox BDmonth = new JComboBox(monthsArray);        

        pnl.add(titleLbl, "spanx, center, wrap");
        pnl.add(nameLbl);
        pnl.add(nameField, "spanx, growx, wrap");
        pnl.add(bdLbl);
        pnl.add(dayDD);
        pnl.add(BDday);
        pnl.add(monthDD);
        pnl.add(BDmonth);

        add(pnl);

        pack();
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                MigLayoutBankingApp ex = new MigLayoutBankingApp();
                ex.setVisible(true);
            }
        });
    }
}

我们可以清楚地看到,代码要短得多。

enter image description here