Java JScrollPane的大小随JList内容的不同而变化
在我的Java JScrollPane的大小随JList内容的不同而变化,java,swing,layout-manager,jlist,gridbaglayout,Java,Swing,Layout Manager,Jlist,Gridbaglayout,在我的JPanel中,我有两个JScrollPane代表JList 问题是,当启动程序时,两个列表都为空,它们的大小相同。但是当其中一个一开始就有一些值时,它的大小就会改变 如何避免呢 我使用的是GridBagLayout,但无论设置哪个约束,它对我的滚动窗格都没有影响 这就是使用空JList时的情况 以及JList中包含一些值的示例 gb.gridx = 1; gb.gridy = 0; gb.fill = GridBagConstraints.NONE; add(scroll, gb);
JPanel
中,我有两个JScrollPane
代表JList
问题是,当启动程序时,两个列表都为空,它们的大小相同。但是当其中一个一开始就有一些值时,它的大小就会改变
如何避免呢
我使用的是GridBagLayout
,但无论设置哪个约束,它对我的滚动窗格都没有影响
这就是使用空JList时的情况
以及JList中包含一些值的示例
gb.gridx = 1;
gb.gridy = 0;
gb.fill = GridBagConstraints.NONE;
add(scroll, gb);
gb.gridx ++;
gb.anchor = GridBagConstraints.CENTER;
gb.fill = GridBagConstraints.NONE;
add(scroll2, gb);
有人知道如何正确设置吗
但是当其中一个一开始就有一些值时,它的大小就会改变。如何避免呢?我
创建JList时,您可以使用:
list.setVisibleRowCount(...);
这将允许JList计算一致的首选大小,即使列表中的数据发生变化
但是当其中一个一开始就有一些值时,它的大小就会改变。如何避免呢?我
创建JList时,您可以使用:
list.setVisibleRowCount(...);
这将允许JList计算一致的首选大小,即使列表中的数据发生变化。使用
scroll.setHorizontalScrollBarPolicy(JScrollPane.HorizontalScrollBar\uAlways);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL\u SCROLLBAR\u ALWAYS)
scroll2.setHorizontalScrollBarPolicy(JScrollPane.HorizontalScrollBar\uAlways);
scroll2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL\u SCROLLBAR\u ALWAYS)
我希望这能对您有所帮助。使用
scroll.setHorizontalScrollBarPolicy(JScrollPane.HorizontalScrollBar\uAlways);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL\u SCROLLBAR\u ALWAYS)
scroll2.setHorizontalScrollBarPolicy(JScrollPane.HorizontalScrollBar\uAlways);
scroll2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL\u SCROLLBAR\u ALWAYS)
我希望这能对你有所帮助
JList#设置VisibleRowCount(…)
在使用JDK1.8.0141的Windows 10上运行良好
宽度
也许这个方法会奏效
import java.awt.*;
import java.util.*;
import javax.swing.*;
public class ListScrollPaneTest {
public JComponent makeUI() {
int visibleRowCount = 8;
String prototypeCellValue = "mmmmmmmmmmmmmmmmmmmm";
String[] model1 = Collections.nCopies(20, "aaa").toArray(new String[0]);
JList<String> list1 = makeJList(visibleRowCount, prototypeCellValue, model1);
JScrollPane scroll1 = makeTitledScrollPane("Included Groups", list1);
String[] model2 = {"loooooooooooooooooooooooooooooooooooooooooooooong"};
JList<String> list2 = makeJList(visibleRowCount, prototypeCellValue, model2);
JScrollPane scroll2 = makeTitledScrollPane("Excluded Groups", list2);
JPanel p = new JPanel(new GridBagLayout());
p.setOpaque(true);
p.setBackground(Color.GRAY);
GridBagConstraints gb = new GridBagConstraints();
gb.insets = new Insets(15, 15, 15, 15);
gb.gridx = 1;
gb.gridy = 0;
gb.fill = GridBagConstraints.NONE;
p.add(scroll1, gb);
gb.gridx ++;
gb.anchor = GridBagConstraints.CENTER;
gb.fill = GridBagConstraints.NONE;
p.add(scroll2, gb);
return p;
}
private static <E> JList<E> makeJList(int rowCount, E prototypeCellValue, E[] m) {
JList<E> list = new JList<>(m);
list.setVisibleRowCount(rowCount);
list.setPrototypeCellValue(prototypeCellValue);
return list;
}
private static JScrollPane makeTitledScrollPane(String title, JComponent c) {
JScrollPane scroll = new JScrollPane(c);
scroll.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEmptyBorder(), title));
return scroll;
}
public static void main(String... args) {
EventQueue.invokeLater(() -> {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new ListScrollPaneTest().makeUI());
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
});
}
}
import java.awt.*;
导入java.util.*;
导入javax.swing.*;
公共类ListScrollPaneTest{
公共JComponent makeUI(){
int visibleRowCount=8;
字符串prototypeCellValue=“mmmmmmmm”;
String[]model1=Collections.nCopies(20,“aaa”).toArray(新字符串[0]);
JList list1=makeJList(visibleRowCount,prototypeCellValue,model1);
JScrollPane scroll1=makeTitledScrollPane(“包括的组”,列表1);
字符串[]model2={“oooooooooooooooooooooong”};
JList list2=makeJList(visibleRowCount,prototypeCellValue,model2);
JScrollPane scroll2=makeTitledScrollPane(“排除的组”,列表2);
JPanel p=newjpanel(newgridbaglayout());
p、 set不透明(true);
p、 挫折背景(颜色:灰色);
GridBagConstraints gb=新的GridBagConstraints();
gb.插图=新插图(15,15,15,15);
gb.gridx=1;
gb.gridy=0;
gb.fill=GridBagConstraints.NONE;
p、 添加(1,gb);
gb.gridx++;
gb.anchor=gridbag.CENTER;
gb.fill=GridBagConstraints.NONE;
p、 添加(2,gb);
返回p;
}
私有静态JList makeJList(int rowCount,E prototypeCellValue,E[]m){
JList列表=新JList(m);
list.setVisibleRowCount(rowCount);
list.setPrototypeCellValue(prototypeCellValue);
退货清单;
}
私有静态JScrollPane MakeTiledScrollPane(字符串标题,JComponentC){
JScrollPane scroll=新的JScrollPane(c);
scroll.setboorder(BorderFactory.createTitledBorder(
createEmptyByOrder(),title));
返回滚动条;
}
公共静态void main(字符串…参数){
EventQueue.invokeLater(()->{
JFrame f=新的JFrame();
f、 setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f、 getContentPane().add(新的ListScrollPaneTest().makeUI());
f、 包装();
f、 setLocationRelativeTo(空);
f、 setVisible(真);
});
}
}
JList#设置VisibleRowCount(…)
在使用JDK1.8.0141的Windows 10上运行良好
宽度
也许这个方法会奏效
import java.awt.*;
import java.util.*;
import javax.swing.*;
public class ListScrollPaneTest {
public JComponent makeUI() {
int visibleRowCount = 8;
String prototypeCellValue = "mmmmmmmmmmmmmmmmmmmm";
String[] model1 = Collections.nCopies(20, "aaa").toArray(new String[0]);
JList<String> list1 = makeJList(visibleRowCount, prototypeCellValue, model1);
JScrollPane scroll1 = makeTitledScrollPane("Included Groups", list1);
String[] model2 = {"loooooooooooooooooooooooooooooooooooooooooooooong"};
JList<String> list2 = makeJList(visibleRowCount, prototypeCellValue, model2);
JScrollPane scroll2 = makeTitledScrollPane("Excluded Groups", list2);
JPanel p = new JPanel(new GridBagLayout());
p.setOpaque(true);
p.setBackground(Color.GRAY);
GridBagConstraints gb = new GridBagConstraints();
gb.insets = new Insets(15, 15, 15, 15);
gb.gridx = 1;
gb.gridy = 0;
gb.fill = GridBagConstraints.NONE;
p.add(scroll1, gb);
gb.gridx ++;
gb.anchor = GridBagConstraints.CENTER;
gb.fill = GridBagConstraints.NONE;
p.add(scroll2, gb);
return p;
}
private static <E> JList<E> makeJList(int rowCount, E prototypeCellValue, E[] m) {
JList<E> list = new JList<>(m);
list.setVisibleRowCount(rowCount);
list.setPrototypeCellValue(prototypeCellValue);
return list;
}
private static JScrollPane makeTitledScrollPane(String title, JComponent c) {
JScrollPane scroll = new JScrollPane(c);
scroll.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEmptyBorder(), title));
return scroll;
}
public static void main(String... args) {
EventQueue.invokeLater(() -> {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new ListScrollPaneTest().makeUI());
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
});
}
}
import java.awt.*;
导入java.util.*;
导入javax.swing.*;
公共类ListScrollPaneTest{
公共JComponent makeUI(){
int visibleRowCount=8;
字符串prototypeCellValue=“mmmmmmmm”;
String[]model1=Collections.nCopies(20,“aaa”).toArray(新字符串[0]);
JList list1=makeJList(visibleRowCount,prototypeCellValue,model1);
JScrollPane scroll1=makeTitledScrollPane(“包括的组”,列表1);
字符串[]model2={“oooooooooooooooooooooong”};
JList list2=makeJList(visibleRowCount,prototypeCellValue,model2);
JScrollPane scroll2=makeTitledScrollPane(“排除的组”,列表2);
JPanel p=newjpanel(newgridbaglayout());
p、 set不透明(true);
p、 挫折背景(颜色:灰色);
GridBagConstraints gb=新的GridBagConstraints();
gb.插图=新插图(15,15,15,15);
gb.gridx=1;
gb.gridy=0;
gb.fill=GridBagConstraints.NONE;
p、 添加(1,gb);
gb.gridx++;
gb.anchor=gridbag.CENTER;
gb.fill=GridBagConstraints.NONE;
p、 添加(2,gb);
返回p;
}
私有静态JList makeJList(int rowCount,E prototypeCellValue,E[]m){
JList列表=新JList(m);
list.setVisibleRowCount(rowCount);
list.setPrototypeCellValue(prototypeCellValue);
退货清单;
}
私有静态JScrollPane MakeTiledScrollPane(字符串标题,JComponentC){
JScrollPane scroll=新的JScrollPane(c);
scroll.setboorder(BorderFactory.createTitledBorder(
createEmptyByOrder(),title));
返回滚动条;
}
公共静态void main(字符串…参数){
EventQueue.invokeLater(()->{
JFrame f=新的JFrame();
f、 setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f、 getContentPane().add(新的ListScrollPaneTest().makeUI());
f、 包装();
f、