Динамически изменять размер 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));
}

также, для справки:

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

FYI, он расширяет JPanel из-за того, как я добавляю его во фрейм:

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

так как лучше всего это сделать? Спасибо!

Edit: Вот файл области прокрутки. В моем 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));
    }

    }

он называется textA и 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());
    }



    }
9
задан Hovercraft Full Of Eels 5 June 2011 в 02:54
поделиться