English 中文(简体)
防止网格BagLayout 调整列大小
原标题:prevent GridBagLayout from resizing columns

秋千的另一个问题。如果其中一个组件的大小发生了变化,我如何阻止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);
问题回答

这可能奏效:

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);
  }
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签