JPanel在设置为图标数组时不显示标签,但会在单个图标

时间:2015-11-10 12:55:26

标签: java swing jpanel

当前代码

ThreeColorButton class: 

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    public class ThreeColorButton {

    private static CompositeIcon icons = new CompositeIcon();
    private static JPanel panel = new JPanel();
    private static JFrame frame = new JFrame();
    private static JLabel label = new JLabel();

    public static void main(String[] args) {

        //create rgb buttons
        JButton redButton = new JButton("Red");
        JButton greenButton = new JButton("Green");
        JButton blueButton = new JButton("Blue");

        //add rgb buttons to panel
        panel.add(redButton);
        panel.add(greenButton);
        panel.add(blueButton);

        //add action listeners to buttons
        redButton.addActionListener(buttonListener(40, Color.red));
        greenButton.addActionListener(buttonListener(40, Color.green));
        blueButton.addActionListener(buttonListener(40, Color.blue));

        frame.setLayout(new BorderLayout());
        frame.add(panel, BorderLayout.NORTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.pack();
        frame.setVisible(true);
    }
    private static ActionListener buttonListener(final int size,final Color color) {
        return new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                SquareIcon icon = new SquareIcon(size, color);
                icons.addIcon(icon);
                label.setIcon(icons);

                frame.add(label, BorderLayout.SOUTH);

                frame.repaint();
                frame.pack();
            }
        };
    }
}

CompositeIcon代码

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

    public class CompositeIcon implements Icon{

    private ArrayList<Icon> icons;
    private int size;

    public CompositeIcon() {
        icons = new ArrayList<Icon>();
    }

    public void paintIcon(Component c, Graphics g, int x, int y) {
        int position = x;
        for(Icon z : icons) {
            z.paintIcon(c,g,position,y);
            position = position + z.getIconWidth();
        }
    }
    public int getIconHeight() {
        return size;
    }
    public int getIconWidth() {
        int total = 0;
        for(Icon z : icons) {
            total = total + z.getIconWidth();
        }
        return total;
    }

    public void addIcon(Icon z) {
        icons.add(z);
    }

}

SquareIcon类只是一个简单的小类,可以创建具有给定大小的单一颜色的正方形。

我的问题是,在我的ThreeColorButton类中,当我运行它时,当我按下任一RGB按钮时,它不会显示任何图标。但是,在buttonListener方法中,如果我将label.setIcons(icons)设置为label.setIcons(icon),它会显示一个正方形并且不会将它并排放置。

我似乎无法弄清楚导致这种行为的原因。使用JLabel显示图标数组有问题吗?

2 个答案:

答案 0 :(得分:1)

由于您从未设置尺寸,因此Icon的高度为0,因此标签中无需绘制任何内容。

我建议代码类似:

@Override
public int getIconHeight()
{
    int size = 0;

    for(Icon z : icons)
    {
        size = Math.max(size, z.getIconHeight());
    }

    return size;
}

您可能需要查看Compound Icon。与您的课程类似,但它有更多功能。它支持水平/垂直/堆叠图标和图标对齐选项。

它不支持动态添加图标,因此您需要更改它。我可能会自己添加该功能(当我有时间时):)

答案 1 :(得分:0)

I can't seem to figure out whats causing this behavior. Is there a problem with displaying an array of icons using JLabel? 是的,正如@mKorbel所说,有一个问题。 JLabel只能显示一个图标。如果要显示n个图标,则需要n个JLabel实例。