Java:JScrollPane 不适用于 GridBagLayout

发布于 2024-10-03 01:07:15 字数 1277 浏览 8 评论 0原文

在我的 Java 应用程序中,我正在编写一个用于查看 PDF 文件的组件。我有一个非常巧妙的实现,用户可以单击 PDF 并将其拖动以查看屏幕上不适合的区域。但我的老板不喜欢它,所以现在我必须使用滚动条。所以我做了显而易见的事情,只是将其放入 JScrollPane 中,但几乎无论我做什么,它都不起作用。

PDF 只是转换为 BufferedImage,然后将其转换为 ImageIcon,以便我可以将其添加到 JLabel,然后将其添加到 JScrollPane。

我有一个 PDFViewer 类,它是 JScrollPane 的子类,重要的代码在这里:

private void drawPDF() {
    PDFRenderer renderer = new PDFDrawer(pdfFile);
    BufferedImage image = renderer.makeImage(page);
    JLabel img = new JLabel(new ImageIcon(image));
    this.setViewportView(img);
}

现在我有一个单独的类,它是 JFrame 的子类,我需要将 PDFViewer 添加到其中。 只要我不使用布局并将 PDFViewer 直接添加到 JFrame 中,它就可以工作。如果我只是将 JScrollPane 添加到 JPanel,然后将 JPanel 添加到 JFrame,滚动条就会消失,看起来就像我直接添加了 JLabel。图像太大,很容易被切断。
我还需要向框架添加一些控件,因此我设置了一个非常基本的 GridBagLayout,其中 PDFViewer 作为唯一添加的组件。通过以下代码,我得到一个窗口,看起来像这样

GridBagLayout glayout = new GridBagLayout();
GridBagConstraints c;
setLayout(glayout);
PDFViewer viewer = new PDFViewer("foo.pdf");
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.gridheight = 1;
c.gridwidth = 1;
add(viewer, c);
setVisible(true);

为什么当我只是将 JScrollPane 添加到布局而不是直接添加到 JFrame 时,它​​会变得如此平滑?我发现它可以与 GridLayout 一起使用,但 GridLayout 不是我想要的。

In my Java application, I'm writing a component that is used to view PDF files. I had a pretty slick implementation where the user could click on the PDF and drag it to view the areas that didn't fit on the screen. But my boss didn't like it, so now I have to use scroll bars. So I did the obvious thing and just put it into a JScrollPane, but almost no matter what I do it refuses to work.

The PDF just gets converted to a BufferedImage and then I convert it to an ImageIcon so I can add it to a JLabel which gets added to a JScrollPane.

I have a PDFViewer class which subclasses JScrollPane, and the important code is here:

private void drawPDF() {
    PDFRenderer renderer = new PDFDrawer(pdfFile);
    BufferedImage image = renderer.makeImage(page);
    JLabel img = new JLabel(new ImageIcon(image));
    this.setViewportView(img);
}

Now I have a separate class which subclasses JFrame that I need to add my PDFViewer to.
It works as long as I don't use a layout and add the PDFViewer directly to the JFrame. If I even just add the JScrollPane to a JPanel and then add the JPanel to the JFrame the scroll bars disappear and it looks like I just added the JLabel directly. The image is too big for this, and it gets cut off easily.
I need to add some controls to the frame as well, so I set up a really basic GridBagLayout with the PDFViewer as the only component being added. And with the following code I get a window that looks like this.

GridBagLayout glayout = new GridBagLayout();
GridBagConstraints c;
setLayout(glayout);
PDFViewer viewer = new PDFViewer("foo.pdf");
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.gridheight = 1;
c.gridwidth = 1;
add(viewer, c);
setVisible(true);

Why does the JScrollPane get smooshed like that when I just simply add it to a layout instead of directly to the JFrame? I found out that it works with GridLayout, but a GridLayout is not what I want.

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

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

发布评论

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

评论(5

眼眸里的那抹悲凉 2024-10-10 01:07:15

您至少需要一个组件的weightx/y 设置为非零值,GridBagLayout 才能正常工作。

您需要指定

c.weightx = 1;
c.weighty = 1;
c.fill = GridBagConstraints.BOTH;

这意味着它将占用其他组件未使用的所有可用空间。我建议阅读 GridBagLayout 以获取更多信息。

You need at least ONE component with the weightx/y set to a non zero value for GridBagLayout to work.

You need to specify

c.weightx = 1;
c.weighty = 1;
c.fill = GridBagConstraints.BOTH;

This means that it will take all available space that is not used by other components. I suggest reading up on GridBagLayout for more information.

大姐,你呐 2024-10-10 01:07:15

尝试添加:

c.fill = GridBagConstraints.BOTH;

这应该确保您在调整大小时在两个方向上调整面板大小。顺便说一句,如果这是唯一的组件,请考虑使用 BorderLayout 并将该组件添加到 BorderLayout.CENTER

Try adding:

c.fill = GridBagConstraints.BOTH;

This should ensure that your panel is resized in both directions when you resize. Incidentally if this is the only component then consider using BorderLayout and adding the component to BorderLayout.CENTER.

伪心 2024-10-10 01:07:15
c.weightx = 1.0;
c.weighty = 1.0;
c.fill = GridBagConstraints.BOTH;

添加查看器之前。

c.weightx = 1.0;
c.weighty = 1.0;
c.fill = GridBagConstraints.BOTH;

before adding the viewer.

浮云落日 2024-10-10 01:07:15

您需要设置要添加到 JScrollpane 的组件的preferredSize()、minimumSize() 和maximumSize()。 将单元格设置为尽可能水平和垂直扩展。

c.weightx = 1;
c.weighty = 1;
c.fill = GridBagConstraints.BOTH;

或者,您可以通过添加到 GridBagConstraints

You need to set the preferredSize(), minimumSize() and maximumSize() of the component you are adding to the JScrollpane. Or you can set the cell to expand horizontally and vertically as far as possible by adding

c.weightx = 1;
c.weighty = 1;
c.fill = GridBagConstraints.BOTH;

to the GridBagConstraints.

慢慢从新开始 2024-10-10 01:07:15

尝试将 prefferedsize(setPrefferedSize()) 设置为要添加到 ScrollPane 的组件。

Try setting prefferedsize(setPrefferedSize()) to the component which you are adding to ScrollPane.

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