How to add one image multiple times in one JFrame?

为君一笑 提交于 2019-12-11 10:23:16

问题


I am trying to make one image display multiple times. Here's part of my code:

JFrame window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setBounds(30, 30, 800, 600);
JLabel blue = new JLabel(new ImageIcon("blue.jpg"));
JLabel green = new JLabel(new ImageIcon("green.jpg"));
window.add(blue);
window.add(green);
window.add(blue);
window.setLayout(new GridLayout(3, 3));
window.setVisible(true);

Unfortunately what is created is just one image of each kind. What am I doing wrong?


回答1:


You have to create 9 separate JLabels to fill a 3 x 3 grid. You cannot reuse Swing components.

You can just create the blue image icon and the green image icon one time.




回答2:


For solid colors, consider using a plain Icon (e.g. implemented as ColorIcon) instead.

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class CheckerBoard {

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                // the GUI as seen by the user (without frame)
                JPanel gui = new JPanel(new BorderLayout());
                gui.setBorder(new EmptyBorder(2, 3, 2, 3));
                gui.setBackground(Color.RED.darker().darker());

                int w = 9;
                int h = 3;
                gui.setLayout(new GridLayout(h, w, 2, 2));
                for (int ii=0; ii<w*h; ii++) {
                    Color c = ii%2==0 ? Color.RED : Color.ORANGE;
                    gui.add(new JLabel(new ColorIcon(c, 16)));
                }

                JFrame f = new JFrame("Demo");
                f.add(gui);
                // Ensures JVM closes after frame(s) closed and
                // all non-daemon threads are finished
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                // See http://stackoverflow.com/a/7143398/418556 for demo.
                f.setLocationByPlatform(true);

                // ensures the frame is the minimum size it needs to be
                // in order display the components within it
                f.pack();
                // should be done last, to avoid flickering, moving,
                // resizing artifacts.
                f.setVisible(true);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
        SwingUtilities.invokeLater(r);
    }
}

class ColorIcon implements Icon {

    Color color;
    int preferredSize = -1;

    private ColorIcon() {
    }

    public ColorIcon(Color color, int preferredSize) {
        this.color = color;
        this.preferredSize = preferredSize;
    }

    @Override
    public void paintIcon(Component c, Graphics g, int x, int y) {
        g.setColor(color);
        g.fillRect(0, 0, preferredSize, preferredSize);
    }

    @Override
    public int getIconWidth() {
        return preferredSize;
    }

    @Override
    public int getIconHeight() {
        return preferredSize;
    }
}



回答3:


If you want to add blue twice, you will need two separate instances of JLabel that have the blue.jpg ImageIcon. in this case you should have something like a blue2.

JFrame window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setBounds(30, 30, 800, 600);
JLabel blue = new JLabel(new ImageIcon("blue.jpg"));
JLabel blue2 = new JLabel(new ImageIcon("blue.jpg"));
JLabel green = new JLabel(new ImageIcon("green.jpg"));
window.add(blue);
window.add(green);
window.add(blue2);
window.setLayout(new GridLayout(3, 3));
window.setVisible(true);

This is not going to be the most flexible solution, but address your problem of trying to add one JComponent to another twice.




回答4:


Like said previously you can't re-use it. A simple fix that I found was re initializing the JLabel! so if you want two blue's simply do this:

blue = new JLabel(new ImageIcon("blue.jpg"));

So it ends up looking like this:

JFrame window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setBounds(30, 30, 800, 600);
JLabel blue = new JLabel(new ImageIcon("blue.jpg"));
JLabel green = new JLabel(new ImageIcon("green.jpg"));
window.add(blue);
window.add(green);
blue = new JLabel(new ImageIcon("blue.jpg"));
window.add(blue);
window.setLayout(new GridLayout(3, 3));
window.setVisible(true);


来源:https://stackoverflow.com/questions/13936538/how-to-add-one-image-multiple-times-in-one-jframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!