当内容动态更改时调整 Swing 组件的大小

发布于 2024-09-17 08:34:38 字数 1934 浏览 2 评论 0原文

我对两个组件 JTextField 和 JComboBox 有同样的问题,我假设我正在寻找的解决方案可以解决所有组件的问题。

我已将组件的大小设置为默认值,因此它们的大小适合我提供给它的初始内容。当我将组件的内容更改为超出组件的区域时,我看不到整个文本,并且我希望调整组件的大小以适合文本。

我怎样才能做到这一点?

更新:

框架上的 pack() 仅放大了文本字段,我怎样才能做同样的事情并放大组合框?

更新:

    private class ComboBoxRenderer extends JLabel implements ListCellRenderer {
    private static final long serialVersionUID = 752379460716217273L;
    Dimension maxSize=new Dimension();
    @Override
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        setText(value.toString());
        Dimension size = getPreferredSize();
        if(maxSize.width<size.width)
            maxSize.width=size.width;
        if(maxSize.height<size.height)
            maxSize.height=size.height;

        resolutionDescriptor_ComboBox.setPreferredSize(maxSize);
        return this;
    }

}

这有效,不是很有效,但这是第一步,事情是,它没有考虑按钮图像的大小,所以一些文本仍然没有显示,但组件调整大小,你有什么建议吗?

亚当.

答案:

这与 pack() 一起完成了任务,不需要重新验证。

    private class ComboBoxRenderer extends JLabel implements ListCellRenderer {
    private static final long serialVersionUID = 752379460716217273L;
    Dimension maxSize=new Dimension();
    @Override
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        setText(value.toString());
        Dimension size = getPreferredSize();
        if(maxSize.width<size.width) {
            maxSize.width=size.width;
            resolutionDescriptor_ComboBox.setPrototypeDisplayValue(value.toString());
        }
        if(maxSize.height<size.height)
            maxSize.height=size.height;

        return this;
    }

}

确保你设计的东西比这个更有效...

更新:

并且不需要 pack()!

亚当.

I have, the same issue with two components JTextField and JComboBox, I assume the solution I'm looking for would solve it for all components.

I have set the size of the components to default thus their size fits the initial content I supplied to it. when I change the content of a component to exceeds the region of the component, I cannot see the whole text, and I would like my component to resize to fit the text.

How can I accomplish that?

Update:

The pack() on the frame only enlarged the text field, how can I do the same and enlarge the combo box?

Update:

    private class ComboBoxRenderer extends JLabel implements ListCellRenderer {
    private static final long serialVersionUID = 752379460716217273L;
    Dimension maxSize=new Dimension();
    @Override
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        setText(value.toString());
        Dimension size = getPreferredSize();
        if(maxSize.width<size.width)
            maxSize.width=size.width;
        if(maxSize.height<size.height)
            maxSize.height=size.height;

        resolutionDescriptor_ComboBox.setPreferredSize(maxSize);
        return this;
    }

}

this works, not very efficient, but it is a first step, thing is, it does not take the button image into size considerations, so some of the text is still not shown, but the component resizes, do you have any suggestions?

Adam.

Answer:

This did the trick together with a pack(), no revalidation needed.

    private class ComboBoxRenderer extends JLabel implements ListCellRenderer {
    private static final long serialVersionUID = 752379460716217273L;
    Dimension maxSize=new Dimension();
    @Override
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        setText(value.toString());
        Dimension size = getPreferredSize();
        if(maxSize.width<size.width) {
            maxSize.width=size.width;
            resolutionDescriptor_ComboBox.setPrototypeDisplayValue(value.toString());
        }
        if(maxSize.height<size.height)
            maxSize.height=size.height;

        return this;
    }

}

make sure you design something more efficient then this...

Update:

and there is no need for the pack()!

Adam.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

彩扇题诗 2024-09-24 08:34:38

JComboBoxsetPrototypeDisplayValue(Object) 方法,用于根据参数的长度计算组件的首选宽度。尝试一下。

并使用 doLayout() 和一些 revalidate()repaint() 来代替 pack()

JComboBox has setPrototypeDisplayValue(Object) method that is used to calculate component's preferred width based on the length of the parameter. Try that.

And instead of pack() use doLayout() together with some revalidate() or repaint()

離殇 2024-09-24 08:34:38

在框架上执行 pack()

Do a pack() on the frame

浅唱ヾ落雨殇 2024-09-24 08:34:38

要调整组合框的大小,您可以尝试:

comboBox.setModel( comboBox.getModel() );

我相信这应该会导致重新计算组合框的首选大小。当然,您需要再次执行 pack() 。

编辑:

添加了一个简单的 SSCCE 来显示此功能:

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

public class ComboBoxTest3 extends JFrame implements ActionListener
{
    JComboBox comboBox;
    JTextField textField;

    public ComboBoxTest3()
    {
        String[] tabs = {"one", "two", "three", "four", "five", "six", "seven" };
        DefaultComboBoxModel model = new DefaultComboBoxModel(tabs);
        comboBox = new JComboBox( model );

        textField = new JTextField("hello");

        add(comboBox, BorderLayout.WEST );
        add(textField, BorderLayout.EAST );

        JButton button = new JButton("Pack");
        button.addActionListener( this );
        add(button, BorderLayout.SOUTH);
    }

    public void actionPerformed(ActionEvent e)
    {
        textField.setText("hello there!");
        comboBox.addItem("some longer text");
        comboBox.setModel( comboBox.getModel() );
        pack();
    }

    public static void main(String[] args)
    {
        JFrame frame = new ComboBoxTest3();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );
     }

}

To resize the combo box you can try:

comboBox.setModel( comboBox.getModel() );

I believe this should cause the preferred size of the combo box to be recalculated. Of course you would then need to do the pack() again.

Edit:

Added a simple SSCCE that shows this works:

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

public class ComboBoxTest3 extends JFrame implements ActionListener
{
    JComboBox comboBox;
    JTextField textField;

    public ComboBoxTest3()
    {
        String[] tabs = {"one", "two", "three", "four", "five", "six", "seven" };
        DefaultComboBoxModel model = new DefaultComboBoxModel(tabs);
        comboBox = new JComboBox( model );

        textField = new JTextField("hello");

        add(comboBox, BorderLayout.WEST );
        add(textField, BorderLayout.EAST );

        JButton button = new JButton("Pack");
        button.addActionListener( this );
        add(button, BorderLayout.SOUTH);
    }

    public void actionPerformed(ActionEvent e)
    {
        textField.setText("hello there!");
        comboBox.addItem("some longer text");
        comboBox.setModel( comboBox.getModel() );
        pack();
    }

    public static void main(String[] args)
    {
        JFrame frame = new ComboBoxTest3();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );
     }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文