0

我正在尝试显示类主教对象的 ImageIcon。使用 getImage() 检索 ImageIcon。返回的 ImageIcon 存储在引用 m 中,但它没有显示,另一个直接加载的 ImageIcon h 正在显示。我犯了什么错误?

import javax.swing.*;

//Game.java

public class Game {

    public static void main(String[] args) {
        board b = new board();
        bishop bis1 = new bishop();
        bis1.setLocation(0, 0);
        ImageIcon m = bis1.getImage();
        b.squares[0][1].add(new JLabel(m));
        ImageIcon h = new ImageIcon("rook.png");
        b.squares[0][0].add(new JLabel(h));
    }
}

//bishop.java
import javax.swing.*;
import java.awt.*;

public class bishop {
    private ImageIcon img;
    private int row;
    private int col;

    public void bishop() {
        img = new ImageIcon("bishop.png");
    }

    public void setLocation(int i, int j) {
        row = i;
        col = j;
    }

    public int getX() {
        return row;
    }

    public int getY() {
        return col;
    }

    public ImageIcon getImage() {
        return img;
    }
}

// board.java
import javax.swing.*;
import java.awt.*;

public class board {
public JFrame frame;
public JPanel squares[][] = new JPanel[3][3];

public board() {
frame = new JFrame("Simplified Chess");
frame.setSize(900, 400);
frame.setLayout(new GridLayout(2,3));

for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 3; j++) {
        squares[i][j] = new JPanel();

        if ((i + j) % 2 == 0) {
            squares[i][j].setBackground(Color.black);
        } else {
            squares[i][j].setBackground(Color.white);
        }   
        frame.add(squares[i][j]);
     }
   }

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
  }

}
4

4 回答 4

5

您以错误的方式定义了构造函数 - 使用了不必要的void. 因此,您的Bishop类调用默认的空构造函数,因此您的变量 img永远不会正确设置。删除它,以便正确调用您的构造函数:

而不是这个:

public void bishop() {
        img = new ImageIcon("bishop.png");
    }

无空定义它:

public bishop() {
            img = new ImageIcon("bishop.png");
        }
于 2012-03-08T14:34:32.737 回答
1

显示的代码不足以让我准确说出。我需要查看板类(顺便说一句:类名应在 Java 中大写:Board.java)

但我猜这与板类对板布局的方式有关。

你能只加载和显示主教吗?这将确定问题是否在于查找和加载主教。以下代码将执行此操作,这将有助于消除可能的原因:

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.getContentPane().add(new JLabel(new ImageIcon("bishop.png")));
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}
于 2012-03-08T14:28:53.520 回答
1

究竟什么是板?我假设它可能会扩展诸如 JFrame 之类的 Swing 组件?

所有与 GUI 相关的事件都应在事件调度程序线程 (EDT) 上发生。该线程负责更新 GUI。如果需要从另一个类更新 GUI,则需要使用SwingUtilities.invokeLater()

public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                board b = new board();
                bishop bis1 = new bishop();
                bis1.setLocation(0, 0);
                ImageIcon m = bis1.getImage();
                b.squares[0][1].add(new JLabel(m));
                ImageIcon h = new ImageIcon("rook.png");
                b.squares[0][0].add(new JLabel(h));
            }
        });
    }
于 2012-03-08T14:33:36.097 回答
0

最简单的解决方案:将图像上传到您的项目文件夹中。例如,您可以使用 JLabel 来输入图像。然后将您的代码编写为以下示例:

 JLabel lblNewLabel = new JLabel("New Label");
 lblNewLabel.setIcon(new ImageIcon("Name of your image"));
 panel.add(lblNewLabel);
于 2012-03-08T15:18:44.853 回答