Swing 我可以知道为什么它没有阅读声明?;

Swing 我可以知道为什么它没有阅读声明?;,swing,Swing,Im将文本字段的内容写入文本文件: import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.SwingUtilities; import java.io.FileNotFoundException; import java.util.Formatter; import javax.swing.*; import j

Im将文本字段的内容写入文本文件:

    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.SwingUtilities;
    import java.io.FileNotFoundException;
    import java.util.Formatter;
    import javax.swing.*;
    import java.util.*;
    import java.io.*;

    public class Payroll extends JFrame implements ActionListener{

    private JButton btn1;
    private JButton btn2;
    private JButton btnadd;


    // initialize the lbl with caption name is employee information.
     JLabel lbl = new JLabel("Nilai University Payroll System");

    public Payroll(){
     super("Nilai University Payroll System");
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

     setLayout(null);

     JLabel lblid,lblname,lblrank,lblclass,lbltype,lblpay;
     JTextField txtid,txtname,txtrank,txtclass,txtEmployeeClass,txtEmployeeType;
     JRadioButton rb1,rb2,rb3,rb4,rb5;

     lbl.setBounds(200,50,500,100);
     lbl.setHorizontalAlignment(lbl.CENTER );

     lblid = new JLabel("Employee ID: ");
     lblname = new JLabel("Employee Name: ");
     lblrank = new JLabel("Employee Rank: ");
     lblclass = new JLabel("Employee Class: ");
     lbltype = new JLabel("Class Type: ");
     lblpay = new JLabel("Employee Pay Rate: ");

  // initialize all the label which are declared in the example above with its caption name 
     lblid.setBounds(300,140,100,20);
     lblname.setBounds(300,180,100,20);
     lblrank.setBounds(300,220,100,20);
     lblclass.setBounds(300,260,100,20);
     lbltype.setBounds(300,300,100,20);
     lblpay.setBounds(300,340,100,20);

     // add all the label on the frame
     add(lblid);
     add(lblname);
     add(lblrank);
     add(lblclass);
     add(lbltype);
     add(lblpay); 

  // initialize the text field with size
     txtid=new JTextField(15);
     txtname=new JTextField(15); 
     txtclass=new JTextField(15);
     txtEmployeeClass=new JTextField(15);
     txtEmployeeType=new JTextField(15);

     // set a particular position on a screen with set bounds constructor
     txtid.setBounds(400,140,100,20);
     txtname.setBounds(400,180,100,20);
     txtclass.setBounds(400,220,100,20);
     txtEmployeeClass.setBounds(400,260,100,20);
     txtEmployeeType.setBounds(400,300,100,20);

  // add text field on a Frame
     add(txtid);
     add(txtname);
     add(txtclass);
     add(txtEmployeeClass);
     add(txtEmployeeClass);

  // initialize radio button with its caption
     rb1 = new JRadioButton("1. Excellent");
     rb2 = new JRadioButton("2. Good");
     rb3 = new JRadioButton("3. Average");
     rb4 = new JRadioButton("4. Fair");
     rb5 = new JRadioButton("5. Poor");

     // set a particular position on a Frame
     rb1.setBounds(400,220,100,20);
     rb2.setBounds(500,220,100,20);
     rb3.setBounds(600,220,100,20);
     rb4.setBounds(700,220,100,20);
     rb5.setBounds(800,220,100,20);

     // add button on a frame
     add(rb1);
     add(rb2);
     add(rb3);
     add(rb4);
     add(rb5);

     btnadd = new JButton("Add Employee");
     btnadd.setToolTipText("Click this button to add employee details to a text file.");
     btnadd.setBounds(400,320,150,20);
     add(btnadd);
     btnadd.addActionListener(this);
     btnadd.setActionCommand("Add");
    }
    private BufferedWriter output;
     public void actionPerformed(ActionEvent e){

         String cmd = e.getActionCommand();
         if(cmd.equals("Add"))
         {
            output.write("ID: "+txtid.getNume()+"\n");
         }   
     }

     public void WriteFile(){
            try {
        output = new BufferedWriter(new FileWriter("E:/EC3307/eclipse/Payroll.txt",true));        
                output.newLine();
                output.close();
            }
            catch(IOException e)
            {
                System.out.println("There was a problem:" + e);

            }
        }

  public static void main(String args[]){

      SwingUtilities.invokeLater(new Runnable(){
          @Override
          public void run()
          {
              Payroll f1=new Payroll();
              // set frame size
              f1.setSize(1000,600);
              // set frame visible true
              f1.setVisible(true);  
          }
      });
      }
     }   

问题在于
txtid
Payroll
构造函数的局部变量,而不是
Payroll
类的成员。只需将txtid声明为
Payroll
类的变量,这将允许在
actionPerformed
方法中访问它。

在Payroll()构造函数之外声明
txtid
到底是什么问题?你有例外吗?需要对该问题进行更多说明。错误在此处输出。写入(“ID:+txtid.getNume()+”\n”);我已经声明了txtid,但是actionPerfomed方法中有错误,我jst wana消除了错误,但是我不知道为什么它说类型没有被解析。你知道吗?你说的ex.printStackTrace()是什么意思?实际上我只是想知道为什么actionPerformed()中的txtid会给我错误,尽管我已经在Payroll()方法中声明了哦,天哪!为什么我没想到呢?好了,现在它解决了问题。谢谢大家!但还有一个问题,如何将文本字段值写入文本文件?如果我使用getText()方法,它会有错误,但是使用getNume()是可以的。但是getNume()需要一个类名作为指针,而getText()只是使用我之前声明的txtid。有什么建议吗?@user1646355我不知道什么是
getNume()
getText()
将返回在文本字段中输入的值。要写入文件,请不要忘记重新创建一个新的编写器,因为您无法写入已关闭的编写器。请将您的新问题作为新问题发布。你应该接受这个答案,因为它解决了你原来的问题。