2012-12-18 92 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);