Java Swing JList,不能设置最小宽度

时间:2013-01-28 15:36:43

标签: java swing awt layout-manager jlist

尝试简单地创建具有指定可见行数但具有最小宽度的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指定高度?感谢。

2 个答案:

答案 0 :(得分:5)

您的代码存在一些问题。

  1. 始终使用SwingUtilities调用Swing应用程序。这可确保在事件调度线程(EDT)上创建和执行Swing组件。

  2. 将数据创建与GUI创建分开。这使您可以一次关注代码的一部分。另外,将代码放入类方法中,而不是尝试在main方法中编写所有内容。

  3. 将您的应用程序分解成越来越小的部分,直到您可以不经过深思熟虑地对每个部分进行编码。我们的想法是分解您的申请。

  4. 必须使用Swing布局。我选择了BorderLayout,但另一种布局可能更适合您想要的效果。研究Oracle's Visual Guide to Layout Managers,直到您可以在睡眠中描述Swing布局管理器。

  5. 最后,当JList将其封闭在JScrollPane时,import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.DefaultListModel; import javax.swing.JFrame; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.SwingUtilities; public class UserInterface implements Runnable { final static private int HEIGHT = 400; final static private int WIDTH = 650; private String[] entries = { "Entry 1", "Entry 2", "Entry 3", "Entry 4", "Entry 5", "Entry 6" }; private DefaultListModel sampleModel; public UserInterface() { this.sampleModel = new DefaultListModel(); } @Override public void run() { createPartControl(); } protected void createPartControl() { JPanel content = new JPanel(); content.setLayout(new BorderLayout()); 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)); JScrollPane scrollPane = new JScrollPane(sampleList); content.add(scrollPane, BorderLayout.CENTER); // 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); } }); } public static void main(String[] args) { SwingUtilities.invokeLater(new UserInterface()); } } 的效果会更好。

  6. {{1}}

答案 1 :(得分:2)

// assuming Strings are being used:    
sampleList.setPrototypeCellValue("1234567890");  // or whatever

请参阅:JList.setPrototypeCellValue(Object)