2

3 つのボタンと、最初は空の CompositeIcon を持つラベルを持つプログラムを作成しようとしています。ボタンの 1 つをクリックすると、画面に四角形が追加され (記述された色で)、別のボタンを押すと別の四角形が追加されます。つまり、ボタンを 5 回押すと、指定された色で 5 つの正方形が作成されます。誰かが私のコードを読んでくれるなら、私は感謝します。

/*
    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()andを呼び出して、パネルが新しいコンポーネントを含むすべてのコンポーネントを再レイアウトすることを認識できるようにする必要があります。repaint()それ自体とその子を再描画します。

于 2013-11-09T18:53:41.957 に答える