【问题标题】:Dynamically Resize a JScrollPane?动态调整 JScrollPane 的大小?
【发布时间】:2011-06-05 02:39:21
【问题描述】:

我有两个文件。一个扩展 JFrame,另一个扩展 JPanel。 每当我改变框架的大小时,无论是最大化、拖动还是其他,我都希望 ScrollPane 能够适应框架的当前大小。 还有更多,还有一个顶部菜单栏和一个底部栏,但为了简单起见,我将它们省略了。

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

现在,我在调用另一个类中的 setSize 方法的框架上使用 ComponentListener。 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);

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

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



    }

【问题讨论】:

  • 您被告知无需设置任何首选大小或使用任何类型的侦听器。您还告诉您将滚动窗格直接添加到框架中,但您将其添加到 JPanel。如果您需要帮助,请努力实际尝试这些建议!

标签: java dynamic resize jscrollpane jtextarea


【解决方案1】:

关于您发布的代码,首先要摆脱对 setSize 的所有调用——在使用布局管理器时这些通常不被尊重,并且摆脱所有 ComponentListener 的东西,因为它是多余的,因为您使用布局管理器来调整大小。我看到的最大问题是您允许您的 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(); 的目的是什么?谢谢!
【解决方案2】:

默认情况下,JFrame 使用 BorderLayout(因此无需重新设置)。您需要做的就是将 JScrollPane 添加到 BorderLayout 的 CENTER,它会自动调整大小。

基本代码是:

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

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

无需在任何组件上使用 setPreferredSize() 或在任何组件上使用监听器。

如果您需要更多帮助,请发帖 SSCCE

【讨论】:

  • textA 是我的 jscrollpane 文件。没有澄清。
  • 您将滚动窗格添加到面板,而不是框架,因此调整了面板的大小,而不是滚动窗格。
  • 好吧,我拿了你的代码并在我的 main 中尝试了它,它就像你说的那样工作。那么我将如何修改我的 textArea 文件以使其执行相同的操作?它应该扩展 JTextArea 还是什么?再次感谢。
  • 根本不需要 TextArea 类。您没有向文本区域添加任何新功能。只需做我所做的,创建 JtextArea 并将其添加到 JScrollPane。
猜你喜欢
  • 2020-08-16
  • 2021-11-30
  • 2011-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-07
相关资源
最近更新 更多