标签内的按钮和图标 - SquareIcon

时间:2013-11-09 18:50:01

标签: java swing composite

我正在尝试创建一个带有三个按钮的程序和一个带有CompositeIcon的标签,该标签在开头是空的。 当您单击其中一个按钮时,将在屏幕上添加一个正方形(具有所描述的颜色),当按下另一个按钮时,将添加另一个正方形,依此类推。这意味着,当我按下按钮五次时,将以给定的颜色创建五个方块。 如果有人会阅读我的代码,我将感激不尽。

/*
    This program creates a window with three buttons - red, green and blue - and a label with an icon.
    This icon is a CompositeIcon that at the start is empty. When we press one of the three buttons, there will
    be added a square at the specified color.
*/

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

public class ResponsiveFrame extends JFrame {
    static SquareIcon icon;
    static int number; // this
    static Color awtColor; // this

    public static void main(String[] args) {
        final JFrame frame = new JFrame();
        final JLabel label = new JLabel();

        JButton redButton = new JButton("RED");
        JButton greenButton = new JButton("GREEN");
        JButton blueButton = new JButton("BLUE");

        /*
            this is the part that i am not sure about!
            not sure about the parameters.
        */      
        icon = new SquareIcon(number, awtColor);

        redButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                icon.addIcon(new SquareIcon(20, Color.RED));
                label.setIcon(icon);
                frame.repaint();
                frame.pack();
            }
        });

        greenButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                icon.addIcon(new SquareIcon(20, Color.GREEN));
                label.setIcon(icon);
                frame.repaint();
                frame.pack();
            }
        });

        blueButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                icon.addIcon(new SquareIcon(20, Color.BLUE));
                label.setIcon(icon);
                frame.repaint();
                frame.pack();
            }
        });

        frame.setLayout(new FlowLayout());

        frame.add(redButton);
        frame.add(greenButton);
        frame.add(blueButton);
        frame.add(label);

        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

这是SquareIcon的部分。

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

public class SquareIcon implements Icon {
        private ArrayList<Icon> icons;
        private int width;
        private int height;

    public SquareIcon(int number, Color awtColor) {
        icons = new ArrayList<Icon>();
        number = number;
        awtColor = awtColor;
    }

    public void addIcon(Icon icon) {
        icons.add(icon);
        width += icon.getIconWidth();
        int iconHeight = icon.getIconHeight();
        if (height < iconHeight)
            height = iconHeight;
    }

    public int getIconHeight() {
        return height;
    }

    public int getIconWidth() {
        return width;
    }

    public void paintIcon(Component c, Graphics g, int x, int y) {
        for (Icon icon : icons) {
            icon.paintIcon(c, g, x, y);
            x += icon.getIconWidth();
        }
    }
} 

程序确实编译,但是当我按下按钮时,没有任何反应!

1 个答案:

答案 0 :(得分:0)

您创建了一个JLabel但是将其添加到任何内容。

我建议您每次需要时创建一个新的JLabel,并确保在创建后添加到GUI。您需要将其添加到JFrame / GUI中显示的JPanel,并且需要在添加标签后调用JPanel上的revalidate()repaint(),以便面板知道重新布局所有组件,包括新组件,并重新绘制自己及其子项。