Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java摆动位置测量单元?_Java_Swing_Position_Jlabel_Null Layout Manager - Fatal编程技术网

Java摆动位置测量单元?

Java摆动位置测量单元?,java,swing,position,jlabel,null-layout-manager,Java,Swing,Position,Jlabel,Null Layout Manager,我在做一个国际象棋游戏,我在使用一个棋盘,我用颜料在480x480制作了一个棋盘,从一些精灵那里得到了棋子,并制作了每个棋盘60x60和透明背景。我设法把棋盘和棋子放在屏幕上,但位置很乱。我使用的是空布局。我做到了: chessBoardLabel.setBounds(0, 0+25, 480, 480); +25是因为框架位于顶部,看起来像是在定位时考虑的。 至于这件作品,例如: for (int i = 0; i <= 7; i++) whitePawnArray[i] = new

我在做一个国际象棋游戏,我在使用一个棋盘,我用颜料在
480x480
制作了一个棋盘,从一些精灵那里得到了棋子,并制作了每个棋盘
60x60
和透明背景。我设法把棋盘和棋子放在屏幕上,但位置很乱。我使用的是空布局。我做到了:

chessBoardLabel.setBounds(0, 0+25, 480, 480);
+25
是因为框架位于顶部,看起来像是在定位时考虑的。 至于这件作品,例如:

for (int i = 0; i <= 7; i++)
 whitePawnArray[i] = new whitePawnPiece(i*60,420+25); 
但这种情况会发生:

如果我这样做:

for (int i = 0; i <= 7; i++)
whitePawnArray[i] = new whitePawnPiece(i*40,280+15);
for(int i=0;i
  • 不要为此使用CardLayout
  • 我会使用一个使用GridLayout的JPanel来容纳一个8x8的棋盘格JPanel
  • 我会把它放在一个窗格里
  • 我会把我的棋子添加到适当的棋盘上
  • 当移动一个部件时,我会将其提升到JLayeredPane的拖动层
此外:

  • 不要直接在顶级窗口(如JFrame)上绘制
  • 不要重写绘制方法
  • 相反,如果必须进行绘图,请覆盖JPanel或JComponent的
    paintComponent(Graphics g)
  • 但是,同样,如果你用小的JPanel象棋方块来创建你的棋盘,那么在棋盘上放置棋子并将其放置好是很容易和自然的

例如,请检查我的代码。

嗯,这就是为什么它强烈推荐使用而不是绝对定位。也许你应该考虑使用单个组件并在其中进行着色(覆盖)。@Azad但当我使用NetBeans IDE制作GUI时,我无法覆盖图像,除非使用空/绝对布局。是否有任何布局不会使我的生活复杂化,让我将两个图像放在同一位置,并使用类似setComponentZOrder的东西?@xav但移动这些图像片段将是一场噩梦,不是吗?@WalrusNine:是的,
CardLayout
可以为您这样做;)OP覆盖了框架的
paint
,这就是为什么他们需要+25偏移量的原因…这将在他们改变外观和感觉时被破坏。介意提及正确的绘制过程吗…我可以使用带有paintComponent的文件,还是只使用JLabel?这也是他们在放置这些对象时遇到问题的原因。OP似乎已将这些片段添加到内容窗格中,并试图使用框架的位置上下文来放置元素,这会将它们推离位置(因为上下文空间坐标错误)-我在你可以借用的评论中有链接,除非你有自己的…@WalrusNine这就是为什么你应该提供一个可运行的问题示例。在哪里根据经验对观察结果进行猜测…@WalrusNine你的代码是一堆错综复杂的循环引用…为什么
whitePiece
chessMain
扩展到你的主框架????
for (int i = 0; i <= 7; i++)
whitePawnArray[i] = new whitePawnPiece(i*40,280+15);
package chess.game;
import java.util.*;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class chessMain extends JFrame{
    public static void main (String arguments[]){

    //Create White
    whitePiece white_piece = new whitePiece();

    whitePawnPiece[] whitePawnArray = new whitePawnPiece[8];
    for (int i = 0; i <= 7; i++) whitePawnArray[i] = new whitePawnPiece(i*40,280+15);

    /*whiteTowerPiece[] whiteTowerArray = new whiteTowerPiece[2];
    whiteTowerArray[0] = new whiteTowerPiece(0,420);
    whiteTowerArray[1] = new whiteTowerPiece(420,420);

    whiteHorsePiece[] whiteHorseArray = new whiteHorsePiece[2];
    whiteBishopPiece[] whiteBishopArray = new whiteBishopPiece[2];
    whiteKingPiece whiteKing = new whiteKingPiece();
    whiteQueenPiece whiteQueen = new whiteQueenPiece();*/

    //Create Black

    JFrame frame = new JFrame();
    JPanel panel;

    //Initialize
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setTitle("Chess");
    frame.setSize (640,640);
    frame.setResizable(false);
    frame.setLayout(null);
    panel = new JPanel();
    panel.setLayout(null);
    frame.getContentPane().add(panel);

    //draw chessBoard
    ImageIcon chessBoardIcon = new           ImageIcon(frame.getClass().getResource("/chess/art/Chess_Art/chessBoard.png"));
    JLabel chessBoardLabel = new JLabel(chessBoardIcon);
    panel.add(chessBoardLabel);
    chessBoardLabel.setBounds(0, 0+25, 480, 480);
    frame.setComponentZOrder(chessBoardLabel, 1);
    frame.setComponentZOrder(panel, 2);
    //draw Pawn
    for (int i = 0; i<=7; i++){
        panel.add(whitePawnArray[i].whitePawnLabel);
        whitePawnArray[i].draw();
        frame.setComponentZOrder(whitePawnArray[i].whitePawnLabel, 0);
    }

    frame.setVisible(true);

}
}

public class whitePawnPiece extends whitePiece{
    JLabel whitePawnLabel;
    ImageIcon whitePawnIcon;
    public whitePawnPiece(int x, int y){
        whitePawnIcon = new ImageIcon(getClass().getResource("/chess/art/Chess_Art/white/whitePawnPiece.png"));
        whitePawnLabel = new JLabel (whitePawnIcon);
        //whitePawnLabel.setOpaque(true);
        this.xPos = x;
        this.yPos = y;
        //this.draw();
    }

    @Override
    public void move(int newX, int newY){
        this.xPos = (newX/60)*60;                     //calcular nova pos
        this.yPos = (newY/60)*60;
        this.draw();
    }
    /*public void possibleMoves(){
        selectorMark.drawNew(this.xPos, this.yPos);
        selectorMark.drawNew(this.xPos - 60, this.yPos - 60);
        if (this.yPos == 420)  selectorMark.drawNew(this.xPos - 120, this.yPos - 120);
    }*/

    @Override
    public void draw(){
        //whitePawnIcon.paintIcon(null, chessGUI2.getGraphics(), xPos, xPos);
        whitePawnLabel.setBounds(this.xPos, this.yPos, this.xPos+60, this.yPos+60);     //x, y, width, height
    }

}

public class whitePiece{
    int xPos, yPos;

    public void move(){}
    public void draw(){}

}