Java 在netbeans的jTextField中设置文本
我知道如何在java中使用Java 在netbeans的jTextField中设置文本,java,jtextfield,Java,Jtextfield,我知道如何在java中使用JtextField设置文本。但出于某种奇怪的原因。我无法在netbeans中操作jtextfield。 我想要的是操作main()中的文本,用户将输入字符串,该字符串应显示在jTextField中 代码如下: import javax.swing.JOptionPane; /* * To change this template, choose Tools | Templates * and open the template in the editor. *
JtextField
设置文本。但出于某种奇怪的原因。我无法在netbeans中操作jtextfield
。
我想要的是操作main()
中的文本,用户将输入字符串,该字符串应显示在jTextField
中
代码如下:
import javax.swing.JOptionPane;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Sachin
*/
public class NewJFrame extends javax.swing.JFrame {
/**
* Creates new form NewJFrame
*/
public NewJFrame() {
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();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("jButton1");
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(134, 134, 134)
.addComponent(jButton1)
.addContainerGap(193, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(53, 53, 53)
.addComponent(jButton1)
.addContainerGap(224, 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(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
String inputValue = JOptionPane.showInputDialog("Please any enter any String");
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
// End of variables declaration
}
import javax.swing.JOptionPane;
/*
*要更改此模板,请选择工具|模板
*然后在编辑器中打开模板。
*/
/**
*
*@作者Sachin
*/
公共类NewJFrame扩展了javax.swing.JFrame{
/**
*创建新表单NewJFrame
*/
公共NewJFrame(){
初始化组件();
}
/**
*从构造函数中调用此方法来初始化表单。
*警告:不要修改此代码。此方法的内容始终为
*由表单编辑器重新生成。
*/
@抑制警告(“未选中”)
//
私有组件(){
jButton1=newjavax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setText(“jButton1”);
javax.swing.GroupLayout=newjavax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(布局);
layout.setHorizontalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(134、134、134)
.addComponent(jButton1)
.addContainerGap(193,简称最大值))
);
layout.setVerticalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(53,53,53)
.addComponent(jButton1)
.addContainerGap(224,简称最大值))
);
包装();
}//
/**
*@param指定命令行参数
*/
公共静态void main(字符串参数[]){
/*设置Nimbus的外观和感觉*/
//
/*如果Nimbus(在JavaSE6中引入)不可用,请使用默认的外观。
*详情请参阅http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
试一试{
for(javax.swing.UIManager.LookAndFeelInfo:javax.swing.UIManager.getInstalledLookAndFeels()){
if(“Nimbus”.equals(info.getName())){
setLookAndFeel(info.getClassName());
打破
}
}
}捕获(ClassNotFoundException ex){
getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}catch(实例化异常){
getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}捕获(非法访问例外){
getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}catch(javax.swing.UnsupportedLookAndFeelException ex){
getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}
//
String inputValue=JOptionPane.showInputDialog(“请输入任意字符串”);
/*创建并显示表单*/
invokeLater(new Runnable()){
公开募捐{
新建NewJFrame().setVisible(true);
}
});
}
//变量声明-不修改
私有javax.swing.JButton jButton1;
//变量结束声明
}
您可以执行以下操作,将inputValue
存储在NewJFrame对象中
private String input;
public NewJFrame(String input) {
this.input = input;
initComponents();
}
new NewJFrame(inputValue);
然后,在NetBeansIDE中,您可以在GUI编辑器中将JTextField的自定义创建代码设置为
新建JTextField(输入)
,或者只需调用setText。您正在谈论的文本字段的代码在哪里?