[英]Java Swing: JScrollPane doesn't work with Buffered Image in a JPanel
This is my JFrame
class which should display an image which has a height and width of 2000px.这是我的JFrame
class 应该显示一个高度和宽度为 2000 像素的图像。 The displayed area should only have a width
and height
of 800px
and I want to be able to use the scrollbar to reach the rest of the 2000px.显示的区域应该只有800px
的width
和height
,我希望能够使用滚动条到达2000px的rest。 But currently there is only the 800x800 pixel Frame and I can't scroll the picture.但目前只有 800x800 像素的帧,我无法滚动图片。 I can see the scrollbar but I can't scroll it.我可以看到滚动条,但我无法滚动它。 I only can see more of the picture if I drag the window larger.如果我将 window 拖得更大,我只能看到更多图片。
What my code is doing right now: creating the instance panel
of the Panel
class which displays an image with height: 2000px and width: 2000px.我的代码现在正在做什么:创建Panel
class 的实例panel
,它显示高度:2000 像素和宽度:2000 像素的图像。 Then I create a JScrollPane
and pass the panel
instance.然后我创建一个JScrollPane
并传递panel
实例。
If I don't use the lines setHorizontalScrollBarPolicy
, setVerticalScrollBarPolicy
I don't even see a scrollbar.如果我不使用setHorizontalScrollBarPolicy
和setVerticalScrollBarPolicy
行,我什至看不到滚动条。 If I use these two lines I see a scrollbar for horizontal and vertical scrolling but I can't use them because it seems like the picture is maximized.如果我使用这两行,我会看到一个用于水平和垂直滚动的滚动条,但我无法使用它们,因为看起来图片已最大化。
This is my Frame class:这是我的框架 class:
public class Frame extends JFrame {
public Frame() {
Panel panel = new Panel();
JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
add(scrollPane);
this.getContentPane().setPreferredSize(new Dimension(800, 800));
panel.generateImage();
setVisible(true);
pack();
}
And this is the class where the Image gets generated这是生成图像的 class
public class Panel extends JPanel {
private BufferedImage img;
public Panel() {
img= new BufferedImage(2000, 2000, BufferedImage.TYPE_INT_RGB);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, null);
}
public void generateImage () {
//...image gets generated here
repaint();
}
}
this is an example what it looks like right now.这是一个例子,它现在的样子。 The dog is just an example but it hopefully makes my problem more clearly-> I want to scroll through the image so that I can see other parts of the dog.这只狗只是一个例子,但它希望能让我的问题更清楚-> 我想滚动浏览图像,这样我就可以看到狗的其他部分。
You may be forgetting the panel's preferred size, since this is what is used to expand the JPanel "view" that is held within a JScrollPane's view-port.您可能忘记了面板的首选尺寸,因为这是用于扩展 JScrollPane 的视口中保存的 JPanel“视图”的尺寸。 Consider overriding getPreferredSize in the drawing JPanel by doing something like:考虑通过执行以下操作来覆盖绘图 JPanel 中的 getPreferredSize:
@Override
public Dimension getPreferredSize() {
if (img == null) {
return super.getPreferredSize();
} else {
int w = img.getWidth();
int h = img.getHeight();
return new Dimension(w, h);
}
}
Of course, you must import java.awt.Dimension
当然必须导入java.awt.Dimension
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.