2013-04-25 71 views
0

我对Java相当陌生,于是决定尝试用Swing制作国际象棋游戏。 我知道我的程序效率极低,但这并不是我的问题。 我的问题是我添加了JButton后无法查看白色棋子的图像,但是如果我在添加按钮之前添加了代码来添加棋子JLabel,则可以看到棋子。出于这个原因,我认为问题是我有一个分层问题,我尝试用LayeredPanes替换面板,但没有任何成功。国际象棋分层问题

基本上,我想要发生的是让JButton和JLabel都显示在同一个方块中,并保持按钮可点击。 另外,我真的很想不纳入任何新的进口,只使用JButtons,JLabels等。 对不起,如果我的解释有点混乱,并提前感谢。 这里是我的代码:

private JFrame frame = new JFrame(); 
private JPanel[][] square = new JPanel[8][8]; 
private JButton[][] squareB = new JButton[8][8]; 
private JPanel blank[] = new JPanel[6]; 
private JButton newGame = new JButton("New Game"); 
private JButton exitGame = new JButton("Exit"); 

ArrayList <Chess> pieces = new ArrayList<Chess>(); 
ChessGame() throws IOException 
{ 

    frame.setLayout(new GridLayout(9,8)); 
    frame.setSize(800,900); 
    for(int x=0; x<8; x++) 
    { 
     for(int y=0; y<8; y++) 
     { 
      square[x][y] = new JPanel(); 
      square[x][y].setSize(100,100); 
     } 
    } 

    for(int y=0; y<8; y++) 
    { 
     for(int x=0; x<8; x++) 
     { 
      frame.add(square[x][y]); 
      if(y%2==0) 
       if(x%2==0) 
        square[x][y].setBackground(Color.cyan); 
       else 
        square[x][y].setBackground(Color.blue); 
      else 
       if(x%2==0) 
        square[x][y].setBackground(Color.blue); 
       else 
        square[x][y].setBackground(Color.cyan); 
     } 
    } 
    for(int x=0; x<8; x++) 
    { 
     for(int y=0; y<8; y++) 
     { 
      squareB[x][y] = new JButton(); 
      squareB[x][y] = new Chess(x,y); 
      square[x][y].add(squareB[x][y]); 
      squareB[x][y].setSize(square[x][y].getSize()); 
      squareB[x][y].setMargin(new Insets(0, 0, 100, 100)); 
      squareB[x][y].setContentAreaFilled(false); 
      squareB[x][y].setOpaque(false); 
      squareB[x][y].setContentAreaFilled(false); 
      squareB[x][y].setBorderPainted(false); 
      squareB[x][y].addActionListener(this); 
     } 
    } 
    for(int x=0; x<6; x++) 
    { 
     blank[x] = new JPanel(); 
     frame.add(blank[x]); 
     if(x==2) 
     { 
      frame.add(newGame); 
      frame.add(exitGame); 
     } 
    } 

    JLabel WPawn = new JLabel(new ImageIcon(((new ImageIcon("C:\\Users\\Matthew\\Desktop\\Chess Pieces\\WPawn.png")).getImage()).getScaledInstance(80, 83, java.awt.Image.SCALE_SMOOTH))); 
    squareB[1][1].add(WPawn); 

    newGame.addActionListener(this); 
    exitGame.addActionListener(this); 
    frame.setVisible(true); 
} 
public static void main(String[] args) throws IOException 
{ 
    new ChessGame(); 
} 
+1

为了更快得到更好的帮助,请发布[SSCCE](http://sscce.org/)..只有几个棋子。 – 2013-04-25 00:50:30

+1

请参阅此[示例](http://stackoverflow.com/a/2562685/230513)和[变体](http://stackoverflow.com/a/2563350/230513)。 – trashgod 2013-04-25 00:50:43

+0

我知道这有点傻,但我有点想用JButton。这不可能吗? – user2317760 2013-04-25 00:54:36

回答

1

不要试图一个JLabel添加到JButton,它没有布局管理器,设置JButton s图标的

Image WPawn = new ImageIcon(ImageIcon("C:\\Users\\Matthew\\Desktop\\Chess Pieces\\WPawn.png").getImage().getScaledInstance(80, 83, java.awt.Image.SCALE_SMOOTH)); 
squareB[1][1].setIcon(WPawn); 

你可能想有一个近距离观察在How to use Buttons

我也建议ImageIO超过ImageIcon读取图像

+0

非常感谢。 – user2317760 2013-04-25 01:13:43