如何正确刷新 JDialog 图像?

How can i refresh a JDialog Image Correctly?

我正在 java 中使用 SWing 编写程序,但这是我的问题,当我按下按钮时,我希望每次按下按钮时,我都会在相同位置更新新图像和前一个一样,我尝试在代码的动作侦听器中执行此操作,但是图像没有更新,而且是在开始时,有人可以帮助我吗?非常感谢。

public MainWindow() {
    initComponents();
    setIconImage(Icono);
    this.setLocationRelativeTo(null);
    this.setResizable(false);
    Imagen fondo=new Imagen();
    this.add(fondo, BorderLayout.CENTER);
    this.pack();
    PracticeMode = new javax.swing.JDialog();
}

private void StartPracticeActionPerformed(java.awt.event.ActionEvent evt) {                                              
    ButtonsSelected(1);
    StartGame Practice=new StartGame(OpcComboBox, numUnity, numTrys, 
    opcNotas, false);
    PracticeBF.dispose();
    PracticeMode.setIconImage(Icono);
    PracticeMode.setBounds(460, 600, 460, 538);
    PracticeMode.setVisible(true);
    CirculodeQuintasBW BW=new CirculodeQuintasBW();
    PracticeMode.add(BW, BorderLayout.CENTER);
    PracticeMode.pack();
    PracticeMode.setLocationRelativeTo(null);
    PracticeMode.setResizable(false);
}           

这是我要刷新的图片,它应该是之前的另一张图片,但每次我尝试刷新它都不起作用... PracticeMode 应该是JDialog,谁能帮帮我?

private void D2ActionPerformed(java.awt.event.ActionEvent evt) {                                   
    CirculodeQuintasD D=new CirculodeQuintasD();
    PracticeMode.add(D, BorderLayout.CENTER);
    PracticeMode.validate();
    PracticeMode.repaint();
    PracticeMode.pack();
}       

首先,变量名和方法名不应该以大写字符开头。通过阅读教科书或教程通过示例学习,然后遵循 Java 约定,不要自己编造!

when i press a button, I want that every time I press a button, I update a new image in the same position as the previous one,

将包含 ImageIcon 的 JLabel 添加到您的面板。

当您想更改您刚刚使用的图像时:

label.setIcon( new ImageIcon(...) );

例如,阅读 How to Use Combo Boxes 上的 Swing 教程部分。它完全符合您的要求。它使用 ActionListener 来更改标签的图像。唯一不同的是 ActionEvent 是通过单击组合框中的项目而不是单击按钮生成的。