简体   繁体   English

Java:JTextField(仍)根据最小化调整大小(新信息)

[英]Java: JTextField (still) resizing upon minimize (new info)

I originally posted a question here 我最初在这里发布了一个问题

I've discovered that the JTextField only resizes if the JScrollPane exists. 我发现JTextField仅在JScrollPane存在时才调整大小。 In other words, I can minimize and maximize it all I want until the scrollbar appears (because there is too much text to fit into the window). 换句话说,我可以最小化和最大化所需的所有内容,直到滚动条出现(因为窗口中容纳的文本过多)。 After that if I minimize the window, the JTextField triples in vertical size. 之后,如果我最小化窗口,则JTextField的垂直大小将增加三倍。 Hopefully this new information will spark a possible solution. 希望这些新信息将引发一个可能的解决方案。 Please post if you have any ideas, thank you. 如果您有任何想法请发表,谢谢。

I am not much of a guy who will use GridBagLayout, seems like the whole issue is with your Layout settings in that. 我不是一个会使用GridBagLayout的家伙,似乎整个问题都与您的Layout设置有关。

Seems like you missed to read the GridBagLayout Tutorials , it clearly states that " it is possible to reuse the same GridBagConstraints instance for multiple components, even if the components have different constraints. However, it is recommended that you do not reuse GridBagConstraints, as this can very easily lead to you introducing subtle bugs if you forget to reset the fields for each new instance." 似乎您错过了阅读GridBagLayout教程 ,它清楚地表明: “即使多个组件具有不同的约束,也可以将相同的GridBagConstraints实例重用。但是,建议您不要重用GridBagConstraints,因为这如果您忘记为每个新实例重置字段,很容易导致您引入细微的错误。”

So after reading that I made a small improvement in your code of my own, hope you wont mind that :-) 因此,在阅读完我自己的代码后,我做了一些小的改进,希望您不要介意:-)

What i did, is that, I made two different objects of GridBagConstraints, one each of your scrollpane and textfield, so that they can have different values. 我所做的是,我制作了GridBagConstraints的两个不同对象,您的滚动窗格和文本字段各一个,以便它们可以具有不同的值。 Since you were using the same object for both the components ie 由于您对两个组件都使用相同的对象,即

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

Now till scroll bar doesn't appears all goes well, but once it does and you minimize the window, and then restores the window, then while repainting the window on the screen, since the values for weights is the same for both the components, hence they try to occupy equal area of the window in terms of Y-axis. 现在直到滚动条不出现为止,一切都进行良好,但是一旦完成,您将窗口最小化,然后还原窗口,然后在屏幕上重新绘制窗口,因为两个组件的权重值相同,因此,他们试图在Y轴上占据窗口的相等面积。

So in order to avoid that, I made two objects with different values for weights along Y-Axis , where JTextArea being mighty has been provided with the higher value ie weighty = 0.8 and JTextField being not so mighty in terms of its use in the present scenario, hence it's been given a weight of weighty = 0.2 . 因此,为了避免这种情况,我在Y 轴上制作了两个权重值不同的对象,其中为JTextArea强大提供了更高的值,即weighty = 0.8JTextField就目前的使用而言没有那么强大场景,因此它的权重为weighty = 0.2

I had rebuild the code again for your extra understanding. 为了您的进一步理解,我再次重建了代码。 Using different GridBagConstraint objects for different components is the way out of the mess. 为不同的组件使用不同的GridBagConstraint对象是摆脱困境的出路。

Do have a look at the code now : 现在看一下代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM