开发者

Changing squares to rectangles in simple a Swing program

I'm having some issues with what is probably a very simple problem in Java. I have written the following program that displays a 10x10 color matrix of squares whose colors are randomly generated and are randomly changed upon being clicked by a user in the window. However, if I wanted to change these squares to rectangles, for example, or any other shape how might I go about doing that? Is it even possible given the current code that I have? Thank you greatly for any help - sorry for the poor code indentation!

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class ColorGrid extends JLabel {

    private static final int n = 10;
    priva开发者_JS百科te static final Random random = new Random();

    public ColorGrid() {
        this.setOpaque(true);
        this.setBackground(new Color(random.nextInt()));
        this.setPreferredSize(new Dimension(50, 50));
    }

    private void GridOutput() {
        JFrame f = new JFrame("ColorGrid Display Window");
        f.setLayout(new GridLayout(n, n));
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        for (int i = 0; i < n * n; i++) {
            final ColorGrid label = new ColorGrid();
            label.addMouseListener(new MouseAdapter() {

                public void mousePressed(MouseEvent e) {
                    label.setBackground(new Color(random.nextInt()));
                }
            });
            f.add(label);
        }
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ColorGrid().GridOutput();
            }
        });
    }
}


As suggested here, just change the preferred size of the label:

this.setPreferredSize(new Dimension(64, 48));

Addendum:

any other shape

One way to get the convenience of a JComponent and the versatility needed for drawing different shapes is to implement the Icon interface, as suggested in this example


You aren't really drawling a rectangle, you are just setting the background of a label. I would suggest changing your ColorGrid item to override paintComponent in order to draw the correct shape and color. That object would also have to maintain shape and color states and respond to mouse clicks just as you do now.


You use GridLayout which by default should try to fill the entire frame, so if you add 10x10 labels they should be resized to fill the grid cells.

You might need to not set a preferredSize however, since that might cause the labels to be 50x50 in size at most (depends on the layout manager and I personally didn't use GridLayout that often).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜