gpt4 book ai didi

Java:JTextField(仍然)在最小化时调整大小(新信息)

转载 作者:行者123 更新时间:2023-11-29 07:16:42 26 4
gpt4 key购买 nike

我最初发布了一个问题 here

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

最佳答案

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

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

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

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

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

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

因此,为了避免这种情况,我沿 Y 轴 制作了两个具有不同权重值的对象,其中为 JTextArea 强大 提供了更高的值,即weighty = 0.8JTextField 就其在当前场景中的使用而言不是那么强大,因此它的权重为 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();
}
});
}
}

关于Java:JTextField(仍然)在最小化时调整大小(新信息),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8945812/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com