2

私はこのように2つのクラスを定義しています:

public class Cartes extends JPanel
{
  private BufferedImage image;
  protected int tabC[] = new int[9];
  public int randomC ;

  public Cartes ()
  {

    ..........

    BufferedImage myPicture = null;

    try {
      myPicture = ImageIO.read(new File("images/"+randomC+".png"));
    } 
    catch (IOException e)
    {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    JLabel picLabel = new JLabel(new ImageIcon( myPicture ));
    add( picLabel );
  }

  public void paintComponent(Graphics g)
  {
    super.paintComponent(g);
    g.drawImage(image, 0, 0, null); //
  }
}

注:randomCはコンストラクターで生成された整数であり、画像をランダムに選択できます。

public class VueGeo extends JFrame
{
  public Cartes pan = new Cartes();
  private JButton bouton = new JButton("Change");

  public VueGeo()
  {

    ...

    container.add(pan, BorderLayout.CENTER);

    bouton.addActionListener(new BoutonListener ());

    ...

    this.setContentPane(container);

    this.setVisible(true);

  }

  class BoutonListener implements ActionListener
  {
    public void actionPerformed(ActionEvent arg0) {
      ????????
    }
  }
} 

actionPerformed問題は、[変更]をクリックするたびに画像を変更できるようにするために何を入力すればよいかわからないことです。誰かがアイデアを持っていますか?

4

1 に答える 1

3

Cartesでセッターメソッドを作成します。

public void setImage(BufferedImage i)
{ image = i; }

次に、actionPerformedで、

cartes.setImage( (whatever image you would like) );
cartes.repaint();
于 2012-11-27T22:20:13.200 に答える