2014-02-05 82 views
-1

我有以下代碼,並且在加載JLabel之後我正在努力做什麼。我如何顯示並給出座標位置?我在網上嘗試了一些解決方案,但他們似乎並沒有像「無法找到getCodeBased」之類的錯誤一樣工作。有人可以幫忙嗎?我仍然是初學者,所以請不要苛刻。以圖形導入圖像

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

public class ChessBoard extends JFrame implements ActionListener 
{ 
    private JButton button; 
    private JPanel panel; 
    JLayeredPane layeredPane; 

    public static void main(String[] args) 
    { 
     ChessBoard demo = new ChessBoard(); 
     demo.setSize(900,900); 
     demo.createGUI(); 
     demo.setVisible(true); 
    } 

    private void createGUI() 
    { 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     Container window = getContentPane(); 
     window.setLayout(new FlowLayout()); 

     panel = new JPanel(); 
     panel.setPreferredSize(new Dimension(800,800)); 
     panel.setBackground(Color.white); 
     window.add(panel); 

     button = new JButton("start"); 
     window.add(button); 
     button.addActionListener(this); 
    } 

    public void actionPerformed(ActionEvent event) 
    { 
     int xLeft; 
     int yTop; 
     Graphics paper = panel.getGraphics(); 
     paper.setColor(Color.black); 
     paper.fillRect(0,0,800,800); 
     paper.setColor(Color.white); 
     xLeft = 0; 
     for (int i = 100; i < 800; i += 100) 
     { 
      paper.drawLine(i,0,i,800); 
     } 
     for (int i = 100; i < 800; i += 100) 
     { 
      paper.drawLine(0, i, 800, i); 
     } 

     for (int j = 1; j < 9; j++) 
     { 
      paper.setColor(new Color(238, 221, 187)); 
      for (int k = 100 * ((j+1) % 2); k < 800; k+=200) 
      { 
       paper.fillRect (k, (j-1) * 100, 100, 100); 
      } 
      paper.setColor(new Color(204,136,68)); 
      for (int i = 100 * (j%2); i < 800; i+=200) 
      { 
       paper.fillRect(i, (j-1) * 100, 100, 100); 
      } 
     } 
    } 

    public void paint(Graphics g) 
    { 
     JLabel piece = new JLabel(new ImageIcon(getClass().getResource("Rooka8.png"))); 

    } 
} 

P.S.

設法解決它自己,試圖在actionPerformed方法張貼下面的代碼,以及import java.awt.Image;

ImageIcon myImage = new ImageIcon(...); 
      myImage.paintIcon(this, paper, ...,...); 
+0

'公共類ChessBoard..'哦,我[記得你(http://stackoverflow.com/q/21525019/418556)。如果你不理會我和Marco13的建議,我擔心你需要很長時間來創建一個破壞的GUI。祝你好運,我有其他人來幫忙。 –

回答

0

您可以全局聲明的變量的JLabel喜歡你的按鈕和麪板。然後在createGUI中將其實例化並將其添加到面板的特定位置以顯示它。希望這有助於