【问题标题】:Java: JTextField (still) resizing upon minimize (new info)Java:JTextField(仍然)在最小化时调整大小(新信息)
【发布时间】:2012-01-20 18:01:21
【问题描述】:

我最初发布了一个问题here

我发现 JTextField 仅在 JScrollPane 存在时才调整大小。换句话说,我可以最小化和最大化它,直到出现滚动条(因为有太多文本无法放入窗口)。之后,如果我最小化窗口,JTextField 的垂直大小会增加三倍。希望这些新信息将引发一个可能的解决方案。有什么想法请留言,谢谢。

【问题讨论】:

  • 你有什么问题? :)
  • 我不想再次发布整个事情,但问题在这里:stackoverflow.com/questions/8915178/…
  • 该问题的答案已被接受。
  • 你想为标准LayoutManager铺设的JComponent设置最小尺寸???
  • 如果您想尽快获得更好的帮助,为什么不发布一个合适的 SSCCE,而不是多次发布问题?

标签: java swing jframe jscrollpane jtextfield


【解决方案1】:

我不是一个会使用 GridBagLayout 的人,似乎整个问题都在于您的布局设置。

好像你错过了阅读GridBagLayout Tutorials,它明确指出“可以为多个组件重用相同的GridBagConstraints实例,即使组件有不同的约束。但是,建议你不要重复使用 GridBagConstraints,因为如果您忘记为每个新实例重置字段,这很容易导致您引入细微的错误。”

所以在阅读了我自己对你的代码做了一些小的改进后,希望你不会介意:-)

我所做的是,我制作了两个不同的 GridBagConstraints 对象,滚动窗格和文本字段各一个,以便它们可以具有不同的值。由于您对两个组件使用相同的对象,即

c.weightx = 1.0;
c.weighty = 1.0;

现在直到滚动条没有出现,一切顺利,但是一旦出现,你最小化窗口,然后恢复窗口,然后在屏幕上重新绘制窗口,因为权重的值是两个组件相同,因此它们尝试在 Y 轴方面占据窗口的相等区域。

因此,为了避免这种情况,我沿 Y 轴 制作了两个具有不同权重值的对象,其中 JTextArea beingmighty 提供了更高的值,即weighty = 0.8 和 JTextField 在当前场景中的使用并没有那么强大,因此它被赋予了 weighty = 0.2 的权重.

为了您的额外理解,我再次重建了代码。为不同的组件使用不同的 GridBagConstraint 对象是解决问题的方法。

现在看看代码:

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class Console extends JPanel implements ActionListener
{
    boolean ready = false;
    protected JTextField textField;
    protected JTextArea textArea;
    private final static String newline = "\n";
    //Game m_game;
    Thread t;

    public Console(/*Game game*/) 
    {
        super(new GridBagLayout());

        //m_game = game;
        textField = new JTextField(20);
        textField.setPreferredSize(null);
        textField.addActionListener(this);

        textArea = new JTextArea(20, 60);
        textArea.setEditable(true);
        textArea.setWrapStyleWord(true);
        textArea.setLineWrap(true);
        Font comic = new Font("Serif", Font.PLAIN, 14);
        textArea.setFont(comic);
        JScrollPane scrollPane = new JScrollPane(textArea);

        //Add Components to this panel.
            // This first object serves as providing values to the TEXTAREA.
        GridBagConstraints c = new GridBagConstraints();
        c.gridwidth = GridBagConstraints.REMAINDER;

        c.fill = GridBagConstraints.HORIZONTAL;
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1.0; 
        c.weighty = 0.8;// This being mighty has been given weight as 0.8, the highest.

        // This second object serves as providing values to the TEXTFIELD.
        GridBagConstraints c1 = new GridBagConstraints();
        c1.gridwidth = GridBagConstraints.REMAINDER;

        c1.fill = GridBagConstraints.HORIZONTAL;
        c1.fill = GridBagConstraints.BOTH;
        c1.weightx = 1.0;
        c1.weighty = 0.2;  // Since this has to occupy less space, hence weight is less also.
        add(scrollPane, c);
        add(textField, c1);
    }

    public void actionPerformed(ActionEvent evt) 
    {
        String text = textField.getText();
        //m_game.input = text;
        textField.selectAll();
        textArea.setCaretPosition(textArea.getDocument().getLength());
        //m_game.wait = false;
        /*synchronized (m_game)
        {
            ready = true;
            m_game.notifyAll();
        }*/
    }

    public static void createAndShowGUI() 
    {
        //Create and set up the window.
        JFrame frame = new JFrame("Game");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Add contents to the window.
        frame.setContentPane(new Console());

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    createAndShowGUI();
                }
            });
    }
}   

【讨论】:

  • @QuanChi : 欢迎您,很高兴我能以某种方式帮助您。问候
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多