我是Swing的新手,从另一个类开始一个窗口时遇到问题。如果我在同一个类中创建一个窗口的新对象,它就会很好地显示,但是当我尝试从另一个类启动它时它会显示为空。
这是我尝试过的,但它不起作用:
ProgressWindow dow = new ProgressWindow(this, null);
这里是我的Swing窗口的代码:
package example;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class ProgressWindow extends javax.swing.JFrame {
private final JPanel panel;
private final javax.swing.JLabel jLabel1;
private final javax.swing.JLabel jLabel2;
private String message = "Vorgang läuft...";
private final int maxLength = 30;
public ProgressWindow(JFrame frame, String pMessage) {
super("Bitte warten");
//Set Message
setMessage(pMessage);
//Labels und panel anlegen
panel = new JPanel(new GridLayout(2, 1));
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
//Position
this.setLocationRelativeTo(frame);
//Close-Operation
setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
//Size
setMaximumSize(new java.awt.Dimension(240, 90));
setMinimumSize(new java.awt.Dimension(240, 90));
setResizable(false);
//Content
jLabel1.setText(message);
jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/xyz/ajax-loader.gif")));
//Alignment
jLabel1.setHorizontalAlignment(JLabel.CENTER);
jLabel2.setHorizontalAlignment(JLabel.CENTER);
//Finish Layout
panel.add(jLabel1);
panel.add(jLabel2);
getContentPane().add(panel);
pack();
setVisible(true);
}
private void setMessage(String pMessage) {
if (pMessage != null) {
//Cut string if too long
if (pMessage.length() > maxLength) {
pMessage = pMessage.substring(0, maxLength) + "...";
}
this.message = pMessage;
}
}
//This works as expected:
public static void main(String[] args) {
try {
ProgressWindow pr = new ProgressWindow(null, null);
Thread.sleep(2000);
pr.dispose();
}
catch (InterruptedException ex) {
}
}
}
这是我用Netbeans GUI-Builder创建的一个快速而又脏的示例类,用于演示不起作用的代码:
package example;
public class Otherclass extends javax.swing.JFrame {
public Otherclass() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButton1)
.addContainerGap(317, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButton1)
.addContainerGap(266, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try {
ProgressWindow pr = new ProgressWindow(this, null);
Thread.sleep(2000);
pr.dispose();
} catch (InterruptedException ex) {
System.err.println(ex);
}
}
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(Otherclass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Otherclass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Otherclass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Otherclass.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 Otherclass().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
// End of variables declaration
}
答案 0 :(得分:2)
在Swing应用程序中使用Swing Timer代替Thread.sleep(2000)
。
示例代码:
final ProgressWindow pr = new ProgressWindow(null, null);
Timer timer=new Timer(2000,new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
pr.dispose();
}
});
timer.setRepeats(false);
timer.start();
答案 1 :(得分:0)
您应该使用传递给构造函数的框架。
将构造函数中的调用更改为:
frame.setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
//Size
frame.setMaximumSize(new java.awt.Dimension(240, 90));
frame.setMinimumSize(new java.awt.Dimension(240, 90));
frame.setResizable(false);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);