Java 从其他类使用时,Swing窗口为空
我对Swing还不熟悉,从另一个类启动窗口时遇到问题。如果我在同一个类中创建窗口的新对象,它会显示得很好,但当我尝试从另一个类启动它时,它会显示为空 这是我尝试过的,但不起作用:Java 从其他类使用时,Swing窗口为空,java,swing,Java,Swing,我对Swing还不熟悉,从另一个类启动窗口时遇到问题。如果我在同一个类中创建窗口的新对象,它会显示得很好,但当我尝试从另一个类启动它时,它会显示为空 这是我尝试过的,但不起作用: ProgressWindow dow = new ProgressWindow(this, null); 下面是我的Swing窗口的代码: package example; import java.awt.GridLayout; import javax.swing.JFrame; import javax.swi
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
}
包示例;
公共类Otherclass扩展了javax.swing.JFrame{
公共类(){
初始化组件();
}
@抑制警告(“未选中”)
//
私有组件(){
jButton1=newjavax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setText(“jButton1”);
jButton1.addActionListener(新java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout=newjavax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(布局);
layout.setHorizontalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButton1)
.addContainerGap(317,简称最大值))
);
layout.setVerticalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButton1)
.addContainerGap(266,简称最大值))
);
包装();
}//
私有void jButton1ActionPerformed(java.awt.event.ActionEvent evt){
//TODO在此处添加您的处理代码:
试一试{
ProgressWindow pr=新的ProgressWindow(此为空);
《睡眠》(2000年);
pr.dispose();
}捕获(中断异常例外){
系统错误打印项次(ex);
}
}
公共静态void main(字符串参数[]){
/*设置Nimbus的外观和感觉*/
//
/*如果Nimbus(在JavaSE6中引入)不可用,请使用默认的外观。
*详情请参阅http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
试一试{
for(javax.swing.UIManager.LookAndFeelInfo:javax.swing.UIManager.getInstalledLookAndFeels()){
if(“Nimbus”.equals(info.getName())){
setLookAndFeel(info.getClassName());
打破
}
}
}捕获(ClassNotFoundException ex){
getLogger(Otherclass.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}catch(实例化异常){
getLogger(Otherclass.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}捕获(非法访问例外){
getLogger(Otherclass.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}catch(javax.swing.UnsupportedLookAndFeelException ex){
getLogger(Otherclass.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}
//
/*创建并显示表单*/
invokeLater(new Runnable()){
公开募捐{
新建Otherclass().setVisible(true);
}
});
}
//变量声明-不修改
私有javax.swing.JButton jButton1;
//变量结束声明
}
您应该使用传递给构造函数的框架
将构造函数中的这些调用更改为:
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);
在Swing应用程序中使用而不是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();
你能提供一个例子来演示不起作用的代码吗?你有什么例外可以与我们分享吗?你能提供第二个类,你称之为ProgressWindow吗?我编辑了这篇文章并添加了一个例子。没有堆栈跟踪。