如何在Java GUI中更新映像

时间:2014-07-09 19:24:54

标签: java image swing user-interface

我有一个带有几个按钮的GUI,我正在使用NetBeans GUI Builder。 点击其中一个,我希望它打开另一个包含图片的框架。

所以我将一个监听器(actionPerformed)与该按钮相关联,当单击它时,它会打开实际发布的新帧。

在新框架中,我打了一个JLabel然后我关联了标签的图像。我看到了这样做,NetBeans生成了这段代码:

label.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/tree.png")));

我的问题是在执行程序期间图片被多次覆盖,但框架中还没有改变。 也就是说,在新帧中始终显示旧版本的图像。

如何才能使图像始终保持最新状态? 非常感谢你


守则:

package View;

import Controller.Util;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

public class AlberoIpotesi extends javax.swing.JFrame {

   /** Creates new form AlberoIpotesi */
   public AlberoIpotesi() {
      initComponents();

      remove(label);
      revalidate(); 
      repaint();
      Decifra.sessioneDec.toString(".../src/img/tree");
      revalidate(); 
      repaint();
   }

   /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
   @SuppressWarnings("unchecked")
   // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
   private void initComponents() {

      jLabel1 = new javax.swing.JLabel();
      scroll = new javax.swing.JScrollPane();
      label = new javax.swing.JLabel();

      setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
      setTitle("Albero delle ipotesi");

      jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
      jLabel1.setText("Albero delle ipotesi");

      label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
      label.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Img/tree.png"))); // NOI18N
      label.setVerticalAlignment(javax.swing.SwingConstants.TOP);
      scroll.setViewportView(label);

      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
      getContentPane().setLayout(layout);
      layout.setHorizontalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(layout.createSequentialGroup()
            .addGap(29, 29, 29)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addComponent(scroll, javax.swing.GroupLayout.PREFERRED_SIZE, 342, javax.swing.GroupLayout.PREFERRED_SIZE)
               .addComponent(jLabel1))
            .addContainerGap(29, Short.MAX_VALUE))
      );
      layout.setVerticalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jLabel1)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(scroll, javax.swing.GroupLayout.PREFERRED_SIZE, 374, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(25, Short.MAX_VALUE))
      );

      pack();
   }// </editor-fold>                        

   /**
    * @param args the command line arguments
    */
   public static void start() {
      /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
       * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
       */
      try {
         for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
               javax.swing.UIManager.setLookAndFeel(info.getClassName());
               break;
            }
         }
      } catch (ClassNotFoundException ex) {
         java.util.logging.Logger.getLogger(AlberoIpotesi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
      } catch (InstantiationException ex) {
         java.util.logging.Logger.getLogger(AlberoIpotesi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
      } catch (IllegalAccessException ex) {
         java.util.logging.Logger.getLogger(AlberoIpotesi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
      } catch (javax.swing.UnsupportedLookAndFeelException ex) {
         java.util.logging.Logger.getLogger(AlberoIpotesi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
      }
        //</editor-fold>

      /* Create and display the form */
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            new AlberoIpotesi().setVisible(true);
         }
      });
   }

   // Variables declaration - do not modify                     
   private javax.swing.JLabel jLabel1;
   public static javax.swing.JLabel label;
   public static javax.swing.JScrollPane scroll;
   // End of variables declaration                   
}

2 个答案:

答案 0 :(得分:2)

我将在JLabel上创建图标和setIcon,如下所示:

ImageIcon icon = new ImageIcon("path/to/picture.png");
label.setIcon(icon);

这样您可以稍后更改图标的图像并更新标签

icon = new ImageIcon("path/to/new_picture.png"); // Changes the icon
label.setIcon(icon); // Updates the label's icon

答案 1 :(得分:0)

基本上我理解的是你有一个按钮可以创建你希望显示的框架的新实例对象吗?然后你有一个图像存储在项目的特定位置,在那里你会想起对象,以显示你所说的更新图像?

我不知道你如何选择启动你的框架。但我可以建议你使用参数化构造函数来每次(如果需要)调用帧时更改图像。

https://www.dropbox.com/s/mpqvy9hgsvsgq9t/PeanutsAndPretzels.zip

我不确定stackoverflow和外部项目文件的条款和条件。但是我编写了可能会或可能不会帮助你的东西。给它一个bash来得到一个想法。