问题
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);
}
}
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!
回答1:
You create a JLabel but add it to nothing.
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.
来源:https://stackoverflow.com/questions/19881700/buttons-and-icons-within-a-label-squareicon