Java 防止GridBagLayout调整列的大小

Java 防止GridBagLayout调整列的大小,java,swing,gridbaglayout,Java,Swing,Gridbaglayout,swing的另一个问题。如果其中一个组件更改了大小,如何阻止GridBagLayout替换组件?例如,我有几个列,其中一个列中有一个带有文本“text”的JLabel。当我将其更改为“texttext”时,布局管理器会调整整个列的大小。我不想让它那样做。有什么办法可以预防吗 例如: import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; public class ResizeIssue { st

swing的另一个问题。如果其中一个组件更改了大小,如何阻止GridBagLayout替换组件?例如,我有几个列,其中一个列中有一个带有文本“text”的JLabel。当我将其更改为“texttext”时,布局管理器会调整整个列的大小。我不想让它那样做。有什么办法可以预防吗

例如:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

public class ResizeIssue {

 static int value = 99;

 public static void main(String[] args) {

    JFrame frame = new JFrame();
    final JLabel valueLabel = new JLabel(String.valueOf(value));
    JButton decButton = new JButton("-");
    decButton.addActionListener(new java.awt.event.ActionListener() {

        @Override
        public void actionPerformed(ActionEvent ae) {
            valueLabel.setText(String.valueOf(--value));
        }
    });

    JButton incButton = new JButton("+");
    incButton.addActionListener(new java.awt.event.ActionListener() {

        @Override
        public void actionPerformed(ActionEvent ae) {
            valueLabel.setText(String.valueOf(++value));
        }
    });

  JPanel panel = new JPanel();
  panel.setLayout(new GridBagLayout());
  GridBagConstraints c = new GridBagConstraints();
  c.weightx = 1;
  c.gridx = 0;
  c.gridy = 0;
  panel.add(decButton, c);
  c.gridx = 1;
  panel.add(valueLabel, c);
  c.gridx = 2;
  panel.add(incButton, c);

  frame.add(panel);
  frame.pack();
  frame.setVisible(true);
  }
 }

当9->10或任何时候文本改变宽度时,它都可见。

GridBagLayout忽略最大宽度/高度。设置JLabel的最大大小并非易事

但是,我认为您真正想要的是,当JLabel中的文本发生更改时,布局不会改变

这可以通过使JLabel足够宽以容纳它需要显示的最大值来实现。例如:

    jLabel1.setFont(new Font("monospace", Font.PLAIN, 12));
    FontMetrics fm = jLabel1.getFontMetrics(jLabel1.getFont());
    int w = fm.stringWidth("0000");
    int h = fm.getHeight();
    Dimension size = new Dimension(w, h);
    jLabel1.setMinimumSize(size);
    jLabel1.setPreferredSize(size);
更新:

要使标签文本居中,只需添加:

    jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

GridBagLayout忽略最大宽度/高度。设置JLabel的最大大小并非易事

但是,我认为您真正想要的是,当JLabel中的文本发生更改时,布局不会改变

这可以通过使JLabel足够宽以容纳它需要显示的最大值来实现。例如:

    jLabel1.setFont(new Font("monospace", Font.PLAIN, 12));
    FontMetrics fm = jLabel1.getFontMetrics(jLabel1.getFont());
    int w = fm.stringWidth("0000");
    int h = fm.getHeight();
    Dimension size = new Dimension(w, h);
    jLabel1.setMinimumSize(size);
    jLabel1.setPreferredSize(size);
更新:

要使标签文本居中,只需添加:

    jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
这可能会奏效:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ResizeIssue2 {
  static int value = 99;
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    final JLabel valueLabel = new JLabel(String.valueOf(value));
    JButton decButton = new JButton("-");
    decButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            valueLabel.setText(String.valueOf(--value));
        }
    });

    JButton incButton = new JButton("+");
    incButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            valueLabel.setText(String.valueOf(++value));
        }
    });

    JPanel panel = new JPanel();
    panel.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.weightx = 1;
    c.gridx = 0;
    c.gridy = 0;
    panel.add(decButton, c);
    c.gridx = 1;
    panel.add(valueLabel, c);
    c.gridx = 2;
    panel.add(incButton, c);

    //*
    c.gridy = 1; 
    int w = 32; //incButton.getPreferredSize().width;
    for(c.gridx=0;c.gridx<3;c.gridx++) {
      panel.add(Box.createHorizontalStrut(w), c);
    }
    // */

    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.add(panel);
    frame.pack();
    frame.setVisible(true);
  }
}
import javax.swing.*;
导入java.awt.*;
导入java.awt.event.*;
公共类调整问题2{
静态int值=99;
公共静态void main(字符串[]args){
JFrame=新JFrame();
最终JLabel valueLabel=新的JLabel(String.valueOf(value));
JButton decButton=新JButton(“-”);
decButton.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效行动(行动事件ae){
valueLabel.setText(String.valueOf(--value));
}
});
JButton incButton=新JButton(“+”);
incButton.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效行动(行动事件ae){
valueLabel.setText(String.valueOf(++value));
}
});
JPanel面板=新的JPanel();
panel.setLayout(新的GridBagLayout());
GridBagConstraints c=新的GridBagConstraints();
c、 权重x=1;
c、 gridx=0;
c、 gridy=0;
面板。添加(decButton,c);
c、 gridx=1;
面板。添加(valueLabel,c);
c、 gridx=2;
面板。添加(INC按钮,c);
//*
c、 gridy=1;
int w=32;//incButton.getPreferredSize().width;
对于(c.gridx=0;c.gridx这可能会起作用:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ResizeIssue2 {
  static int value = 99;
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    final JLabel valueLabel = new JLabel(String.valueOf(value));
    JButton decButton = new JButton("-");
    decButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            valueLabel.setText(String.valueOf(--value));
        }
    });

    JButton incButton = new JButton("+");
    incButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            valueLabel.setText(String.valueOf(++value));
        }
    });

    JPanel panel = new JPanel();
    panel.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.weightx = 1;
    c.gridx = 0;
    c.gridy = 0;
    panel.add(decButton, c);
    c.gridx = 1;
    panel.add(valueLabel, c);
    c.gridx = 2;
    panel.add(incButton, c);

    //*
    c.gridy = 1; 
    int w = 32; //incButton.getPreferredSize().width;
    for(c.gridx=0;c.gridx<3;c.gridx++) {
      panel.add(Box.createHorizontalStrut(w), c);
    }
    // */

    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.add(panel);
    frame.pack();
    frame.setVisible(true);
  }
}
import javax.swing.*;
导入java.awt.*;
导入java.awt.event.*;
公共类调整问题2{
静态int值=99;
公共静态void main(字符串[]args){
JFrame=新JFrame();
最终JLabel valueLabel=新的JLabel(String.valueOf(value));
JButton decButton=新JButton(“-”);
decButton.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效行动(行动事件ae){
valueLabel.setText(String.valueOf(--value));
}
});
JButton incButton=新JButton(“+”);
incButton.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效行动(行动事件ae){
valueLabel.setText(String.valueOf(++value));
}
});
JPanel面板=新的JPanel();
panel.setLayout(新的GridBagLayout());
GridBagConstraints c=新的GridBagConstraints();
c、 权重x=1;
c、 gridx=0;
c、 gridy=0;
面板。添加(decButton,c);
c、 gridx=1;
面板。添加(valueLabel,c);
c、 gridx=2;
面板。添加(INC按钮,c);
//*
c、 gridy=1;
int w=32;//incButton.getPreferredSize().width;

for(c.gridx=0;c.gridx)为获得更好的帮助,请尽早使用编辑您的问题,使用大小调整演示您的问题为获得更好的帮助,请尽早使用大小调整演示您的问题这只能部分解决问题,因为如果我这样做,文本不会居中于按钮之间的空间(如果“掩码”为xx,则一位数字为9x,x不可见,但占用空间).@update:gosh,我没有考虑过这个问题,在这种情况下它起了作用。我仍然有类似的情况,长文本,但按钮仍然会移动一点。@joval它只是决定了JLabel的大小,这样它就不需要增长以适应您将要显示的文本。如果您无法控制文本,请在中切换到JTextAreajscrollpaneth这只能部分解决问题,因为如果我这样做,文本不会在按钮之间的空格中居中(如果“掩码”为xx,一位数为9x,x不可见,但占用空格).@update:gosh,我没有考虑过这个问题,在这种情况下它起了作用。我仍然有类似的情况,长文本,但按钮仍然会移动一点。@joval它只是决定了JLabel的大小,这样它就不需要增长以适应您将要显示的文本。如果您无法控制文本,请在中切换到JTextAreaJScrollPaneWell,按钮标签按钮只是我对话框的一部分,下面有四行类似,一行类似,因此我无法在下面添加任何内容。在类似块上尝试过,但不起作用。嗯,按钮标签按钮只是我对话框的一部分,下面有四行类似,一行类似,因此我无法在下面添加任何内容。在类似块上尝试过,但是不起作用。