2011-11-06 185 views
0

我試圖在JPanel頂部放置一個.jpg圖標,以表示板上的一塊電路板。我有一個包含.java文件的GUI文件夾和另一個包含.jpg文件的文件夾。在JPanel頂部添加JLabel

- 主要Edit-- 示例代碼

當方點擊白色圖標是爲了放置,然後黑色等等等等,這是什麼即時試圖實現

一個非常基本的例子
import java.awt.Dimension; 

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

public class gui extends JFrame implements MouseListener { 

/** 
* 
*/ 
private static final long serialVersionUID = -973341728129968945L; 
JLayeredPane layeredPane; 
JPanel board; 
JLabel piece; 
int numSquares; 
private boolean currentPlayer; 

public gui(){ 

    Dimension boardSize = new Dimension(600, 600); 

    numSquares = 6; 
    currentPlayer = true; 

    layeredPane = new JLayeredPane(); 
    getContentPane().add(layeredPane); 
    layeredPane.setPreferredSize(boardSize); 
    layeredPane.addMouseListener(this); 

    board = new JPanel(); 
    layeredPane.add(board, JLayeredPane.DEFAULT_LAYER); 

    board.setLayout(new GridLayout(numSquares, numSquares)); 
    board.setPreferredSize(boardSize); 
    board.setBounds(0, 0, boardSize.width, boardSize.height); 

    for (int i = 0; i < (numSquares * numSquares); i++) { 
     JPanel square = new JPanel(new BorderLayout()); 
     square.setBorder(BorderFactory.createLineBorder(Color.black)); 
     square.setBackground(Color.green); 
     board.add(square); 


    } 



} 

public static void main(String[] args) { 
     JFrame frame = new gui(); 
     frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE); 
     frame.pack(); 
     frame.setResizable(true); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 



@Override 
public void mouseClicked(MouseEvent e) { 
    JPanel temp = (JPanel)board.findComponentAt(e.getX(), e.getY()); 
    System.out.println(e.getX() + " " + e.getY()); 

    if(currentPlayer){ 
     ImageIcon white = new ImageIcon("l/Images/white.jpg"); 
     piece = new JLabel(white); 
     temp.add(piece); 
    } 
    else{ 
     ImageIcon black = new ImageIcon("/Images/black.jpg"); 
     piece = new JLabel(black); 
     temp.add(piece); 
    } 
    currentPlayer = !currentPlayer; 


} 


@Override 
public void mouseEntered(MouseEvent e) { 


} 

@Override 
public void mouseExited(MouseEvent arg0) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void mousePressed(MouseEvent arg0) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void mouseReleased(MouseEvent e) { 

} 




} 
+0

我刪除了我的答案,因爲我看到你已經使用JPanels作爲電路板的單元。我認爲你可能需要創建並向我們展示一個[sscce](http://sscce.org),以便我們能夠幫助你。 –

+0

沒有任何東西可以顯示。代碼運行時,會創建一個6乘6的黑板背景。然後,當一個cellis點擊一個令牌(白色或黑色)是爲了放置。但沒有任何反應。 – CNevin561

+0

如果你發佈了可編譯的,可運行的代碼,我們可以更容易地研究它,理解它,修改它,並可能幫助你糾正它。由你決定。 –

回答

4

如果添加或刪除容器中的組件,請不要忘記重新驗證並重新繪製。我已經修改了您的SSCCE,並且已經擺脫了使用圖像使其無法訪問您的圖像文件(像我一樣)的需要。變化由// !!評論中指出:

import java.awt.Color; 
import java.awt.Dimension; 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.awt.image.BufferedImage; 

public class Gui2 extends JFrame implements MouseListener { 

    private static final long serialVersionUID = -973341728129968945L; 
    JLayeredPane layeredPane; 
    JPanel board; 
    JLabel piece; 
    int numSquares; 
    private boolean currentPlayer; 

    // !! 
    private ImageIcon whiteIcon; 
    private ImageIcon blackIcon; 

    public Gui2() { 
     // !! 
     whiteIcon = createIcon(Color.white); 
     blackIcon = createIcon(Color.black); 

     Dimension boardSize = new Dimension(600, 600); 

     numSquares = 6; 
     currentPlayer = true; 

     layeredPane = new JLayeredPane(); 
     getContentPane().add(layeredPane); 
     layeredPane.setPreferredSize(boardSize); 
     layeredPane.addMouseListener(this); 

     board = new JPanel(); 
     layeredPane.add(board, JLayeredPane.DEFAULT_LAYER); 

     board.setLayout(new GridLayout(numSquares, numSquares)); 
     board.setPreferredSize(boardSize); 
     board.setBounds(0, 0, boardSize.width, boardSize.height); 

     for (int i = 0; i < (numSquares * numSquares); i++) { 
     // !! JPanel square = new JPanel(new BorderLayout()); 
     JPanel square = new JPanel(new GridBagLayout()); // !! 

     square.setBorder(BorderFactory.createLineBorder(Color.black)); 
     square.setBackground(Color.green); 
     square.setName(String.format("[%d, %d]", i % numSquares, i 
      /numSquares)); // !! 
     board.add(square); 

     } 

    } 

    // !! 
    private ImageIcon createIcon(Color color) { 
     int width = 40; 
     int height = width; 
     BufferedImage img = new BufferedImage(width, height, 
      BufferedImage.TYPE_INT_ARGB); 
     Graphics2D g2 = img.createGraphics(); 
     g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
      RenderingHints.VALUE_ANTIALIAS_ON); 
     g2.setColor(color); 
     g2.fillOval(0, 0, width, height); 
     g2.dispose(); 
     ImageIcon icon = new ImageIcon(img); 
     return icon; 
    } 

    public static void main(String[] args) { 
     JFrame frame = new Gui2(); 
     frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE); 
     frame.pack(); 
     frame.setResizable(true); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    @Override 
    // !! 
    public void mousePressed(MouseEvent e) { 
     JPanel temp = (JPanel) board.findComponentAt(e.getX(), e.getY()); 
     System.out.println(e.getX() + " " + e.getY()); 
     System.out.println(temp.getName()); // !! 

     if (currentPlayer) { 
     // !! ImageIcon white = new ImageIcon("l/Images/white.jpg"); 
     // !! piece = new JLabel(white); 
     piece = new JLabel(whiteIcon); // !! 
     temp.add(piece); 
     } else { 
     // !! ImageIcon black = new ImageIcon("/Images/black.jpg"); 
     // !! piece = new JLabel(black); 
     piece = new JLabel(blackIcon); // !! 
     temp.add(piece); 
     } 
     temp.revalidate(); // !! 
     temp.repaint(); // !! 
     currentPlayer = !currentPlayer; 

    } 

    @Override 
    public void mouseEntered(MouseEvent e) { 

    } 

    @Override 
    public void mouseExited(MouseEvent arg0) { 
    } 

    @Override 
    public void mouseClicked(MouseEvent arg0) { 
    } 

    @Override 
    public void mouseReleased(MouseEvent e) { 

    } 

} 

而且類名應該大寫,也應再次進行ImageIcons一次。同樣,一個ImageIcon可以被許多JLabel共享。您還需要響應mousePressed而不是mouseClicked,因爲mouseClicked可能會非常繁瑣,特別是在按下鼠標和釋放鼠標之間移動鼠標時。

希望你也看到了SSCCE的價值。 :)

+0

輕微的非編程建議 - 用字母A-H(對於列)和數字1-8(對於該行)命名方塊。例如白王從E1開始。就像在真正的國際象棋。 – user949300

+0

其實我認爲OP創造的遊戲是Reversi,而不是國際象棋,但是如果我的記憶爲我服務,那麼隊伍和文件的命名與國際象棋相同。 –

+0

發佈SSCCE – mKorbel