我已经实现了一个对话框,在其中我在 JPanel 中添加了许多复选框,该 JPanel 也有一个滚动条。每次运行程序时复选框的数量并不固定,但在创建对话框之前我就知道了。
不幸的是,当我有大量复选框来添加对话框时,它看起来像这样:
我的代码是:
JPanel listPanel = new JPanel(new GridLayout(files.size() + 2, 1));
for (int i = 0; i < files.size(); i++) {
listPanel.add(files.get(i));
}
listPanel.setPreferredSize(new Dimension(600, 1400));
JScrollPane sp =
new JScrollPane(listPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
sp.setPreferredSize(new Dimension(300, 200));
mainPanel.add(sp, BorderLayout.CENTER);
mainPanel.add(buttonPane, BorderLayout.EAST);
getContentPane().setLayout(new FlowLayout());
getContentPane().add(mainPanel);
pack();
setResizable(false);
如何在不影响框架的情况下将滚动 Pane 内的面板尺寸设置为更大的尺寸?
最佳答案
避免使用setPreferredSize
,让布局管理器决定他们实际需要的尺寸...
已更新示例
滚动 Pane 的 JViewport
使用 listPane
的首选大小来确定它需要多少可视空间,这是很多......
为了克服这个问题,您需要向 View 端口提供更好的提示,以了解码件实际想要的可视大小。组件的 preferredSize
在这里不起作用,您需要从 Scrollable
接口(interface)提供 preferredScrollableViewportSize
public class BadList {
public static void main(String[] args) {
new BadList();
}
public BadList() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
List<String> files = new ArrayList<>(2000);
for (int index = 0; index < 2000; index++) {
files.add(Integer.toString(index));
}
ListPane listPanel = new ListPane();
listPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = GridBagConstraints.REMAINDER;
for (String file : files) {
listPanel.add(new JCheckBox(file), gbc);
}
JScrollPane sp = new JScrollPane(listPanel);
JPanel mainPanel = new JPanel(new BorderLayout());
JPanel buttonPane = new JPanel(new GridBagLayout());
buttonPane.add(new JButton("Select All"), gbc);
buttonPane.add(new JButton("Deselect All"), gbc);
gbc.weighty = 1;
buttonPane.add(new JPanel(), gbc);
mainPanel.add(sp, BorderLayout.CENTER);
mainPanel.add(buttonPane, BorderLayout.EAST);
setLayout(new BorderLayout());
add(mainPanel);
}
}
public class ListPane extends JPanel implements Scrollable {
@Override
public Dimension getPreferredScrollableViewportSize() {
return new Dimension(200, 200);
}
@Override
public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
return 128;
}
@Override
public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
return 128;
}
@Override
public boolean getScrollableTracksViewportWidth() {
return false;
}
@Override
public boolean getScrollableTracksViewportHeight() {
return false;
}
}
}
nb-我在这个例子中使用了一些任意的魔数(Magic Number),你将如何为你的需要提供更现实的值
关于java - JPanel 的尺寸比 Scrollpane 的尺寸大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14682972/