简体   繁体   English

使用GridLayout制作JList

[英]Making a JList with GridLayout

I needed a JList to contain a JPanel , however, this is not possible, so I am making a replica via the use of GridLyayout . 我需要一个JList来包含一个JPanel ,但是,这是不可能的,所以我通过使用GridLyayout制作了一个副本。 It works fine, however, I would want an 'absolute' dimension set to all the grids, or at least have it so only 10 are visible on the screen and the rest are below, since the container is a scroll pane. 它可以正常工作,但是,我希望为所有网格设置一个“绝对”尺寸,或者至少要设置为“绝对”尺寸,这样在屏幕上只能看到10个,其​​余的在下面,因为该容器是一个滚动窗格。 Just to clarify, the grid layout has 1 row, and 0 (or x) columns. 为了澄清,网格布局具有1行和0(或x)列。

Anyone have any idea? 有人知道吗

I needed a JList to contain a JPanel , however, this is not possible 我需要一个JList来包含一个JPanel ,但是,这是不可能的

Of course it is possible. 当然可以。 The secret to getting them to look nice is in the rendering. 使它们看起来不错的秘密在于渲染。

在此处输入图片说明

import java.awt.*;
import java.util.Vector;
import javax.swing.*;

class PanelsInList {

    JPanel ui = null;
    Color[] colors = {
        Color.RED, Color.GREEN, Color.BLUE,
        Color.MAGENTA, Color.PINK, Color.YELLOW,
        Color.BLACK, Color.GRAY, Color.WHITE 
    };

    PanelsInList() {
        initUi();
    }

    public final void initUi() {
        if (ui != null) {
            return;
        }
        ui = new JPanel(new BorderLayout());

        Vector<JPanel> listContent = new Vector<JPanel>();
        for (int ii=1; ii<colors.length+1; ii++) {
            Color c = colors[(ii-1)%colors.length];
            listContent.add(getPanel(c,ii));
        }
        JList<JPanel> list = new JList<JPanel>(listContent);
        // the secret to getting them to look nice is in the rendering
        list.setCellRenderer(new PanelCellRenderer());
        list.setVisibleRowCount(3);
        ui.add(new JScrollPane(list));
    }

    class PanelCellRenderer extends DefaultListCellRenderer {

        @Override
        public Component getListCellRendererComponent(
                JList list, 
                Object value, 
                int index, 
                boolean isSelected, 
                boolean cellHasFocus) {
            Component c = super.getListCellRendererComponent(
                    list, value, index, isSelected, cellHasFocus);
            JPanel p = null;
            if (value instanceof JPanel && c instanceof JLabel) {
                 p = (JPanel)value;
                 JLabel l = (JLabel)c;
                 p.setBackground(l.getBackground());
                 p.setForeground(l.getForeground());
                 p.setBorder(l.getBorder());
            }
            return p;
        }
    }

    public JPanel getPanel(Color color, int n) {
        JPanel p = new JPanel(new GridLayout());

        JLabel l = new JLabel(
                "Label " + n, new ColoredIcon(color), SwingConstants.LEADING);
        p.add(l);

        return p;
    }

    class ColoredIcon implements Icon {

        int sz = 16;
        Color color;

        ColoredIcon(Color color) {
            this.color = color;
        }

        @Override
        public void paintIcon(Component c, Graphics g, int x, int y) {
            g.setColor(color);
            g.fillRect(0, 0, sz, sz);
        }

        @Override
        public int getIconWidth() {
            return sz;
        }

        @Override
        public int getIconHeight() {
            return sz;
        }

    }

    public JComponent getUi() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                PanelsInList pil = new PanelsInList();

                JOptionPane.showMessageDialog(null, pil.getUi());
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency
        SwingUtilities.invokeLater(r);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM