2

我正在尝试创建一个程序,其中包含三个按钮和一个带有 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();
        }
    }
} 

程序确实可以编译,但是当我按下按钮时,什么也没有发生!

4

1 回答 1

0

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

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

于 2013-11-09T18:53:41.957 回答