gpt4 book ai didi

java - JTextField 在最小化时调整大小

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:00:13 24 4
gpt4 key购买 nike

我在 JFrame 中有一个 JTextField 和一个 JTextArea。但是,当应用程序运行时,如果我最小化窗口,则 JTextField 会调整大小。它的高度通常是原来的两倍或三倍,但每次调整大小的方式并不一致。我不知道为什么会这样。我并不是真的要求一个直接的解决方案,只是一些可能导致问题的事情。如果你能在这里帮助我,那就太好了。谢谢

编辑:这是我用来初始化它的代码:

public class Console extends JPanel implements ActionListener, Serializable {
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(false);
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.
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;

c.fill = GridBagConstraints.HORIZONTAL;
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
add(scrollPane, c);
add(textField, c);
}

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 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.add(new Console(m_game));

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

最佳答案

关于java - JTextField 在最小化时调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8915178/

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