0

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

4

2 回答 2

5

您的代码有几处问题。

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

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

  3. 将您的应用程序分解成越来越小的部分,直到您可以不加思索地对每个部分进行编码。想法是分解您的应用程序。

  4. 必须使用 Swing 布局。我选择了BorderLayout,但另一种布局可能更适合您的需求。学习Oracle 的布局管理器可视化指南,直到您能够在睡梦中描述 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());
    }

}
于 2013-01-28T17:13:27.613 回答
2
// assuming Strings are being used:    
sampleList.setPrototypeCellValue("1234567890");  // or whatever

请参阅:JList.setPrototypeCellValue(Object)

于 2013-01-28T20:40:51.090 回答