java JList大小

时间:2011-05-09 09:00:09

标签: java swing jlist

嗨我想将JList高度设置为屏幕高度为

public Locations() {
    Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
    screenWidth = screen.width;
    screenHeight = screen.height;
    initComponents();
}

private void initComponents() {
    setLayout(new GroupLayout());
    add(getJScrollPane0(), new Constraints(new Leading(8, 100, 10, 10), new Leading(5, 135, 10, 10)));
    setSize(1366, 768);
}

private JScrollPane getJScrollPane0() {
    if (jScrollPane0 == null) {
        jScrollPane0 = new JScrollPane();
        jScrollPane0.setSize(screenWidth, screenHeight);
        jScrollPane0.setViewportView(getJList0());
    }
    return jScrollPane0;
}

private JList getJList0() {
    if (jList0 == null) {
        jList0 = new JList();
        jList0.setSize(400, screenHeight);
        DefaultListModel listModel = new DefaultListModel();
        listModel.addElement("item0");
        listModel.addElement("item1item1item1item1item1");
        listModel.addElement("item2");
        listModel.addElement("item3");
        jList0.setModel(listModel);
    }
    return jList0;
}

private static void installLnF() {
    try {
        String lnfClassname = PREFERRED_LOOK_AND_FEEL;
        if (lnfClassname == null)
            lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
        UIManager.setLookAndFeel(lnfClassname);
    } catch (Exception e) {
        System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL
                + " on this platform:" + e.getMessage());
    }
}

/**
 * Main entry of the class.
 * Note: This class is only created so that you can easily preview the result at runtime.
 * It is not expected to be managed by the designer.
 * You can modify it as you like.
 */
public static void main(String[] args) {
    installLnF();
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setTitle("Locations");
            Locations content = new Locations();
            content.setPreferredSize(content.getSize());
            frame.add(content, BorderLayout.CENTER);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    });
}

但是列表大小就像我在编辑器中绘制它,如何更改它?

编辑:

我已经通过添加

解决了这个问题
add(getJScrollPane0(), new Constraints(new Leading(8, 100, 10, 10), new Leading(5, screenHeight - 115, 10, 10)));

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:1)

每个List都与JScrollPane相关联。 首先设置与list关联的JScrollPane的大小,然后使用list.setsize(width,height)方法设置list的大小

在我的计划中:

jScrollPane1.setSize(100,500);
list.setSize(100,500);