gpt4 book ai didi

java - 尝试从 jframe 制作屏幕保护程序,无法切换图像

转载 作者:行者123 更新时间:2023-11-30 08:18:48 25 4
gpt4 key购买 nike

我正在尝试使用 NetBeans GUI 构建器在 java.swing 中制作屏幕保护程序框架。当我设置在图像之间循环的无限循环时,图像将不会显示。通过 setIcon 将图像设置为显示在 jLabel 上。代码如下:

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;


public class AdFrame extends javax.swing.JFrame {

/**
* Creates new form FifthFrame
*/
public AdFrame() {
initComponents();
}

/**
* 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() {

jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(new java.awt.GridBagLayout());

jPanel1.setOpaque(false);

jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/btc_gui/newpackage/btc-zg.jpg"))); // NOI18N

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(30, 30, 30)
.addComponent(jLabel1)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(41, 41, 41)
.addComponent(jLabel1)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);

getContentPane().add(jPanel1, new java.awt.GridBagConstraints());

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

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* 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(AdFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(AdFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(AdFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(AdFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
AdFrame ff1 = new AdFrame();
ff1.setExtendedState(JFrame.MAXIMIZED_BOTH);
ff1.setVisible(true);
int index = 0;
String[] filearray = new String[2];
filearray[0] = "/btc_gui/newpackage/btc-zg.jpg";
filearray[1] = "/btc_gui/newpackage/pic2.jpeg";
while (true){
ff1.jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource(filearray[index])));
ff1.jLabel1.repaint();
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
Logger.getLogger(AdFrame.class.getName()).log(Level.SEVERE, null, ex);
}
index++;
if (index >= filearray.length) index = 0;
}
}

});
}

// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
// End of variables declaration
}

最佳答案

Could you please elaborate?

Thread.sleep(5000);

您有一个无限循环,并且您告诉 EDT 线程 hibernate 。由于 EDIT 负责绘制 GUI,因此永远无法完成绘制,因为 EDT 一直在 hibernate 。

不要使用无限循环,也不要使用Thread.sleep()。

而是使用 Swing Timer安排屏幕保护程序。

关于java - 尝试从 jframe 制作屏幕保护程序,无法切换图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29272984/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com