我是java的新手,并尝试在NetBeans 7.4中创建一个表单,该表单将采用名为“name”的变量并将其用作jTextField的显示名称。我尝试了很多东西,这是我能得到的尽可能接近(不起作用,但我没有错误)。您能否查看代码并帮助我了解我所缺少的内容。 class StatusScreen扩展了javax.swing.JFrame {
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
}
答案 0 :(得分:0)
要解决的问题:
PartName
是StatusScreen
类中的字段。因此,您需要创建StatusScreen
的实例并使用一个将调用PartName.setText(name)
的setter方法。如果没有它的实例,则无法调用另一个类的字段。主要方法包括以下内容:
StatusScreen ss = new StatusScreen();
ss.setVisible(true);
ss.setPartName(name);
setPartName
方法类似于:
public void setPartName(String name){
PartName.setText(name);
}