如何在一个JFrame中多次添加一个图像?

时间:2012-12-18 15:46:42

标签: java image swing jlabel layout-manager

我试图多次进行一次图像显示。这是我的代码的一部分:

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);

不幸的是,创建的只是每种图像。我做错了什么?

4 个答案:

答案 0 :(得分:3)

您必须创建9个单独的JLabel来填充3 x 3网格。您无法重复使用Swing组件。

您可以只创建一次蓝色图像图标和绿色图像图标。

答案 1 :(得分:2)

对于纯色,请考虑使用普通Icon(例如,实现为ColorIcon)。

Checker Board

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;
    }
}

答案 2 :(得分:1)

如果您想要添加蓝色两次,则需要两个具有JLabel blue.jpg的{​​{1}}个单独实例。在这种情况下,你应该有类似ImageIcon的东西。

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); 添加到另一个{2}的问题。

答案 3 :(得分:0)

如前所述,您无法重复使用它。我发现一个简单的修复方法是重新初始化JLabel!所以,如果你想要两个蓝色,只需这样做:

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

所以最终看起来像这样:

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);