![](/img/trans.png)
[英]How to increase the vertical size of a JList parallel with the addition of the elements
[英]How to make a Blank JList at a specified size? Without empty elements
我正在尝试为用户创建一个BLANK JList,以查看添加的befor元素。 总而言之,我正在使用JFileChooser选择要添加到列表中的txt文档,然后计算每个txt文档中的元音并显示在另一个列表中。 在用户选择任何内容之前,该列表必须是可见的,并且列表必须为空。 是的,有两个空JList,但很明显,如果我可以得到一个另一个很容易的帮助。 到目前为止,互联网还没有触及这一特定主题。
设置行计数(可见)不起作用,也不设置setsize()。 除非我错了。 请举例说明。 提前致谢!
码:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;
public class VowelCounterApp extends JFrame implements ActionListener
{
/**
*
*/
private static final long serialVersionUID = 1L;
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JScrollPane scrollPane = new JScrollPane();
JList selectList = new JList();
JList showList = new JList();
JFileChooser fileChooser = new JFileChooser();
JButton addFiles = new JButton("Add Files");
JButton process = new JButton("Process");
JButton clear = new JButton("Clear");
JButton help = new JButton("Help");
public VowelCounterApp()
{
JFrame appWindow = new JFrame("Vowel Counter");
appWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
appWindow.setSize(1500, 600);
appWindow.setLayout(new BorderLayout());
appWindow.setVisible(true);
appWindow.add(panel1, BorderLayout.WEST);
appWindow.add(panel2, BorderLayout.EAST);
panel1.add(selectList);
selectList.add(scrollPane);
panel2.add(showList);
showList.add(scrollPane);
}
public void actionPerformed(ActionEvent e)
{
}
public static void main(String[] args)
{
new VowelCounterApp();
}
}
JList API将向您展示两个关键方法:
public void setVisibleRowCount(int rowCount)
public void setPrototypeCellValue(E prototype)
因此,只需为列表提供足够的行可见计数和足够长的原型值,然后其封闭的JScrollPane的视口将相应地自动调整其视口大小。
例如:
import java.awt.GridLayout;
import javax.swing.*;
public class JListExample extends JPanel {
private JList<String> list1 = new JList<>();
private JList<String> list2 = new JList<>();
public JListExample() {
list1.setVisibleRowCount(20);
list2.setVisibleRowCount(20);
list1.setPrototypeCellValue(String.format("%60s", ""));
list2.setPrototypeCellValue(String.format("%60s", ""));
setLayout(new GridLayout(1, 2));
add(new JScrollPane(list1));
add(new JScrollPane(list2));
}
private static void createAndShowGui() {
JListExample mainPanel = new JListExample();
JFrame frame = new JFrame("JList Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
或者更好地避免“魔术”数字:
public class JListExample extends JPanel {
private static final int LIST_ROW_COUNT = 20;
private static final int LIST_CHAR_WIDTH = 80;
private static final String LIST_PROTOTYPE = "%" + LIST_CHAR_WIDTH + "s";
private JList<String> list1 = new JList<>();
private JList<String> list2 = new JList<>();
public JListExample() {
list1.setVisibleRowCount(LIST_ROW_COUNT);
list2.setVisibleRowCount(LIST_ROW_COUNT);
list1.setPrototypeCellValue(String.format(LIST_PROTOTYPE, ""));
list2.setPrototypeCellValue(String.format(LIST_PROTOTYPE, ""));
在这里,现在您可以通过将它们和JScrollPane包装在使用TitledBorder的JPanel中来更好地查看列表:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.*;
public class JListExample extends JPanel {
private static final int LIST_ROW_COUNT = 20;
private static final int LIST_CHAR_WIDTH = 80;
private static final String LIST_PROTOTYPE = "%" + LIST_CHAR_WIDTH + "s";
private JList<String> list1 = new JList<>();
private JList<String> list2 = new JList<>();
public JListExample() {
list1.setVisibleRowCount(LIST_ROW_COUNT);
list2.setVisibleRowCount(LIST_ROW_COUNT);
list1.setPrototypeCellValue(String.format(LIST_PROTOTYPE, ""));
list2.setPrototypeCellValue(String.format(LIST_PROTOTYPE, ""));
setLayout(new GridLayout(1, 2));
add(createListWrapper(list1, "JList 1"));
add(createListWrapper(list2, "JList 2"));
}
private JComponent createListWrapper(JList<String> list, String title) {
JScrollPane scrollPane = new JScrollPane(list);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JPanel wrapperPanel = new JPanel(new BorderLayout());
wrapperPanel.add(scrollPane);
wrapperPanel.setBorder(BorderFactory.createTitledBorder(title));
return wrapperPanel;
}
private static void createAndShowGui() {
JListExample mainPanel = new JListExample();
JFrame frame = new JFrame("JList Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
编辑
正如MadProgrammer在评论中所述:
我还要小心使用prototypeCellValue,除非该值与数据的预期长度相匹配,否则它可能会在显示时截断您的数据,只需要小心
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.