java - 使用 GridLayout 制作 JList

标签 java swing layout-manager jlist grid-layout

我需要一个 JList 来包含 JPanel,但是这是不可能的,所以我通过使用 GridLyayout 制作一个副本>。它工作得很好,但是,我希望为所有网格设置一个“绝对”尺寸,或者至少有它,这样屏幕上只有 10 个可见,其余的在下面,因为容器是一个滚动 Pane 。澄清一下,网格布局有 1 行和 0(或 x)列。

有人有什么想法吗?

最佳答案

I needed a JList to contain a JPanel, however, this is not possible

当然有可能。让它们看起来漂亮的秘诀在于渲染。

enter image description here

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);
    }
}

关于java - 使用 GridLayout 制作 JList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22875101/

相关文章:

Java计算无效结果

java - 约束必须是字符串(或 null)

java - JProgressBar 不可见,但进度正在正确更新

java - JApplet多页面应用

java - FlowLayout 对齐方式更改在 Swing 中不直接可见

java - 为什么我的代码在运行此代码时打印 [I@87816d?

java - Kerberos:校验和失败问题

java - 如何与 Maven 一起运行 xml 配置驱动和无 xml Spring Web 应用程序?

java - jtextarea 不会用 GridBagConstraints 填充面板

java - 在具有固定大小的 JPanel 中显示未知数量的 JButton