0
我對java很新,並試圖在NetBeans 7.4中創建一個表單,該表單將接收一個名爲「name」的變量並將其用作jTextField的顯示名稱。我已經嘗試了很多東西,並且儘可能地接近(不起作用,但我沒有錯誤)。請您查看代碼並幫助我瞭解我錯過了什麼。 類StatusScreen延伸javax.swing.JFrame中{我怎樣才能加載一個變量作爲jTextField顯示文本
public StatusScreen() { // Creates new form StatusScreen
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
PartName = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowOpened(java.awt.event.WindowEvent evt) {
formWindowOpened(evt);
}
});
PartName.setEditable(false);
PartName.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
PartName.setHorizontalAlignment(javax.swing.JTextField.CENTER);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(PartName, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(PartName, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(235, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void formWindowOpened(java.awt.event.WindowEvent evt) {
setExtendedState(JFrame.MAXIMIZED_BOTH);
}
//public static void main(String args[]) { //This is here for class testing.
public void showStatusScreen(final String name){ //This is program entry point.
/* 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(StatusScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(StatusScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(StatusScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(StatusScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
java.awt.EventQueue.invokeLater(new Runnable() { //To create and display the form
@Override
public void run() {
new StatusScreen().setVisible(true);
}
});
java.awt.EventQueue.invokeLater(new Runnable() { //Trying to initialize the PartName field with String name (not working)
@Override
public void run() { //Trying to initialize the PartName field with String name
PartName.setText(name); //Trying to initialize the PartName field with String name
} //Trying to initialize the PartName field with String name
}); }
// Variables declaration - do not modify
private javax.swing.JTextField PartName;
// End of variables declaration
}
圍棋,學習手工做到這一點。學習如何做到這一點需要花費很長時間。 – jzd