日期:2014-05-16  浏览次数:20487 次

JPanel放置到JScrollPane里面并显示滚动条

一开始,我是知道不能直接在JScrollPane对象里用add()的,需要获取它的视图板
getViewport();这是一个JViewport对象,然后在这上面添加,而且它不能添加轻量级组件。
? 我弄了一个JPanel,上面用网格布局10行1列的,本来想,把这个板添加到Viewport上就行了,结果发现,没有出现滚动条,于是我设置Viewport的布局方式,用尽了都不行。比如说用NULL布局吧,然后把Panel设长,结果Panel确实变长了,但滚动条不出来
?
? 经过费心苦想,我直接设置了一下Panel的setPerferredSize(Dimension dim),dim的参数我用的是Viewport的getDemension所得。这下好了,哈哈,安装上去实现了滚动。
?
===============
附上一个例子
?
因为scrollPane是根据里面的子控件的preferredSize来确定滚动条的。
让image尺寸变化时,scrollPane能得到新的数据。
解决方式有两种,一种是改变大小时重新设置JPanel的preferred size;
一种是重载getPreferredSize()方法,按照需要返回,楼主的情况可以返回当前
图片的大小的Dimension。
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class ScrollPaneDemo extends JFrame implements MouseListener {

??????? ImagePanel jp;
???????
??????? JScrollPane jsp;

??????? public ScrollPaneDemo() {
??????????????? super("JScrollPane Demo");
??????????????? jp = new ImagePanel();
??????????????? jp.setPreferredSize(new Dimension(600, 400));
??????????????? jsp = new JScrollPane(jp);
??????????????? jp.addMouseListener(this);
??????????????? getContentPane().add(jsp);
??????????????? setSize(640, 480);
??????????????? setDefaultCloseOperation(EXIT_ON_CLOSE);
??????????????? setVisible(true);
??????? }

??????? public void mouseClicked(MouseEvent e) {
??????????????? System.out.println("mouseClicked");
??????????????? jp.enlarge();
??????????????? jp.setPreferredSize(jp.getPreferredSize());
??????????????? jsp.validate();
??????? }

??????? public void mousePressed(MouseEvent e) {
??????????????? // System.out.println("mousePressed");
??????? }

??????? public void mouseReleased(MouseEvent e) {
??????????????? // System.out.println("mouseReleased");
??????? }

??????? public void mouseEntered(MouseEvent e) {
??????????????? // System.out.println("mouseEntered");
??????? }

??????? public void mouseExited(MouseEvent e) {
??????????????? // System.out.println("mouseExited");
??????? }

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

class ImagePanel extends JPanel {
??????? private Image image;

??????? private Dimension theSize = new Dimension(600, 400);

??????? public void setImage(Image image) {
??????????????? this.image = image;
??????????????? this.repaint();
??????? }

??????? public Image getImage() {
??????????????? return this.image;
??????? }

??????? public void paintComponent(Graphics g) {
??????????????? ImageIcon img = new ImageIcon("Winter.jpg");
??????????????? g.drawImage(img.getImage(), 0, 0, theSize.width, theSize.height, null);
??????? }

??????? public void enlarge() {
??????????????? theSize.width = (theSize.width * 101) / 100;
??????????????? theSize.height = (theSize.height * 101) / 100;
??????????????? setSize(theSize);
??????? }
???????
??????? public Dimension getPreferredSize() {
??????????????? return this.theSize;
??????? }
}