java – 将JScrollPane添加到显示图像的面板
发布时间:2020-12-15 08:31:15 所属栏目:Java 来源:网络整理
导读:我有这个小程序在apanel上显示图像..但我无法添加滚动到它..这是我的代码 扩展jpanel以显示图像的类: public class ShowPanel extends JPanel{public ShowPanel(BufferedImage image,int height,int width) { this.image = image; this.height = height; th
我有这个小程序在apanel上显示图像..但我无法添加滚动到它..这是我的代码
扩展jpanel以显示图像的类: public class ShowPanel extends JPanel{ public ShowPanel(BufferedImage image,int height,int width) { this.image = image; this.height = height; this.width = width; //this.setBounds(width,width,height); } public void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(image,height,null); } public void setImage(BufferedImage image,int width) { this.image = image; this.height = height; this.width = width; } private BufferedImage image; private int height; private int width; } 还有一个主框架样本,它有另一个名为imageContainer的面板来保存显示面板: public class MainFrame extends javax.swing.JFrame { /** Creates new form MainFrame */ public MainFrame() { initComponents(); image = null; path = "PantherOnLadder.gif"; image = Actions.loadImage(path); showPanel = new ShowPanel(image,0); spY = toolBar.getY() + toolBar.getHeight(); showPanel.setBounds(0,spY,image.getWidth(),image.getHeight()); showPanel.repaint(); imageContainer.add(showPanel); JScrollPane scroller = new JScrollPane(imageContainer); scroller.setAutoscrolls(true); } 那我在这里做的错误是什么? 解决方法
添加到滚动窗格的组件的首选大小超过滚动窗格的大小时,滚动条会自动显示.您的自定义面板没有首选大小,因此滚动条永远不会出现.一种解决方案是仅返回等于图像大小的优选大小.
当然,我永远不明白为什么人们会为了这种类型的自定义绘画而烦恼.不需要自定义类.只需从BufferedImage创建一个ImageIcon,然后将Icon添加到JLable,然后将标签添加到滚动窗格,您将不会遇到任何这些问题.进行自定义绘画的唯一原因是,如果您需要缩放图像或提供其他一些奇特的效果. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |