Java:JScrollPane 不适用于 GridBagLayout
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4226755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: JScrollPane doesn't work with GridBagLayout
提问by jonescb
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.
在我的 Java 应用程序中,我正在编写一个用于查看 PDF 文件的组件。我有一个非常巧妙的实现,用户可以在其中单击 PDF 并拖动它以查看屏幕不适合的区域。但是我的老板不喜欢它,所以现在我不得不使用滚动条。所以我做了显而易见的事情,只是把它放到了 JScrollPane 中,但几乎不管我做什么,它都拒绝工作。
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.
PDF 刚刚转换为 BufferedImage,然后我将其转换为 ImageIcon,这样我就可以将它添加到 JLabel 中,然后将其添加到 JScrollPane 中。
I have a PDFViewer class which subclasses JScrollPane, and the important code is here:
我有一个 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);
}
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.
现在我有一个单独的类,它是 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);
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.
当我只是简单地将 JScrollPane 添加到布局而不是直接添加到 JFrame 时,为什么它会像这样被弄乱?我发现它适用于 GridLayout,但 GridLayout 不是我想要的。
回答by willcodejavaforfood
You need at least ONE component with the weightx/y set to a non zero value for GridBagLayout to work.
您至少需要一个将 weightx/y 设置为非零值的组件才能使 GridBagLayout 工作。
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.
这意味着它将占用其他组件未使用的所有可用空间。我建议阅读 GridBagLayout 以获取更多信息。
回答by khachik
c.weightx = 1.0;
c.weighty = 1.0;
c.fill = GridBagConstraints.BOTH;
before adding the viewer
.
在添加之前viewer
。
回答by Kintaro
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
您需要设置要添加到 JScrollpane 的组件的 preferredSize()、minimumSize() 和 maximumSize()。或者您可以通过添加将单元格设置为尽可能水平和垂直扩展
c.weightx = 1;
c.weighty = 1;
c.fill = GridBagConstraints.BOTH;
to the GridBagConstraints.
到 GridBagConstraints。
回答by Adamski
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
.
这应该确保您的面板在调整大小时在两个方向上调整大小。顺便说一句,如果这是唯一的组件,则考虑使用BorderLayout
该组件并将其添加到BorderLayout.CENTER
.
回答by Serhiy
Try setting prefferedsize(setPrefferedSize()) to the component which you are adding to ScrollPane.
尝试将 prefferedsize(setPrefferedSize()) 设置为要添加到 ScrollPane 的组件。