动态调整JScrollPane的大小?

9
我有两个文件。一个扩展了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));
}

此外,供参考:

public void componentResized(ComponentEvent e)
 {
     textA.resize(panel.getWidth(),panel.getHeight());
  }

顺便提一下,这里将其扩展为JPanel是因为我将其添加到框架中的方式:

panel = (JPanel) this.getContentPane();
panel.setLayout(new BorderLayout());
panel.add(textA, BorderLayout.CENTER);

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

编辑:这是滚动窗格文件。在我的主文件中它被称为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);
     }


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

    }

并且主要的:

    public class JEdit extends JFrame implements ComponentListener

    {
    TextArea textA=new TextArea();
    JPanel panel;
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {

       JEdit run=new JEdit();

    }

    public JEdit()
    {

        setTitle("JEdit");
        setLayout(new BorderLayout());
        setSize(1100, 1000);

        this.setMinimumSize(new Dimension(100,100));
        //setBackground(Color.BLACK);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
           System.out.println("error1");
        } catch (InstantiationException ex) {
            System.out.println("error2");
        } catch (IllegalAccessException ex) {
           System.out.println("error3");
        } catch (UnsupportedLookAndFeelException ex) {
            System.out.println("error4");
        }

         panel = (JPanel) this.getContentPane();
         panel.setLayout(new BorderLayout());
         //TopBar top=new TopBar();
        // PositionBar posB=new PositionBar();
         panel.add(textA, BorderLayout.CENTER);




       // add(top,BorderLayout.NORTH);
       // add(posB,BorderLayout.SOUTH);




        addComponentListener(this);
        setVisible(true);

    }



    public void componentResized(ComponentEvent e)
    {
         textA.resize(panel.getWidth(),panel.getHeight());
    }

    public void componentMoved(ComponentEvent e) {
    textA.resize(panel.getWidth(),panel.getHeight());
    }

    public void componentShown(ComponentEvent e) {
    textA.resize(panel.getWidth(),panel.getHeight());
    }

    public void componentHidden(ComponentEvent e) {
         textA.resize(panel.getWidth(),panel.getHeight());
    }



    }

1
你被告知不需要设置任何首选大小或使用任何类型的监听器。你还被告知可以直接将滚动面板添加到框架中,但是你却将其添加到了JPanel。如果你想得到帮助,请努力尝试这些建议! - camickr
2个回答

7
关于您发布的代码,首先请删除所有对setSize的调用,因为在使用布局管理器时这些调用通常不起作用。此外,请删除所有组件监听器相关的代码,因为由于您使用了布局管理器来调整大小,所以这些代码是多余的。但我发现最大的问题是您允许TextArea JPanel使用其默认布局,即FlowLayout,这将防止它所包含的JScrollPane进行调整大小。请给该类一个BorderLayout(或者更好的方法是直接从该类返回一个JScrollPane),然后就完成了。例如,通过快速修改并重命名类以防止与标准Java类发生冲突,
import java.awt.*;
import javax.swing.*;

@SuppressWarnings("serial")
public class JEdit2 extends JFrame  {
   TextArea2 textA = new TextArea2();
   JPanel panel;

   public static void main(String[] args) {
      new JEdit2();
   }

   public JEdit2() {
      setTitle("JEdit 2");
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      panel = (JPanel) this.getContentPane();
      panel.add(textA, BorderLayout.CENTER);

      pack(); //!! added
      setLocationRelativeTo(null);
      setVisible(true);
   }

}

@SuppressWarnings("serial")
class TextArea2 extends JPanel {
   JTextArea textA = new JTextArea(500, 500); // !! this is one friggin' huge JTextArea!
   JScrollPane areaScrollPane = new JScrollPane(textA);

   public TextArea2() {
      textA.setEditable(true);
      textA.setForeground(Color.WHITE);
      textA.setBackground(Color.DARK_GRAY);
      this.setFont(null);

      setLayout(new BorderLayout()); //!! added
      add(areaScrollPane, BorderLayout.CENTER);
   }
}

完美!如果您不介意,pack()的目的是什么?谢谢! - Nyx

3

默认情况下,JFrame使用BorderLayout布局(因此无需重置)。您只需要将JScrollPane添加到BorderLayout的中心位置即可自动调整大小。

基本代码如下:

JTextArea textArea = new JTextArea(...);
JScrollPane scrollPane = new JScrollPane();
frame.add(scrollPane, BorderLayout.CENTER);

我不确定为什么你要将文本区域添加到中心位置。

没有必要对任何组件使用setPreferredSize()或在任何组件上使用监听器。

如果需要更多帮助,则需要发布SSCCE


textA是我的JScrollPane文件。我没有说明清楚。 - Nyx
您将滚动窗格添加到面板而不是框架中,因此面板会被调整大小,而不是滚动窗格。 - camickr
好的,我拿了你的代码并在我的主程序中尝试了一下,它像你说的那样工作得很好。那么我该如何修改我的textArea文件才能做到同样的事情呢?它应该扩展JTextArea还是其他什么?再次感谢。 - Nyx
根本不需要一个TextArea类。您没有为文本区添加任何新功能。只需像我所做的那样,创建JtextArea并将其添加到JScrollPane中即可。 - camickr

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接