[英]How do I resize JScrollPane after resizing a JTextArea
我的JFrame由三个主要部分组成,顶部滚动窗格中的横幅,底部包含JTextArea中心,底部包含JTextField。 调整框架大小时,我调整了JTextArea中的列和行。 当使框架变大时,JTextArea会在视觉上扩展,但会移除滚动条。 然后,如果我缩小框架,则JTextArea保持不变。 这是我尝试调整JTextArea大小的地方。
frame.addComponentListener(new ComponentAdapter() {//Waits for window to be resized by user
public void componentResized(ComponentEvent e) {
uneditTextArea.setRows(((int)((frame.getHeight()-140)/18.8)));//sets Textarea size based on window size
uneditTextArea.setColumns(((int)((frame.getWidth()-100)/10.8)));
frame.revalidate();//refreshes screen
}
});
为什么ScrollPane无法重新适应TextField的大小更改。 如果需要,下面的代码其余部分。
public class window extends JFrame
{
private static JFrame frame = new JFrame("Lillian");
private static JButton inputButton = new JButton("Send");
private static JTextField editTextArea = new JTextField(46);
private static JTextArea uneditTextArea = new JTextArea(26,50);
private static JPanel logoPanel = new JPanel();//Input text window
private static JPanel itextPanel = new JPanel();//Input text window
private static JPanel submitPanel = new JPanel();//Submit Button
private static JPanel bottom = new JPanel();//will contain scrollpane
private static JPanel middle = new JPanel();//willcontain itextpanel & submitbutton
private static JPanel otextPanel = new JPanel();//Text Output
public static void runWindow()
{
ImageIcon logo = new ImageIcon("Lillian_resize.png");//banner
ImageIcon icon = new ImageIcon("Lillian_icon.png");//application icon
frame.setIconImage(icon.getImage());
frame.setSize(660,640);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
logoPanel.setSize(10,10);
JLabel logoLabel = new JLabel(logo);
final JScrollPane scrollPane = new JScrollPane(otextPanel);//adds text to panel will scrollbar
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);//scrollbar only apears when more text than screen
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);//scrollbar never apears
scrollPane.setBorder(BorderFactory.createEmptyBorder());
logoPanel.add(logoLabel);
submitPanel.add(inputButton);
itextPanel.add(editTextArea);
otextPanel.add(uneditTextArea);
frame.getContentPane().add(logoPanel,"North");
frame.getContentPane().add(middle);
frame.getContentPane().add(bottom,"South");
middle.add(scrollPane,"North");//adds panels to outer panel
bottom.add(itextPanel, "West");
bottom.add(submitPanel, "East");
uneditTextArea.setLineWrap(true);
uneditTextArea.setWrapStyleWord(true);
uneditTextArea.setEditable(false);
uneditTextArea.setCaretPosition(uneditTextArea.getDocument().getLength());
frame.revalidate();//refreshes screen
//---------------wait for action------------
frame.addComponentListener(new ComponentAdapter() {//Waits for window to be resized by user
public void componentResized(ComponentEvent e) {
uneditTextArea.setRows(((int)((frame.getHeight()-140)/18.8)));//sets Textarea size based on window size
uneditTextArea.setColumns(((int)((frame.getWidth()-100)/10.8)));
frame.revalidate();//refreshes screen
}
});
}
}
无需使用ComponentListener来调整组件的大小。 这就是您用来动态调整组件大小的布局管理器的工作。
您不应该首先将文本区域添加到JPanel。 相反,当使用文本区域时,通常可以使用以下代码将文本区域直接添加到JScrollPane的视口中:
JScrollPane scrollPane = new JScrollPane( textArea );
然后,使用以下代码将滚动窗格添加到框架:
frame.add(scrollPane, BorderLayout.CENTER);
如您所见,您也不应使用诸如“ Center”之类的硬编码字面量。 而是使用布局管理器提供的变量。 由于您使用的是BorderLayout,因此请使用BorderLayout类中定义的变量。
另外,您不应使用静态变量来创建GUI。 我建议您阅读Layout Manager上Swing教程中的这一节。 本教程将为您提供更多信息,示例代码将向您展示如何更好地构建程序结构,从而无需使用静态变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.