0

JPanel を拡張してゲーム ボードを表示し、下部に JEditorPane を追加してステータス テキストを保持します。残念ながら、ゲーム ボードは問題なく表示されますが、JEditorPane は、その中のテキストを強調表示するまで、空白の灰色の領域にすぎません。super.paintComponent(g) は他の子 (つまり、JEditorPane) をレンダリングする必要があるため、Swing を正しく理解していれば機能するはずです。教えてください、すばらしいスタックオーバーフローよ、私が犯している骨の折れる間違いは何ですか?

public GameMap extends JPanel {
  public GameMap() {
    JEditorPane statusLines = new JEditorPane("text/plain","Stuff");
    this.setLayout(new BoxLayout(this,BoxLayout.PAGE_AXIS));
    this.add(new Box.Filler(/*enough room to draw my game board*/));
    this.add(statusLines);
  }
  public void paintComponent(Graphics g){
    super.paintComponent(g);
    for ( all rows ){
      for (all columns){
        //paint one tile
      }
    }
  }
}
4

1 に答える 1

2

一般的に、あなたのコードについてすぐに骨抜きにされたものは見当たりませんが、コンポーネントの階層は少し骨抜きに見えます。

オブジェクトをより適切に分離していない理由はありますか? コードを維持し、テストしやすくするために、GameBoardロジックを別のクラスに抽出することをお勧めします。GameMapこれにより、を削除して単純化することができますpaintComponent(...)

public class GameMap extends JPanel{
  private JEditorPane status;
  private GameBoard board;
  public GameMap() {
    status= createStatusTextPane();
    board = new GameBoard();
    this.setLayout(new BoxLayout(this,BoxLayout.PAGE_AXIS));
    this.add(board);
    this.add(status);
  }
  //...all of the other stuff in the class
  // note that you don't have to do anything special for painting in this class
}

そして、あなたは次のGameBoardように見えるかもしれません

public class GameBoard extends JPanel {
  //...all of the other stuff in the class
  public void paintComponent(Graphics g) {
    for (int row = 0; row < numrows; row++)
      for (int column = 0; column < numcolumns ; column ++)
        paintCell(g, row, column);
  }
}
于 2010-05-02T06:38:36.693 に答える