2014-01-12 105 views
-4

問題是我想把電路板放在屏幕的中心。我已經嘗試了JFrame中的setLocation方法,setBounds等,但它沒有以這種方式工作。我也嘗試設置chessBoard.setBounds(0, 0, boardSize.width, boardSize.height)中的x軸和y軸值。它確實幫助我將整個板子居中在屏幕上,但還有一個問題出現了:現在我的棋子不移動,並且MouseListenerMouseMotionListener顯示異常...在屏幕上放置Java國際象棋棋盤?

任何人都可以幫助我解決這種情況嗎?我試圖調試它,但沒有任何反應對我有好處!

import java.awt.*; 
import java.awt.event.*; 
import java.util.*; 
import javax.swing.*; 
import static javax.swing.JFrame.EXIT_ON_CLOSE; 
import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE; 

public class ChessInterface extends JFrame implements MouseListener, MouseMotionListener { 
    JLayeredPane layeredPane; 
    JPanel chessBoard; 
    JLabel chessPiece; 
    int xAdjustment; 
    int yAdjustment; 
    public ChessInterface(){ 
     Dimension boardSize = new Dimension(400, 400); 
     // Use a Layered Pane for this this application 
     //FlowLayout flow=new FlowLayout(); 
     layeredPane = new JLayeredPane(); 
     getContentPane().add(layeredPane); 
     layeredPane.setPreferredSize(boardSize); 
     layeredPane.addMouseListener(this); 
     layeredPane.addMouseMotionListener(this); 
     //layeredPane.setLocation(90, 90); 

     //Add a chess board to the Layered Pane 
     chessBoard = new JPanel(); 
     layeredPane.add(chessBoard, JLayeredPane.DEFAULT_LAYER); 
     //chessBoard.setLocation(90, 90); 
     chessBoard.setLayout(new GridLayout(8, 8)); 
     //chessBoard.setPreferredSize(boardSize); 
     chessBoard.setBounds(0, 0, boardSize.width, boardSize.height); 

     for (int i = 0; i < 64; i++) { 
      JPanel square = new JPanel(new BorderLayout()); 
      chessBoard.add(square); 
      int row = (i/8) % 2; 
      if (row == 0) 
       square.setBackground(i % 2 == 0 ? Color.black : Color.white); 
      else 
       square.setBackground(i % 2 == 0 ? Color.white : Color.black); 
     } 
     //Add pieces to board 
     JLabel piece; 
     piece = new JLabel(new ImageIcon("C:\\Users\\Moemmer\\Documents\\NetBeansProjects\\ChessGame\\src\\Images\\23797642-complete-wooden-chess-set-with-s-full-complement-of-chess-pieces-in-both-colours-lined-up-in-rows-is - Copy.jpg")); 
     JPanel panel = (JPanel)chessBoard.getComponent(0); 
     panel.add(piece); 
     piece = new JLabel(new ImageIcon("C:\\Users\\Moemmer\\Documents\\NetBeansProjects\\ChessGame\\src\\Images\\23797642-complete-wooden-chess-set-with-s-full-complement-of-chess-pieces-in-both-colours-lined-up-in-rows-is - Copy.jpg")); 
     panel = (JPanel)chessBoard.getComponent(15); 
     panel.add(piece); 
     piece = new JLabel(new ImageIcon("C:\\Users\\Moemmer\\Documents\\NetBeansProjects\\ChessGame\\src\\Images\\23797642-complete-wooden-chess-set-with-s-full-complement-of-chess-pieces-in-both-colours-lined-up-in-rows-is - Copy.jpg")); 
     panel = (JPanel)chessBoard.getComponent(16); 
     panel.add(piece); 
     piece = new JLabel(new ImageIcon("C:\\Users\\Moemmer\\Documents\\NetBeansProjects\\ChessGame\\src\\Images\\23797642-complete-wooden-chess-set-with-s-full-complement-of-chess-pieces-in-both-colours-lined-up-in-rows-is - Copy.jpg")); 
     panel = (JPanel)chessBoard.getComponent(20); 
     panel.add(piece); 
    } 

    public void mousePressed(MouseEvent e){ 
     chessPiece = null; 
     Component c = chessBoard.findComponentAt(e.getX(), e.getY()); 
     if (c instanceof JPanel) 
      return; 
     Point parentLocation = c.getParent().getLocation(); 
     xAdjustment = parentLocation.x - e.getX(); 
     yAdjustment = parentLocation.y - e.getY(); 
     chessPiece = (JLabel)c; 
     chessPiece.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment); 
     chessPiece.setSize(chessPiece.getWidth(), chessPiece.getHeight()); 
     layeredPane.add(chessPiece, JLayeredPane.DRAG_LAYER); 
    } 

    //Move the chess piece around 

    public void mouseDragged(MouseEvent me) { 
     if (chessPiece == null) return; 
     chessPiece.setLocation(me.getX() + xAdjustment, me.getY() + yAdjustment); 
    } 

    //Drop the chess piece back onto the chess board 

    public void mouseReleased(MouseEvent e) { 
     if(chessPiece == null) return; 
     chessPiece.setVisible(false); 
     Component c = chessBoard.findComponentAt(e.getX(), e.getY()); 

     if (c instanceof JLabel){ 
      Container parent = c.getParent(); 
      parent.remove(0); 
      parent.add(chessPiece); 
     } 

     else { 
      Container parent = (Container)c; 
      parent.add(chessPiece); 
     } 

     chessPiece.setVisible(true); 
    } 

    public void mouseClicked(MouseEvent e) { 

    } 

    public void mouseMoved(MouseEvent e) { 

    } 

    public void mouseEntered(MouseEvent e){ 

    } 

    public void mouseExited(MouseEvent e) { 

    } 

    public void BackgroundImageJFrame() 

    { 

    } 
} 

ChessGame class。

import java.awt.Dimension; 
import java.awt.Toolkit; 
import javax.swing.JFrame; 
import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE; 

/** 
* 
* @author Moemmer 
*/ 
public class ChessGame { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     JFrame frame = new ChessInterface(); 
     frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE); 
     frame.pack(); 
     frame.setResizable(true); 

     //Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
     // int height = screenSize.height; 
     //int width = screenSize.width; 
     //frame.setSize(width/2, height/2); 
     //frame.setLocation(90, 90); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
     // TODO code application logic here 
    } 
} 
+2

1)請使用代碼格式化像HTML或XML代碼,輸入/輸出結構化文檔。爲此,請選擇樣本並單擊郵件發佈/編輯表單上方的「{}」按鈕。 2)對代碼塊使用一致的邏輯縮進。代碼的縮進旨在幫助人們理解程序流程。 –

+1

並且不要將所有內容都加倍。 –

+0

@HotLicks我不能肯定地說,但是有時*由OP完成,以便在SO中看到所有代碼行一起運行。 –

回答

2

最好是setLocationByPlatform(true);。詳情請參見this answer