动态调整 JScrollPane 的大小?

发布于 2024-11-13 06:59:07 字数 3981 浏览 3 评论 0原文

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

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

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



    }

I Have two files. One extends JFrame, and another Extends JPanel.
Whenever I change the size of the frame, whether it be maximizing, dragging, whatever, i want the ScrollPane to fit itself to the current size of the frame.
There's more to it, there's a top menubar and a bottom bar as well, but i left those out for simplicity.

Essentially, i want it to work like notepad.

right now, I use a ComponentListener on the frame that calls a setSize method in the the other class.
The setSize method is just:

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

areaScrollPane.setPreferredSize(new Dimension(x,y-50));
}

also, for reference:

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

FYI, it extends JPanel because of the way I add it to the frame:

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

so what's the best way to do this?
Thanks!

Edit: Here's the scrollpane file. It's called textA in my main.

    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));
    }

    }

and the main:

    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());
    }



    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

待天淡蓝洁白时 2024-11-20 06:59:07

关于您发布的代码,首先要删除对 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);
   }
}

Regarding the code you've posted, for one get rid of all calls to setSize -- these are generally not honored when using layout managers and get rid of all of your ComponentListener stuff as it's superfluous since you are using layout managers to resize things. The biggest problem I see though is that your allow your TextArea JPanel to use its default layout, which is FlowLayout, and doing so will prevent the JScrollPane that it holds from resizing. Give this class a BorderLayout (or better simply return a JScrollPane from the class), and you're set. e.g. with quick modifications and with renaming of classes to prevent clashes with the standard Java classes,

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);
   }
}
甜扑 2024-11-20 06:59:07

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

基本代码是:

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

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

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

如果您需要更多帮助,那么您需要发布 SSCCE

By default a JFrame uses a BorderLayout (so there is no need to reset it). All you need to do is add the JScrollPane to the CENTER of the BorderLayout and it will resize automatically.

And the basic code would be:

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

I'm not sure why you are adding the text area to the center.

There is no need to use setPreferredSize() on any component or use a listener on any component.

If you need more help then you need to post a SSCCE.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文