尝试简单地创建一个具有指定可见行数但最小宽度的 JList。似乎“setMinimumSize()”什么也没做……
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class UserInterface
{
final static private int HEIGHT = 400;
final static private int WIDTH = 650;
public static void main(String[] args) {
JPanel content = new JPanel();
String[] entries = { "Entry 1", "Entry 2", "Entry 3",
"Entry 4", "Entry 5", "Entry 6" };
DefaultListModel sampleModel = new DefaultListModel();
for(int i=0; i<entries.length; i++)
sampleModel.addElement(entries[i]);
JList sampleList = new JList(sampleModel);
sampleList.setMinimumSize(new Dimension(1000,1000));
sampleList.setMaximumSize(new Dimension(1000,1000));
content.add(sampleList);
//main window frame
JFrame window = new JFrame("NAD Assignment 1");
window.setSize(WIDTH, HEIGHT);
window.setContentPane(content);
window.setLocationRelativeTo(null);
window.setVisible(true);
window.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}
setPreferredSize() 似乎有效,但它覆盖了我在 setVisibleRowCount() 中设置的任何内容:
public static void main(String[] args) {
JPanel content = new JPanel();
String[] entries = { "Entry 1", "Entry 2", "Entry 3",
"Entry 4", "Entry 5", "Entry 6" };
DefaultListModel sampleModel = new DefaultListModel();
for(int i=0; i<entries.length; i++)
sampleModel.addElement(entries[i]);
JList sampleList = new JList(sampleModel);
sampleList.setPreferredSize(new Dimension(200,10));
sampleList.setVisibleRowCount(8);
content.add(sampleList);
//main window frame
JFrame window = new JFrame("NAD Assignment 1");
window.setSize(WIDTH, HEIGHT);
window.setContentPane(content);
window.setLocationRelativeTo(null);
window.setVisible(true);
window.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
如何在我的 JList 上设置最小宽度并使用 setVisibleRowCount 指定高度?谢谢。