标签内的按钮和图标 - SquareIcon [英] Buttons and Icons Within a Label - SquareIcon

查看:36
本文介绍了标签内的按钮和图标 - SquareIcon的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to create a program with three buttons and a label with a CompositeIcon that at the start is empty. When you click one of the buttons there will be added a square at the screen (with the described color), when another button is pressed there is gonna be added another square and so on. That means, when i press a button five times, there will be created five squres at the given colors. If any one will read my code i will be thankful.

/*
    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 的部分.

And here is the part with the 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();
        }
    }
} 

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

The program does compile, but when i press the buttons, nothing happens!

推荐答案

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

You create a JLabel but add it to nothing.

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

I suggest that you create a new JLabel each time you need one, and be sure to add it to the GUI after creating it. You will want to add it to a JPanel that is displayed in the JFrame/GUI, and will want to call revalidate() and repaint() on the JPanel after adding the label so that the panel knows to re-layout all its components, including the new one, and to re-draw itself and its children.

这篇关于标签内的按钮和图标 - SquareIcon的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆