我正在用netbeans摇摆创建一个GUI.在设计GUI中,netbeans在程序末尾实例化了所有无法操作的变量.现在,我想获取Jtextfield数据并将其放在String对象中.假设netbeans将我的JTextField变量创建为jTextField1,如果我这样做了
开头的字符串名称= jTextField1.getText()
它显示了非法前向引用的错误,如果我尝试在程序结尾(在所有netbeans声明之后)定义此错误,则会引发数百万个异常.
那么,我该怎么做才能声明此字符串对象并在变量中检索gettext()的值?
编辑:
//编码
import javax.swing.JOptionPane;
public class LoginInternalFrame extends javax.swing.JInternalFrame
{
public LoginInternalFrame()
{
initComponents();
}
public int check()
{
if(jTextField1.getText().equals(""))
{
return 0;
}
if(jPasswordField1.getText().equals(""))
{
return 0;
}
return 1;
}
//HERE I AM TRYING TO DECLARE ********************
String user = jTextField1.getText();
//THIS IS SHOWING ILLEGAL FORWARD REFERENCE BECAUSE jTextField IS DECLARED AT THE END OF THE CODE WHICH IS UNMODIFIABLE IN THE IDE
//HERE STARTS NETBEANS' GENERATED CODE
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jPasswordField1 = new javax.swing.JPasswordField();
jButton1 = new javax.swing.JButton();
setClosable(true);
setIconifiable(true);
setMaximizable(true);
setResizable(true);
jLabel1.setFont(new java.awt.Font("Tekton Pro", 0, 18)); // NOI18N
jLabel1.setText("Username");
jLabel2.setFont(new java.awt.Font("Tekton Pro Ext", 0, 18)); // NOI18N
jLabel2.setText("Password");
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
jButton1.setFont(new java.awt.Font("Tarzan", 1, 18)); // NOI18N
jButton1.setText("Login");
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(213, 213, 213)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(127, 127, 127)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(0, 245, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 202, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(351, 351, 351))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(93, 93, 93)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(49, 49, 49)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(84, 84, 84)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(145, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
//NETBEANS' GENERATED CODE ENDS.
//THE ACTIOINPERFORMED METHODS
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if(check()==0)
{
JOptionPane.showMessageDialog(this, "All Fields are Required...","Error",JOptionPane.ERROR_MESSAGE);
return;
}
WelcomeUser ww=new WelcomeUser();
ww.setVisible(true);
this.setVisible(false);
}
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPasswordField jPasswordField1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
//THESE ARE THE UNMODIFIABLE DECLARATIONS IN THE END.NOW WHEN I TRY DECLARE STRING HERE ,THE IDE SHOWS A LOT OF EXCEPTIONS.
}
而且我无法在字符串变量中获取文本字段的文本.
解决方法:
我的猜测是,您在代码的初始化部分,任何构造函数或方法之外,因此在声明jtf JTextField之前声明了名称String.如果是这样,解决方案是,是的,请在该区域中声明名称,但不要尝试使用JTextField的文本对其进行初始化.
编辑:是的,我是对的-您在任何构造函数或方法之外声明了用户变量,这很好,但是您还尝试在其中使用JTextField jTextField1的文本对其进行初始化,但这并没有没有道理.文本字段变量不仅尚未声明,而且即使已声明,也尚未构造.而且即使它被构造,它也不会保存任何有用的文本,因为用户还没有机会用文本填充它.
解决方案:使用空字符串“”初始化您的String,然后在某种事件中(例如,用户在用文本填充文本字段后调用的ActionListener),为您的名称变量提供JTextField保留的文本.
编辑2:感谢您改善您的问题!
编辑3
您发表评论:
But there is still one conceptual problem that say when I press “enter” button on my gui page(without writing anything in the text field),the check function gets invoked(in my program)and there in the if statement getText works?(So there is STILL nothing written in the textfield and the jTextField1 has also not been declared yet!)
实际上,是的,它已经被声明,并且现在其中可能包含文本(如果用户在输入文本后按Enter键).除非您处于同一作用域级别,否则在代码中的地理位置*问变量并不重要,而是在逻辑上进行访问.由于您是从非静态方法访问它的,并且您的JTextField变量是在类的声明部分中声明的,因此编译器知道它已被声明.
编辑4
一个关键概念是了解对象可以做什么和不能做什么,以及字符串是不可变的-不能更改.请注意,如果您的初始声明被允许,它将仍然无济于事,因为当JTextField的文本更改时,您的name变量将不会.更改它的唯一方法是在发生事件时在需要时分配一个新的String(可能是JTextField中的新String). Java没有魔术.
另外,关于check()方法,由于它返回二进制结果,所以为什么不让它返回布尔值.另外,您不应在JPasswordField上调用getText(),因为它会暴露密码文本,从而大大降低程序的安全性.我会做类似的事情:
//!! make this boolean
public boolean check() {
if (jTextField1.getText().trim().isEmpty()) {
return false;
}
if (jPasswordField1.getPassword().length == 0) {
return false;
}
return true;
}
然后这样称呼它:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if (check()) {
// ....