0

クラス bishop のオブジェクトの ImageIcon を表示しようとしています。ImageIcon は getImage() を使用して取得されます。返された 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");
    }

void なしで定義します。

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 に答える