java 在 JScrollPane 中使用绝对布局
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11995516/
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
Use a absolute layout inside a JScrollPane
提问by Jonás
I need to use a JScrollPane with absolute layoute. I know that the use of setLayout(null) is not recommendable at all. I've been reading that if you want to use the absolute layout with a JScrollPane it is necessary to set the preferred size property of the elements inside in order to JScrollPane can calculate its size.
我需要使用具有绝对布局的 JScrollPane。我知道 setLayout(null) 的使用根本不值得推荐。我一直在读,如果你想在 JScrollPane 中使用绝对布局,则必须设置内部元素的首选大小属性,以便 JScrollPane 可以计算其大小。
I've been trying the next code changing the order and sizes of elemnts but I can't work out where I've been wrong.
我一直在尝试更改元素的顺序和大小的下一个代码,但我无法弄清楚我错在哪里。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class frame extends JFrame {
private JPanel contentPane;
private JScrollPane scrollPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frame frame = new frame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public frame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setLayout(null);
setContentPane(contentPane);
JPanel panel = new JPanel();
panel.setBackground(Color.red);
panel.setBounds(0,0,600,600);
panel.setPreferredSize(new Dimension(420,280));
scrollPane = new JScrollPane(panel);
scrollPane.setBounds(0, 0, 600, 600);
scrollPane.setLayout(null);
scrollPane.setBackground(Color.green);
scrollPane.add(panel);
scrollPane.setPreferredSize(new Dimension(450,300));
contentPane.add(scrollPane);
}
}
Why setPreferredSize is not doing the right work? Thanks in advance.
为什么 setPreferredSize 没有做正确的工作?提前致谢。
采纳答案by Adamski
Changing the layout manager of the actual JScrollPane
does not make sense; if you wish to use absolute layout within your application you would typically call setLayout(null)
on a container class (such as JPanel
) in order to position child components within it using absolute positioning.
更改实际的布局管理器JScrollPane
没有意义;如果您希望在应用程序中使用绝对布局,您通常会调用setLayout(null)
容器类(例如JPanel
),以便使用绝对定位将子组件定位在其中。
I suggest that you should try:
我建议你应该尝试:
- Call
setLayout(null)
on theJPanel
contained within theJScrollPane
(as I thinkthis is what you're trying to do). - Rather than use
JPanel
directly, subclass it and have your subclass implement theScrollable
interface, which provides additional information to theJScrollPane
regarding the optimal scrollable viewport size.
- 呼叫
setLayout(null)
在JPanel
其中所包含的JScrollPane
(因为我认为这是你想要做什么)。 - 与其
JPanel
直接使用,不如将其子类化并让您的子类实现Scrollable
接口,这为JScrollPane
有关最佳可滚动视口大小提供了额外的信息。
回答by MadProgrammer
First of all, don't change the layout manager of the scroll pane, it's not required for what you want to achieve
首先,不要更改滚动窗格的布局管理器,它不是您想要实现的必需的
Second, scroll pane works though a JViewport
. You have to add your container to it instead (scrollPane.add(panel)
is the wrong thing to do), instead use scrollPane.setViewportView(panel)
其次,滚动窗格通过JViewport
. 您必须将容器添加到其中(这scrollPane.add(panel)
是错误的做法),而是使用scrollPane.setViewportView(panel)
Thirdly, you want to take a look at the Scrollableinterface as scroll pane doesn't just rely on the preferred size of its contents
第三,您想看看Scrollable界面,因为滚动窗格不仅仅依赖于其内容的首选大小
回答by Nievinski
I can see your problem.
我可以看到你的问题。
Here you set the bound and the preferred size of your panel.
您可以在此处设置面板的边界和首选大小。
panel.setBounds(0,0,600,600);
panel.setPreferredSize(new Dimension(420,280));
then you set the bound of the scrollPane
然后你设置滚动窗格的边界
scrollPane.setBounds(0, 0, 600, 600);
Well, the bounds of the panel is useless, cause scrollPane cares only about the preferred size, and the preferred size of the panel is smaller then the bounds of the scrollPane, so the scrollPanel thinks he doesn't need to show anything else, that's why it doesn't show any scrollBars or enable them. OBS: I see no sense at setting the layout of the scrollPanel. So you can skip that line.
好吧,面板的边界没用,因为scrollPane只关心首选大小,而面板的首选大小比scrollPane的边界小,所以scrollPanel认为他不需要显示任何其他东西,那就是为什么它不显示任何滚动条或启用它们。OBS:我认为设置 scrollPanel 的布局没有意义。所以你可以跳过那一行。
Just as commentary: Using scroll with absolut layout pane is just fine since you make the care of the preferred size of the pane.
就像评论一样:使用带有绝对布局窗格的滚动就可以了,因为您可以关注窗格的首选大小。