JScrollPane與JPanel 捲軸 解決canvas的捲軸問題

來源:互聯網
上載者:User

標籤:c   style   class   blog   code   java   

當用JScrollPane和JPanel顯示圖片時,需要將JPanel的PrefferedSize及時傳遞給JScrollPane,否則容易出現JScrollPane的捲軸無法與圖片大小匹配的問題,下面是從Matrix網上搜到的一段代碼供參考使用:

 

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

因為scrollPane是根據裡面的子控制項的preferredSize來確定捲軸的。
所以圖片變化而捲軸無響應的問題在於讓image尺寸變化時,scrollPane能得到新的資料。

解決方式有兩種,一種是改變大小時重新設定JPanel的preferred size;
一種是重載getPreferredSize()方法,按照需要返回,樓主的情況可以返回當前
圖片的大小的Dimension。

文章出自:http://xu.wy.blog.163.com/blog/static/11568296200786103315608/

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.