Bla*_*ein 1 java swing jframe jscrollpane jtextfield
我最初在这里发了一个问题
我发现只有JScrollPane存在时,JTextField才会调整大小.换句话说,我可以最小化并最大化它我想要的所有内容,直到滚动条出现(因为有太多文本适合窗口).之后,如果我最小化窗口,JTextField的垂直尺寸将增加三倍.希望这些新信息能够引发一个可能的解决方案.如果您有任何想法,请发帖,谢谢.
我不是一个会使用GridBagLayout的人,看起来整个问题都在于你的布局设置.
好像你错过了阅读GridBagLayout教程,它明确指出"即使组件有不同的约束,也可以为多个组件重用相同的GridBagConstraints实例.但是,建议你不要重复使用GridBagConstraints,因为这个如果你忘记重置每个新实例的字段,很容易导致你引入微妙的错误."
所以看完之后我对你自己的代码做了一点改进,希望你不要介意:-)
我所做的是,我制作了两个不同的GridBagConstraints对象,每个滚动窗格和文本域,以便它们可以有不同的值.因为你为两个组件使用相同的对象,即
c.weightx = 1.0;
c.weighty = 1.0;
Run Code Online (Sandbox Code Playgroud)
现在直到滚动条没有出现一切顺利,但一旦它确实并且你最小化窗口,然后恢复窗口,然后重新绘制屏幕上的窗口,因为两个组件的权重值是相同的,因此,他们试图在Y轴方面占据窗口的相等面积.
因此,为了避免这种情况,我在Y 轴上制作了两个具有不同重量值的对象,其中强大的JTextArea已经提供了更高的值,即weighty = 0.8并且JTextField在其目前的使用方面不是那么强大因此,它的权重为权重= 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();
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
941 次 |
| 最近记录: |