JScrollPane 弄乱大小
JScrollPane messing with size
我正在尝试将 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;
}
您似乎没有在 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());
}
}
我正在尝试将 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;
}
您似乎没有在 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());
}
}