小编Nyx*_*Nyx的帖子

动态调整JScrollPane的大小?

我有两个文件.一个扩展JFrame,另一个扩展JPanel.每当我改变帧的大小时,无论是最大化,拖动还是其他什么,我都希望ScrollPane能够适应帧的当前大小.除此之外,还有一个顶级菜单栏和一个底栏,但为了简单起见,我把它们留了下来.

基本上,我希望它像记事本一样工作.

现在,我在框架上使用ComponentListener,在另一个类中调用setSize方法.setSize方法只是:

public void resize(int x, int y)
{
textA.setPreferredSize(new Dimension(x, y-50));

areaScrollPane.setPreferredSize(new Dimension(x,y-50));
}
Run Code Online (Sandbox Code Playgroud)

另外,供参考:

public void componentResized(ComponentEvent e)
 {
     textA.resize(panel.getWidth(),panel.getHeight());
  }
Run Code Online (Sandbox Code Playgroud)

仅供参考,它扩展了JPanel,因为我将其添加到框架中:

panel = (JPanel) this.getContentPane();
panel.setLayout(new BorderLayout());
panel.add(textA, BorderLayout.CENTER);
Run Code Online (Sandbox Code Playgroud)

那么最好的方法是什么?谢谢!

编辑:这是滚动窗格文件.它在我的主要部分称为textA.

    public class TextArea extends JPanel
    {

    JTextArea textA=new JTextArea(500,500);

    JScrollPane areaScrollPane = new JScrollPane(textA);
    Toolkit toolkit =  Toolkit.getDefaultToolkit ();
    Dimension dim = toolkit.getScreenSize();
    Dimension dim2=(new Dimension((int)(dim.getWidth()),(int)(dim.getHeight()-120)));
    public TextArea()
    {
        //textA.setLineWrap(true);
        //textA.setWrapStyleWord(true);
        textA.setEditable(true);
        textA.setForeground(Color.WHITE);

        textA.setBackground(Color.DARK_GRAY);
        this.setFont(null);



        areaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        areaScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        areaScrollPane.setMinimumSize(new Dimension(300,300));
        areaScrollPane.setSize(new Dimension(800,800));
        textA.setPreferredSize(dim2);
        areaScrollPane.setPreferredSize(dim2);
        areaScrollPane.setMaximumSize(dim2);
        add(areaScrollPane); …
Run Code Online (Sandbox Code Playgroud)

java resize dynamic jscrollpane jtextarea

9
推荐指数
1
解决办法
3万
查看次数

标签 统计

dynamic ×1

java ×1

jscrollpane ×1

jtextarea ×1

resize ×1