多色列表

时间:2012-08-15 03:58:39

标签: java swing layout colors jlist

enter image description here

我必须连续创建5个Jlists,以使他们的背景看起来有不同的颜色。我现有的代码创建了这5个没有颜色的列表(默认的JList)。我知道我只能自定义jlist的内部/边框而不是它周围的边距/边距?或者我错了,有办法吗?

顺便说一句,列表上方的空格是Jlabel(上图中未显示),正在使用的布局管理器是GroupLayout和GridBagLayout。

更新

AT,根据您的建议,这里是一个比较列表在jpanel包围时的样子。后面的列表是Jpanel包围的最小空边框大小为1的列表。

创建一个覆盖了preferredsize的JPanel的问题是jlists在水平jpanel中,而在它们上面是另一个带有标签的jpanel。因此,将jlist包装在jpanel中并不会这样做。

enter image description here

1 个答案:

答案 0 :(得分:4)

请查看此代码示例。它是否更接近您想要的,否则您定义需要完成的更改。我一收到您的回复,我就会在他们身上: - )

结果如下:

JLIST

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

public class JListExample
{
    private JList<String> list1;
    private JList<String> list2;
    private JList<String> list3;
    private JList<String> list4;
    private JList<String> list5;
    private CustomPanel panel1;
    private CustomPanel panel2;
    private CustomPanel panel3;
    private CustomPanel panel4;
    private CustomPanel panel5;
    private String[] data = {"one", "two", "three", "four"};
    private int width = 110;
    private int height = 300;

    private void displayGUI()
    {
        JFrame frame = new JFrame("JList Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new GridLayout(0, 5, 2, 2));

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.weightx = 1.0;
        gbc.weighty = 0.9;

        panel1 = new CustomPanel(width, height, Color.GRAY, "List 1");
        list1 = new JList<String>(data);
        panel1.add(list1, gbc);
        panel2 = new CustomPanel(width, height, 
                 Color.GREEN.brighter().brighter(), "List 2");
        list2 = new JList<String>(data);         
        panel2.add(list2, gbc);
        panel3 = new CustomPanel(width, height, 
                          Color.ORANGE.brighter(), "List 3");
        list3 = new JList<String>(data);                  
        panel3.add(list3, gbc);
        panel4 = new CustomPanel(width, height, 
                            Color.BLUE.brighter(), "List 4");
        list4 = new JList<String>(data);                    
        panel4.add(list4, gbc);
        panel5 = new CustomPanel(width, height, Color.RED, "List 5");
        list5 = new JList<String>(data);
        panel5.add(list5, gbc);

        contentPane.add(panel1);
        contentPane.add(panel2);
        contentPane.add(panel3);
        contentPane.add(panel4);
        contentPane.add(panel5);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new JListExample().displayGUI();
            }
        });
    }
}

class CustomPanel extends JPanel
{
    private final int GAP = 5;
    private int width;
    private int height;
    private Color backgroundColour;
    private JLabel titleLabel;

    public CustomPanel(int w, int h, Color c, String title)
    {
        width = w;
        height = h;
        backgroundColour = c;
        titleLabel = new JLabel(title, JLabel.CENTER);
        setBackground(backgroundColour);
        setBorder(
            BorderFactory.createEmptyBorder(
                                GAP, GAP, GAP, GAP));                   
        setLayout(new GridBagLayout()); 
        titleLabel.setAlignmentX(JLabel.CENTER_ALIGNMENT);
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.NONE;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.weighty = 0.1;
        add(titleLabel, gbc);
    }   

    @Override
    public Dimension getPreferredSize()
    {
        return (new Dimension(width, height));
    }
}

**最新编辑:**

正如@kleopatra正确指出的那样(对我来说不是新的东西:-)),判断力太好了。完成与以下内容相关的编辑:

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

public class JListExample
{
    private final int GAP = 5;
    private JList<String> list1;
    private JList<String> list2;
    private JList<String> list3;
    private JList<String> list4;
    private JList<String> list5;
    private JPanel panel1;
    private JPanel panel2;
    private JPanel panel3;
    private JPanel panel4;
    private JPanel panel5;
    private String[] data = {"one", "two", "three", "four"};
    private int width = 110;
    private int height = 300;
    private GridBagConstraints gbc = new GridBagConstraints();

    private void displayGUI()
    {
        JFrame frame = new JFrame("JList Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new GridLayout(0, 5, 2, 2));      

        panel1 = getPanel(Color.GRAY, "List 1");
        list1 = new JList<String>(data);

        panel2 = getPanel(Color.GREEN.brighter().brighter(), "List 2");
        list2 = new JList<String>(data);         

        panel3 = getPanel(Color.ORANGE.brighter(), "List 3");
        list3 = new JList<String>(data);                  

        panel4 = getPanel(Color.BLUE.brighter(), "List 4");
        list4 = new JList<String>(data);                    

        panel5 = getPanel(Color.RED, "List 5");
        list5 = new JList<String>(data);        

        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.weightx = 1.0;
        gbc.weighty = 0.9;

        panel1.add(list1, gbc);
        panel2.add(list2, gbc);
        panel3.add(list3, gbc);
        panel4.add(list4, gbc);
        panel5.add(list5, gbc);

        contentPane.add(panel1);
        contentPane.add(panel2);
        contentPane.add(panel3);
        contentPane.add(panel4);
        contentPane.add(panel5);

        frame.setContentPane(contentPane);
        frame.setSize(610, 300);
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private JPanel getPanel(Color c, String title)
    {
        JPanel panel = new JPanel();
        panel.setOpaque(true);
        panel.setBorder(
            BorderFactory.createEmptyBorder(
                                GAP, GAP, GAP, GAP));       
        panel.setBackground(c);
        panel.setLayout(new GridBagLayout());   
        JLabel label = new JLabel(title, JLabel.CENTER);
        label.setAlignmentX(JLabel.CENTER_ALIGNMENT);

        gbc.fill = GridBagConstraints.NONE;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.weighty = 0.1;

        panel.add(label, gbc);

        return panel;
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new JListExample().displayGUI();
            }
        });
    }
}