如何正确刷新JDialog图像?

时间:2018-04-29 01:19:23

标签: java swing jframe refresh jdialog

我在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);
}           

这是我想要刷新的图像,它之前是另一个图像,但每次我试图刷新它不起作用... 练习它是一个JDialog,任何人都可以帮助我吗?

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

1 个答案:

答案 0 :(得分:1)

首先,变量名和方法名不应以大写字母开头。通过阅读您的教科书或教程,然后遵循Java惯例而不是自己编写来学习!

  

当我按下按钮时,我希望每次按下按钮时,我都会在与前一个图像相同的位置更新新图像,

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

如果您想更改刚刚使用的图像:

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

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