我是NetBeans(但不是Java)的新手,我遇到問題。我創建了一個NetBeans GUI,其中只包含JTextField和JButton。如何從netbeans中的main方法設置文本字段文本?
我想從主要方法向文本字段添加文本,所以我在主方法的末尾添加了以下行(所以基本上在主方法中生成的代碼創建了JFrame,只有在這之後額外的行):jTextField1.setText("WHATEVER");
沒有任何反應。我已將文本框更改爲public static
,但仍然沒有任何結果。
但是,如果我在按鈕的actionPerformed
方法內使用相同的行,它的工作原理。
爲什麼?爲什麼我無法設置主課程中的文本?
下面是代碼:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pac.jframe_test;
/**
*
* @author Orand
*/
public class JFrame_Test_UI extends javax.swing.JFrame {
/**
* Creates new form JFrame_Test_UI
*/
public JFrame_Test_UI() {
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() {
jButton1 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
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()
.addGap(154, 154, 154)
.addComponent(jButton1)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(112, Short.MAX_VALUE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 185, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(103, 103, 103))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(109, Short.MAX_VALUE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(96, 96, 96)
.addComponent(jButton1)
.addGap(52, 52, 52))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText("whatever");
}
/**
* @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(JFrame_Test_UI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(JFrame_Test_UI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(JFrame_Test_UI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(JFrame_Test_UI.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 JFrame_Test_UI().setVisible(true);
}
});
jTextField1.setText("WHATEVER");
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
public static javax.swing.JTextField jTextField1;
// End of variables declaration
}
的問題是,我不能從構造函數初始化。問題實際上比這更復雜,因爲我已經創建了這個程序只是作爲一個測試,找到答案爲什麼我的主要項目不工作。 – dwarfhero
然後在框架中添加一個setText()方法,該方法在文本字段中設置文本,並使用類似於主方法末尾的代碼的代碼(EventQueue.invokeLater())從事件派發線程調用它。 '或'SwingUtilities.invokeLater()') –
我的主項目將是串行端口的終端。我有那裏應該列出可用端口的組合框。還有一個類,其中一個方法首先對端口進行計數,然後創建一個數組並將端口名稱放在那裏。然後該數組由組合框讀取,因此它將被填充。由於我不知道端口和端口名稱的數量,我無法初始化組合框。所以這就是我嘗試使用主要方法的全部原因。我不知道如何解決這個問題。因爲要運行組合框的第一件事是從其他類的方法 – dwarfhero