Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 标签内的按钮和图标-方形图标_Java_Swing_Composite - Fatal编程技术网

Java 标签内的按钮和图标-方形图标

Java 标签内的按钮和图标-方形图标,java,swing,composite,Java,Swing,Composite,我正在尝试创建一个包含三个按钮和一个标签的程序,该标签的开头是空的。 当您单击其中一个按钮时,屏幕上将添加一个正方形(具有描述的颜色),当按下另一个按钮时,将添加另一个正方形,依此类推。这意味着,当我按下一个按钮五次时,将在给定的颜色下创建五个正方形。 如果有人会读我的代码,我会很感激 /* This program creates a window with three buttons - red, green and blue - and a label with an icon.

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

/*
    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);
    }
}
这是带有方形图标的部分

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();
        }
    }
} 
import java.util.*;
导入java.awt.*;
导入javax.swing.*;
公共类SquareIcon实现Icon{
私有ArrayList图标;
私有整数宽度;
私人内部高度;
公共方形图标(整数、颜色awtColor){
icons=newarraylist();
数字=数字;
awtColor=awtColor;
}
公共无效附加组件(图标){
图标。添加(图标);
宽度+=icon.getIconWidth();
int iconHeight=icon.getIconHeight();
如果(高度<图标高度)
身高=身高;
}
public int getIconHeight(){
返回高度;
}
public int getIconWidth(){
返回宽度;
}
公共虚空绘制图标(组件c、图形g、整数x、整数y){
用于(图标:图标){
图标。绘画图标(c,g,x,y);
x+=icon.getIconWidth();
}
}
} 

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

您创建了一个JLabel,但没有添加它

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