java - GridBagLayout,如何在放置组件时阻止屏幕拉伸(stretch)?

标签 java swing gridbaglayout

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

这是我的程序的图片: enter image description here

如您所见,名称文本字段比其他所有内容都延伸得更远,并且随着我使用插入修改我的 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); 

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

谢谢

最佳答案

您使用 GridBagLayout 尤其是 GridBagConstraints 是错误的,因为您将组件放在彼此之上。您需要确保不要为同一组件提供相同的 gridx 和 gridy 位置。使用 GridWidth 拉伸(stretch)组件,但为下一个组件重新设置它。不要为此目的使用插图。请注意,通常最好为每个组件使用新的 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);
   }
}

关于java - GridBagLayout,如何在放置组件时阻止屏幕拉伸(stretch)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25147732/

相关文章:

java - 是否可以在 Hibernate/JPA 中动态定义列名?

java - 谷歌应用引擎大表

java - 在 Grails 中使用 Java 类(具有 Spring/Hibernate 依赖项的整个模块)

java - 正确使用界面中的default关键字

java - JPopupMenu 的 MenuItems 不响应更改...为什么?

java - 简单的 Java 程序越来越消耗内存

java - 按钮文本在 gridBagLayout 中不完全可见

java - GridBagLayout gridwidth 不能按预期工作

java - 在 CardLayout 中切换面板时如何提醒面板可见

java - JAVA中的GridBagLayout问题