gpt4 book ai didi

java - 如何从 netbeans 的 main 方法设置文本字段文本?

转载 作者:行者123 更新时间:2023-12-01 14:40:52 25 4
gpt4 key购买 nike

我是 NetBeans 的新手(但不是 Java),我有一个问题。我使用 NetBeans 创建了一个 GUI,其中仅包含 JTextField和一个JButton

我想从 main 方法向文本字段添加文本,因此我将以下行添加到 main 方法的末尾(因此基本上在 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

}

最佳答案

因为在您尝试设置其文本时,jTextField1 变量尚未初始化。事实上,它仅在事件队列调用在 main 方法末尾传递给 invokeLater 的 runnable 时才初始化。

此外,您正在从主线程访问 Swing 组件,并且 Swing 组件只能在事件调度线程中使用。顺便说一句,这就是为什么 main 方法会在传递给 EventQueue.invokeLater() 的可运行对象内初始化框架。请阅读Swing tutorial about concurrency ,或者你的下一个问题会问为什么在事件监听器中 hibernate 会卡住整个 GUI。

这个字段不应该是公共(public)的,更不应该是静态的。为什么不从框架构造函数初始化字段的文本?这就是它应该初始化的地方。

关于java - 如何从 netbeans 的 main 方法设置文本字段文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15977909/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com