gpt4 book ai didi

java - 在 Swing 中关闭 Flash 屏幕

转载 作者:行者123 更新时间:2023-12-02 04:39:07 27 4
gpt4 key购买 nike

/* * 要更改此许可证 header ,请在项目属性中选择许可证 header 。 * 要更改此模板文件,请选择“工具”|“模板 * 并在编辑器中打开模板。 */

package newpackage;

import javax.swing.JProgressBar;

/**
*
* @author Pavilion
*/
public class Progress extends javax.swing.JFrame {

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

Progressloader pl=new Progressloader(jProgressBar2);

// try {
// pl.join();
// } catch (InterruptedException ex) {
// new Homescreen().setVisible(true);
// }
//System.exit(0);

dispose();

}

// public void shut(){
// dispose();
//}

/**
* 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();
jPanel2 = new javax.swing.JPanel();
jProgressBar2 = new javax.swing.JProgressBar();
jPanel3 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setUndecorated(true);
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/newpackage/above-progress-bar-image_1.png"))); // NOI18N
jLabel1.setText("jLabel1");

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 561, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(1, 1, 1))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 320, Short.MAX_VALUE)
);

getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, -1, 320));

jProgressBar2.setValue(50);

javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jProgressBar2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 560, Short.MAX_VALUE)
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jProgressBar2, javax.swing.GroupLayout.PREFERRED_SIZE, 5, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
);

getContentPane().add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 320, 560, 5));

jTextArea1.setEditable(false);
jTextArea1.setBackground(new java.awt.Color(51, 51, 51));
jTextArea1.setColumns(20);
jTextArea1.setFont(new java.awt.Font("Arial", 0, 8)); // NOI18N
jTextArea1.setForeground(new java.awt.Color(255, 255, 255));
jTextArea1.setLineWrap(true);
jTextArea1.setRows(3);
jTextArea1.setText("XOD SECURE is based on software from XOD Technologies Inc. which has been licensed under the Common Development and Distribution License(CDDL) and the GNU general public license number 2 by the Indian Government. This software has been solely made for the use of the Inadian Police as well as the CPRF. Any use of the software for any other purpose and improper distribution will be an illegal action and serious action will be taken against the person.");
jScrollPane1.setViewportView(jTextArea1);

javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 560, Short.MAX_VALUE)
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 4, Short.MAX_VALUE))
);

getContentPane().add(jPanel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 325, 560, 40));

pack();
setLocationRelativeTo(null);
}// </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(Progress.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Progress.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Progress.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Progress.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 Progress().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JProgressBar jProgressBar2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration
}

class Progressloader extends Thread{
JProgressBar jpb;

Progressloader(JProgressBar jpb){
this.jpb=jpb;
start();
}

@Override
public void run()
{

for(int i=jpb.getMinimum();i<=jpb.getMaximum();i++)
{
jpb.setValue(i);
try
{
Thread.sleep(20);
}catch(InterruptedException ex){}

}
new Homescreen().setVisible(true);
//new Progress().setVisible(false);

}
}

这段代码有什么问题?进度条运行正常,新窗口打开,但我无法处理闪屏

最佳答案

您一开始就违反了 Swing 的单线程规则,您应该使用 SwingWorker,它提供了可用于安全更新 UI 的方法。

参见Concurrency in SwingWorker Threads and SwingWorker了解更多详情。

例如:

您遇到的主要问题是您的 ProgressLoader 没有引用您的启动屏幕,因此无法关闭它。

SwingWorker提供了一个PropertyChangeListener,您可以使用它来监视worker的状态,当它DONE时,您可以使用它来关闭窗口

这里的技巧是从“闪屏”中创建 SwingWorker 并在将其作为匿名类启动之前添加一个 PropertyChangeListener ,这样您就可以完成后可以从 PropertyChangeListener 中调用 dispose

从概念上讲,如此处所示:Using SwingWorker to add a progress bar in a GUI

关于java - 在 Swing 中关闭 Flash 屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30393296/

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