2014-01-12 5 views
-4

문제는 보드를 화면 중앙에 배치하고 싶습니다. 나는 setBounds 등등 JFrame에 setLocation 방법으로 시도했지만 그렇게해서 작동하지 않았다. 또한 chessBoard.setBounds(0, 0, boardSize.width, boardSize.height)에 x 축 및 y 축 값을 설정하려고했습니다. 그것은 화면에 전체 보드를 중심으로하는 데 도움이되었지만 하나의 문제가 발생했습니다. 이제는 내 조각이 움직이지 않으며 MouseListenerMouseMotionListener은 예외를 보여줍니다 ...스크린에 자바 체스 보드를 놓으시겠습니까?

누구든지이 상황에 도움이 될 수 있습니까? 디버깅을 시도했지만 아무 것도 잘되지 않습니다!

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 클래스.

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 확실히 말할 수는 없지만 SO에서 보았을 때 함께 실행되는 모든 코드 줄을 중지시키기 위해 * 가끔 OP에 의해 수행됩니다. –

답변

2

최고는 setLocationByPlatform(true);입니다. 자세한 내용은 this answer를 참조하십시오

관련 문제