2012-12-18 88 views
1

我正在尝试多次显示一个图像。这里是我的代码的一部分:如何在一个JFrame中多次添加一个图像?

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

不幸的是,创建的只是每种形式的一个图像。我究竟做错了什么?

+0

考虑使用画布,而不是jcomponents的。 (java.awt.Graphics) – Aaron

+0

1)为了更好地提供帮助,请发布[SSCCE](http://sscce.org/)。 2)'window.add(blue); window.setLayout(new GridLayout(3,3));'在***添加组件之前设置布局***,以获得可靠的结果。例如。 'window.setLayout(new GridLayout(3,3)); window.add(蓝色); // ..' –

+0

如果能帮助解决问题,请[接受答案](http://meta.stackexchange.com/a/5235/155831)。 –

回答

3

您必须创建9个独立的JLabel s以填充3 x 3网格。你不能重用Swing组件。

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

+0

只需创建一个'JLabel'数组。 – GGrec

1

如果要添加蓝色两次,则需要两个单独的JLabel实例,它们的编号为blue.jpgImageIcon。在这种情况下,你应该有一个像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到另一个两次问题。

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