2016-11-30 62 views
0

当您在Web服务器运行时在Eclipse中对JSP页面进行更改并重新编译它时,更改会在下一页的浏览器中显示更新。我可以在运行时对swing组件属性进行更改

我可以在Java应用程序中执行与Swing组件类似的操作吗?假设我有一个JFrame,其中包含文本为"fname"的JLabel。我希望能够将其更改为"first name",并且正在运行的应用程序中的JLabel文本立即更改,而不必退出并重新启动应用程序。这在我尝试时不起作用。有没有办法做到这一点?

感谢您的帮助。

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package pack3; 


/** 
* 
* @author Love Poet 
*/ 
public class ChangeAtRuntime extends javax.swing.JFrame { 

    /** 
    * Creates new form ChangeAtRuntime 
    */ 
    public ChangeAtRuntime() { 
     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() { 

     jLabel1 = new javax.swing.JLabel(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N 
     jLabel1.setText("Name"); 

     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(151, 151, 151) 
       .addComponent(jLabel1) 
       .addContainerGap(204, Short.MAX_VALUE)) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addGap(116, 116, 116) 
       .addComponent(jLabel1) 
       .addContainerGap(162, Short.MAX_VALUE)) 
     ); 

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

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

你不能没有段落 –

+0

你是什么意思 –

+0

如何格式化你的问题,以便它可以被人轻松地阅读 –

回答

1

如果你在运行时调用setText()方法上的JLabel实例,应该更新显示的文本。只需从事件处理程序调用setText()即可触发标签上的更改。

编辑:现在你已经添加了代码,我建议你更新相关的部分来尝试这样的事情。它应该证明这个概念。

public void run() { 
    ChangeAtRuntime change = new ChangeAtRuntime(); 
    change.setVisible(true); 
    change.jLabel1.setText("New text..."); 
} 

此代码是否有意义?

编辑2:为整个班级添加代码。

public class ChangeAtRuntime extends javax.swing.JFrame { 
/** 
* Creates new form ChangeAtRuntime 
*/ 
public ChangeAtRuntime() { 
    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() { 

    jLabel1 = new javax.swing.JLabel(); 

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

    jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N 
    jLabel1.setText("Name"); 

    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(151, 151, 151) 
          .addComponent(jLabel1) 
          .addContainerGap(204, Short.MAX_VALUE)) 
    ); 
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addGroup(layout.createSequentialGroup() 
          .addGap(116, 116, 116) 
          .addComponent(jLabel1) 
          .addContainerGap(162, Short.MAX_VALUE)) 
    ); 

    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(ChangeAtRuntime.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (InstantiationException ex) { 
     java.util.logging.Logger.getLogger(ChangeAtRuntime.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (IllegalAccessException ex) { 
     java.util.logging.Logger.getLogger(ChangeAtRuntime.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
     java.util.logging.Logger.getLogger(ChangeAtRuntime.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() { 
      ChangeAtRuntime change = new ChangeAtRuntime(); 
      change.setVisible(true); 
      change.jLabel1.setText("New text..."); 
     } 
    }); 
} 

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

其实si io nevver在gui中使用编码部分我总是使用netbeans拖放,所以你可以帮我多一点 –

+0

你的代码目前的样子是什么? –

+0

好的我正在编辑我的帖子,代码为 –

相关问题