将 JScrollPane 添加到显示图像的面板

发布于 2024-08-10 12:16:06 字数 1309 浏览 6 评论 0原文

我有一个用于在 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, width, height);

}

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawImage(image, height, width, null);
}

public void setImage(BufferedImage image, int height, 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, 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);


}

那么我在这里犯了什么错误?

i have this little program for showing images on apanel.. but i'm not able to add a scroll to it.. and this is my code

the class that extends jpanel to show the image:

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, width, height);

}

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawImage(image, height, width, null);
}

public void setImage(BufferedImage image, int height, int width) {
    this.image = image;
    this.height = height;
    this.width = width;

}

private BufferedImage image;
private int height;
private int width;

}

and a sample of the main Frame that has another panel called imageContainer to hold the showing panel:

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, 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);


}

so what's the mistake i did here ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

东京女 2024-08-17 12:16:06

当添加到滚动窗格的组件的首选大小超过滚动窗格的大小时,滚动条会自动显示。您的自定义面板没有首选尺寸,因此滚动条永远不会出现。一种解决方案是仅返回等于图像大小的首选大小。

当然,我永远不明白为什么人们要费尽心思去做这种类型的定制绘画。不需要自定义类。只需从 BufferedImage 创建一个 ImageIcon,然后将图标添加到 JLable,然后将标签添加到滚动窗格,您就不会遇到任何这些问题。进行自定义绘画的唯一原因是您需要缩放图像或提供其他一些奇特的效果。

The scrollbars appear automatically when the preferred size of the component added to the scrollpane exceeds the size of the scroll pane. Your custom panel doesn't have a preferred size so the scrollbars never appear. One solution is to just return a preferred size equal to the size of the image.

Of course I never understand why people go to all the trouble to do this type of custom painting. There is no need for a custom class. Just create an ImageIcon from the BufferedImage and then add the Icon to a JLable and then add the label to the scrollpane and you won't have any of these problems. The only reason to do custom painting is if you need to scale the image or provide some other fancy effect.

还给你自由 2024-08-17 12:16:06

由于“imagecontainer”是添加到可滚动面板中的类,因此该面板需要超过一定大小才能使内容可滚动。您应该将“showPanel”直接放入可伸缩容器中。

Since the 'imagecontainer' is the class being added to your scrollable panel, that is the panel which needs to exceed a certain size to make things scrollable. You should place the 'showPanel' directly into a scollable container.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文