1

我正在尝试多次显示一张图像。这是我的代码的一部分:

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

4 回答 4

3

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

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

于 2012-12-18T15:49:20.560 回答
2

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

棋盘

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;
    }
}
于 2012-12-18T23:10:58.433 回答
1

如果要添加蓝色两次,则需要两个单独的实例,JLabel它们的blue.jpg 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);

这不会是最灵活的解决方案,但可以解决您尝试将一个添加JComponent到另一个添加两次的问题。

于 2012-12-18T15:53:38.167 回答
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);
于 2012-12-20T21:28:43.607 回答