Java 设置包含JScrollPane的容器的大小,使滚动条不可见
如何设置包含Java 设置包含JScrollPane的容器的大小,使滚动条不可见,java,swing,jscrollpane,miglayout,preferredsize,Java,Swing,Jscrollpane,Miglayout,Preferredsize,如何设置包含JScrollPane的容器的大小,使滚动条不会出现 考虑此SSCCE(使用): 目标是设置视口内容的宽度,以便不需要水平滚动条 应该是: 编辑:按照camikr的建议,结果如下: public static final int pref_height = 500; public static void main(String[] args) { JPanel panel = new JPanel(new MigLayout()); for(int i = 0
JScrollPane
的容器的大小,使滚动条不会出现
考虑此SSCCE(使用):
目标是设置视口内容的宽度,以便不需要水平滚动条
应该是:
编辑:按照camikr的建议,结果如下:
public static final int pref_height = 500;
public static void main(String[] args) {
JPanel panel = new JPanel(new MigLayout());
for(int i = 0; i < 15; i++) {
JTextArea textArea = new JTextArea();
textArea.setColumns(20);
textArea.setRows(5);
textArea.setWrapStyleWord(true);
textArea.setLineWrap(true);
JScrollPane jsp = new JScrollPane(textArea);
panel.add(new JLabel("Notes" + i));
panel.add(jsp, "span, grow");
}
JScrollPane jsp = new JScrollPane(panel) {
@Override
public Dimension getPreferredSize() {
setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
Dimension dim = new Dimension(super.getPreferredSize().width + getVerticalScrollBar().getSize().width, pref_height);
setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
return dim;
}
};
JFrame frame = new JFrame();
frame.add(jsp);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static final int pref_height=500;
公共静态void main(字符串[]args){
JPanel panel=newjpanel(newmiglayout());
对于(int i=0;i<15;i++){
JTextArea textArea=新的JTextArea();
textArea.setColumns(20);
textArea.setRows(5);
textArea.setWrapStyleWord(true);
textArea.setLineWrap(true);
JScrollPane jsp=新的JScrollPane(textArea);
增补(新的JLabel(“注释”+i));
add(jsp,“span,grow”);
}
JScrollPane jsp=新的JScrollPane(面板){
@凌驾
公共维度getPreferredSize(){
setVerticalScrollBarPolicy(JScrollPane.VERTICAL\u SCROLLBAR\u ALWAYS);
尺寸标注=新尺寸标注(super.getPreferredSize().width+getVerticalScrollBar().getSize().width,pref_height);
setVerticalScrollBarPolicy(JScrollPane.VERTICAL\u SCROLLBAR\u根据需要);
返回暗淡;
}
};
JFrame=新JFrame();
frame.add(jsp);
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
对我来说似乎有点刻薄,但它是有效的 如你所见,我正试图找出这条线上应该放什么:
frame.setSize(jsp.getViewport().getViewSize().width, 500);
不要放任何东西。您不应该试图管理框架的大小。例如,您的代码甚至不考虑框架的边框。如果有任何更改,您的代码将更改为使用帧的宽度,而不是滚动窗格
更好的解决方案是重写滚动窗格的getPreferredSize()
方法,返回super.getPreferredSize()
的宽度,然后指定合理的高度。您需要确保垂直滚动条始终可见,以便计算正常进行
然后,pack()方法将按预期工作。同样适用于水平情况:
new JScrollPane(panel) {
public Dimension getPreferredSize() {
Component view = getViewport().getView();
if (view == null) return super.getPreferredSize();
int pref_width = view.getPreferredSize().width;
setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
Dimension dim = new Dimension(pref_width, super.getPreferredSize().height + getHorizontalScrollBar().getSize().height);
setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
return dim;
}
}
此外,如果您稍后关闭滚动窗格内的视图,它也会进行适当的调整。对我来说似乎有点粗糙,但它可以工作。在对问题的编辑中发布了最终解决方案…@ryvantage JComboBox和JScrollPane无法返回合理的维度,
new JScrollPane(panel) {
public Dimension getPreferredSize() {
Component view = getViewport().getView();
if (view == null) return super.getPreferredSize();
int pref_width = view.getPreferredSize().width;
setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
Dimension dim = new Dimension(pref_width, super.getPreferredSize().height + getHorizontalScrollBar().getSize().height);
setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
return dim;
}
}