2012-01-18 109 views
3

我有一个JTextField以及一个JFrame中的JTextArea。但是,当应用程序运行时,如果我最小化窗口,JTextField会被调整大小。它的高度通常是双倍或三倍,但它在每次调整大小方面并不一致。我不知道为什么会发生这种情况。我并不真正要求直接解决方案,只是可能导致问题的一些可能的事情。如果你能在这里帮助我,那会很棒。由于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); 
} 
+0

你在做什么代码明智的? – 2012-01-18 18:16:42

+0

这可能是您在布局中所做的事情。 – fireshadow52 2012-01-18 18:22:30

+1

为了更快提供更好的帮助,请发布[SSCCE](http://sscce.org/)。 – 2012-01-18 18:24:24

回答

相关问题