如何让我的JPanel自动将其滚动条移动到最右边?

时间:2015-05-01 12:00:55

标签: java swing

我有一个JScroll窗格处理我的JFrame,我有一个自动扩展首选大小的功能,以适应我绘制到屏幕上的图像数量。

任何人都可以告诉我如何将我的视图设置为最右边并随着我的滚动区域扩展而扩展。

    //implementing a scroll pane to the drawing section and 
    //attaching it to the canvas object 
    JScrollPane scrollPane = new JScrollPane(canvas);
    scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);



    //Scroll pane for text section
    JScrollPane scrollText = new JScrollPane(textArea);
    scrollText.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    scrollText.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

    //screen size
    this.setSize(900, 390);

    //adding panels to the screen 

    this.add(buttonPanel, BorderLayout.SOUTH);
    this.add(scrollPane, BorderLayout.CENTER);
    this.add(scrollText, BorderLayout.EAST);

    //DO NOT EVER RESIZE IT WILL MESS UP 
    //MORE THAN I CARE TO ADMIT OR TRY TO FIX :)
    this.setResizable(false);
    setVisible(true); // Display the window

}

public void setCanvasPreferredSize(Dimension size){
    this.canvas.setPreferredSize(size);
    canvas.revalidate();
    canvas.repaint();

}

0 个答案:

没有答案