Java GridBagLayout:如何设置固定列宽?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24780480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 14:50:02  来源:igfitidea点击:

GridBagLayout: How to set fixed column width?

javaswingawtgridbaglayout

提问by Marie

I've been trying to set fixed column width in the following code for ages...The thing is that when I add a label to the panel on the left, then the width is incremented automatically...and i Would like the column width to be fixed...

我一直在尝试在以下代码中设置固定列宽多年...问题是当我向左侧面板添加标签时,宽度会自动增加...我想要列宽度固定...

Could anyone help me please??

有人可以帮我吗??

This is the code:

这是代码:

import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Panel;
import java.awt.Point;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.TargetDataLine;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;

import java.io.IOException;

public class CurrentItem extends JPanel{

    private static JFrame currentScreen; 

    public CurrentItem(JFrame p) { 

        GridBagConstraints c = new GridBagConstraints();

        //Borramos todo lo que haya en la pantalla 
        this.removeAll();
        this.revalidate();

        currentScreen = p;     

        this.setBackground(Color.LIGHT_GRAY);      

        this.setLayout(new GridBagLayout());  

        currentScreen.add(this);       

        // --->
        Panel leftPanel = new Panel(); 
        leftPanel.setBackground(Color.BLACK);

        c.weightx = 1.5;
        c.weighty = 1;
        //c.fill = GridBagConstraints.BOTH;      
        this.add(leftPanel, c);
        // <---

        // --->
        Panel secondPanel = new Panel(); 
        secondPanel.setBackground(Color.green);

        c.weightx = 0.25;
        c.weighty = 1;
        c.fill = GridBagConstraints.BOTH;

        this.add(secondPanel, c);
        // <---     

        // --->
        Panel rightPanel = new Panel(); 
        rightPanel.setBackground(Color.CYAN);

        c.weightx = 1.5;
        c.weighty = 1;
        c.fill = GridBagConstraints.BOTH;

        this.add(rightPanel, c);
        // <---

        currentScreen.setVisible(true);

        this.requestFocusInWindow();         
    }
}

回答by Braj

GridBagLayoutplaces components in rectangles (cells) in a grid, and then uses the components' preferred sizesto determine how big the cells should be.

GridBagLayout将组件放在网格中的矩形(单元格)中,然后使用组件的首选大小来确定单元格应该有多大。

Weights are used to determine how to distribute space among columns (weightx) and among rows (weighty); this is important for specifying resizing behavior.

权重用于确定如何在列 ( weightx) 和行 ( weighty)之间分配空间;这对于指定调整大小行为很重要。

Generally weights are specified with 0.0 and 1.0 as the extremes: the numbers in between are used as necessary.

通常权重指定为 0.0 和 1.0 作为极端值:根据需要使用介于两者之间的数字。

Read more How to Use GridBagLayout

阅读更多如何使用 GridBagLayout



Sample code: (below code set fixed width column with 100% vertical size.)

示例代码:(下面的代码设置了 100% 垂直大小的固定宽度列。)

Panel leftPanel = new Panel() {
    @Override
    public Dimension getPreferredSize() {
        return new Dimension(..., ...);
    }
};

Remove weightxfor first column and override getPreferredSize()method.

删除weightx第一列并覆盖getPreferredSize()方法。

leftPanel.setBackground(Color.BLACK);

c.weighty = 1;
c.fill = GridBagConstraints.VERTICAL;
this.add(leftPanel, c);

回答by splungebob

If you know ahead of time the labels that you'll be adding, and how wide each one is:

如果您提前知道要添加的标签以及每个标签的宽度:

  • You can call label.getPreferredSize().widthon each label to determine the width of the largest label.

  • Then, add an invisible component (Box.createHorizontalStrut(width)) to the column to force it to be that wide at all times.

  • Then, when you add all of your labels, the width of that column of the panel will not change.

  • 您可以调用label.getPreferredSize().width每个标签来确定最大标签的宽度。

  • 然后,Box.createHorizontalStrut(width)向该列添加一个不可见组件 ( ) 以使其始终保持那么宽。

  • 然后,当您添加所有标签时,面板该列的宽度不会改变。