我正在尝试将一个JScrollPane添加到JList中,但是当我这样做时它会弄乱大小,如图所示:
如果列表是空的,它会像右边那样大,但是如果我添加一些东西,它会缩小到右边的那个,设置最大尺寸或首选尺寸对它
这很烦人,我希望它总是看起来像左边的那个,不管它是否为空。这是我正在使用的代码,但对项目的复杂性做了很难提供一个工作示例。
private JPanel setUpRight(){
//right.setLayout(new BoxLayout(right,BoxLayout.PAGE_AXIS));
right.setBorder(BorderFactory.createEmptyBorder(
2, //top
10, //left
2, //bottom
10)); //right
JPanel localCenter = new JPanel(new BorderLayout());
JPanel localBottom = new JPanel(new BorderLayout());
saves.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
saves.setLayoutOrientation(JList.VERTICAL);
saves.setDragEnabled(true);
saves.setTransferHandler(new FromTransferHandler());
JScrollPane scroll = new JScrollPane(saves,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
configs.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
configs.setLayoutOrientation(JList.VERTICAL);
configs.setDragEnabled(true);
configs.setTransferHandler(new FromTransferHandler1());
JScrollPane scroll1 = new JScrollPane(configs,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
localCenter.add(new JLabel("List of saved macros"),BorderLayout.NORTH);
localCenter.add(scroll,BorderLayout.CENTER);
localCenter.add(new JButton("Delete"),BorderLayout.SOUTH);
localBottom.add(new JLabel("List of saved configs"),BorderLayout.NORTH);
localBottom.add(scroll1,BorderLayout.CENTER);
right.add(localCenter);
right.add(Box.createRigidArea(new Dimension(left.getHeight(), 10)));
JSeparator separate = new JSeparator(SwingConstants.HORIZONTAL);
Dimension d = separate.getPreferredSize();
d.width = separate.getMaximumSize().width;
separate.setMaximumSize( d );
right.add(separate,BoxLayout.LINE_AXIS);
right.add(Box.createRigidArea(new Dimension(left.getHeight(), 10)));
right.add(localBottom);
return right;
}
答案 0 :(得分:2)
您似乎没有调用JList对象上最重要的方法,因为它们正确地调整了JScrollPane的视口大小:setVisibleRowCount(int rowCount)
和setPrototypeCellValue(T value)
。一个帮助JList告诉JScrollPane它的垂直视口大小,第二个方法帮助JList告诉它持有JScrollPane首选的水平视口大小。
请查看JList API了解详情。
如,
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
public class JListExample extends JPanel {
public JListExample() {
String[] listData = { "One", "Two", "Three", "Four", "Five", "One", "Two", "Three",
"Four", "Five", "One", "Two", "Three", "Four", "Five", "One", "Two", "Three",
"Four", "Five", "One", "Two", "Three", "Four", "Five", "One", "Two", "Three",
"Four", "Five", "One", "Two", "Three", "Four", "Five", "One", "Two", "Three",
"Four", "Five", "One", "Two", "Three", "Four", "Five", "One", "Two", "Three",
"Four", "Five", "One", "Two", "Three", "Four", "Five", "One", "Two", "Three",
"Four", "Five", "One", "Two", "Three", "Four", "Five", "One", "Two", "Three",
"Four", "Five" };
JList<String> myList = new JList<>(listData);
myList.setVisibleRowCount(10);
myList.setPrototypeCellValue("abcdefghijklm");
JScrollPane scrollPane = new JScrollPane(myList);
add(scrollPane);
}
private static void createAndShowGui() {
JListExample mainPanel = new JListExample();
JFrame frame = new JFrame("E.G.");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}