我以这种方式定义了 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
才能让我在单击 Change 时更改图像。有人有想法吗?