GUI가 여러 개 있고 NetBeans GUI Builder를 사용하고 있습니다. 다음 중 하나를 클릭하면 그림이 포함 된 다른 프레임을 여는 것이 좋습니다.Java GUI에서 이미지를 업데이트하는 방법
그래서 나는 청취자 (actionPerformed)와 버튼을 연관 시키며, 클릭하면 실제로 새 프레임을 게시합니다.
새 프레임에서 나는 JLabel을 왁스 칠 한 다음 레이블의 이미지를 연관시킵니다.
label.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/tree.png")));
내 문제는 그림이 프로그램의 실행 중에 덮어 여러 번 프레임에 아직 변경되지 않은 점이다 : 나는 그 넷빈즈이 코드를 생성하는 할 보았다. 즉, 새 프레임에는 항상 이미지의 이전 버전이 표시됩니다.
이미지를 항상 최신 상태로 유지하려면 어떻게해야합니까? 당신에게 대단히 감사합니다
강령 : 내가 지금처럼 아이콘의 생성과 JLabel의에의 setIcon 분리해서 것
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
}
기능을 시도하십시오. revalidate(); 그리고 repaint(); – schlagi123
@ schlagi123 답장을 보내 주셔서 감사 합니다만 작동하지 않습니다. 이미지는 항상 업데이트되지 않습니다 ... – user3602008
상기 프레임의 코드를 추가 할 수 있습니까? –