gpt4 book ai didi

java - 在 Java 程序中链接图像

转载 作者:行者123 更新时间:2023-11-29 07:44:41 26 4
gpt4 key购买 nike

快速提问 我有一个名为 Board.java 的 java 文件,它创建并放置棋子,但我目前有一个问题,它只制作棋盘本身但不分配棋子

我假设我没有链接 png 的权利

这是我使用 Netbeans 构建游戏的设置的屏幕截图 我已经对其进行了截图,因此您可以看到图像的存储位置以及到目前为止我是如何编写程序的

此外,在你问我运行程序时没有出现错误之前,它只是弹出一个没有棋子的空棋盘

enter image description here

这里还有Java文件

棋盘.java

package chessgame;

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

public class Board extends JFrame {

JLayeredPane layeredPane;
JPanel chessBoard;
JLabel chessPiece;
int xAdjustment;
int yAdjustment;
int startX;
int startY;
int initialX;
int initialY;
JPanel panels;
JLabel pieces;

public Board(){
Dimension boardSize = new Dimension(700, 700);

// This is a Layered Pane for this application
layeredPane = new JLayeredPane();
getContentPane().add(layeredPane);
layeredPane.setPreferredSize(boardSize);

//Add a chess board to the Layered Pane
chessBoard = new JPanel();
layeredPane.add(chessBoard, JLayeredPane.DEFAULT_LAYER);
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.white : Color.gray );
else
square.setBackground( i % 2 == 0 ? Color.gray : Color.white );
}
// Setting up the Initial Chess board.
for(int i=8;i < 16; i++){
pieces = new JLabel( new ImageIcon("WhitePawn.png") );
panels = (JPanel)chessBoard.getComponent(i);
panels.add(pieces);
}
pieces = new JLabel( new ImageIcon("WhiteRook.png") );
panels = (JPanel)chessBoard.getComponent(0);
panels.add(pieces);
pieces = new JLabel( new ImageIcon("WhiteKnight.png") );
panels = (JPanel)chessBoard.getComponent(1);
panels.add(pieces);
pieces = new JLabel( new ImageIcon("WhiteKnight.png") );
panels = (JPanel)chessBoard.getComponent(6);
panels.add(pieces);
pieces = new JLabel( new ImageIcon("WhiteBishup.png") );
panels = (JPanel)chessBoard.getComponent(2);
panels.add(pieces);
pieces = new JLabel( new ImageIcon("WhiteBishup.png") );
panels = (JPanel)chessBoard.getComponent(5);
panels.add(pieces);
pieces = new JLabel( new ImageIcon("WhiteKing.png") );
panels = (JPanel)chessBoard.getComponent(3);
panels.add(pieces);
pieces = new JLabel( new ImageIcon("WhiteQueen.png") );
panels = (JPanel)chessBoard.getComponent(4);
panels.add(pieces);
pieces = new JLabel( new ImageIcon("WhiteRook.png") );
panels = (JPanel)chessBoard.getComponent(7);
panels.add(pieces);
for(int i=48;i < 56; i++){
pieces = new JLabel( new ImageIcon("BlackPawn.png") );
panels = (JPanel)chessBoard.getComponent(i);
panels.add(pieces);
}
pieces = new JLabel( new ImageIcon("PieceImages/BlackRook.png") );
panels = (JPanel)chessBoard.getComponent(56);
panels.add(pieces);
pieces = new JLabel( new ImageIcon("BlackKnight.png") );
panels = (JPanel)chessBoard.getComponent(57);
panels.add(pieces);
pieces = new JLabel( new ImageIcon("BlackKnight.png") );
panels = (JPanel)chessBoard.getComponent(62);
panels.add(pieces);
pieces = new JLabel( new ImageIcon("BlackBishup.png") );
panels = (JPanel)chessBoard.getComponent(58);
panels.add(pieces);
pieces = new JLabel( new ImageIcon("BlackBishup.png") );
panels = (JPanel)chessBoard.getComponent(61);
panels.add(pieces);
pieces = new JLabel( new ImageIcon("BlackKing.png") );
panels = (JPanel)chessBoard.getComponent(59);
panels.add(pieces);
pieces = new JLabel( new ImageIcon("BlackQueen.png") );
panels = (JPanel)chessBoard.getComponent(60);
panels.add(pieces);
pieces = new JLabel( new ImageIcon("BlackRook.png") );
panels = (JPanel)chessBoard.getComponent(63);
panels.add(pieces);
}
}

感谢阅读,欢迎提供帮助

最佳答案

是的,您错误地添加了图片。

    new JLabel(new ImageIcon(getClass().getResource("/chessgame/PieceImages/WhitePawn.png"));

您应该为每个必须添加的部分使用该格式。它之所以这样工作是因为当您将程序打包到 JAR 中时,它必须能够从 JAR 内部读取。这允许我们从指示的包(“chessgame.PieceImages”)中获取类资源。

您可能还想考虑使用 NetBeans Swing Designer,这可能是处理您的这个国际象棋程序的更简单的方法。

关于java - 在 Java 程序中链接图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26834373/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com