2012-09-13 2 views
0

임 텍스트 파일에 텍스트 필드의 내용 쓰기 :는 내가 그것을 선언을 읽는되지 않는 이유를 알 수

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); 
      } 
     }); 
     } 
    } 
+0

txtid 외부를 선언? 예외가 있습니까? 문제에 대한보다 명확한 설명이 필요합니다. –

+0

오류는 여기에 output.write ("ID :"+ txtid.getNume() + "\ n"); txtid를 선언했지만 actionPerfomed 메서드에 오류가 있습니다. jana는 오류를 없애고 있지만 형식이 해석되지 않는다고 말하는 이유는 아닙니다. 어떤 생각? – user1646355

+0

ex.printStackTrace()를 사용하면 무엇을 의미합니까? 사실 난 그냥 Payroll() 메서드에서 선언했지만 actionPerformed()의 txtid가 오류를주는 이유를 알고 싶습니다. – user1646355

답변

1

문제는 txtid 로컬 사용자 Payroll 생성자의 변수가 아니라 구성원인지입니다 Payroll 클래스의 단순히 Payroll 클래스의 변수로 txtid를 선언하면 actionPerformed 메소드에서 액세스 할 수 있습니다.

+0

오 세상에! 내가 왜 그렇게 생각하지 않았 니? 이제 문제가 해결되었습니다. 고마워요! 하지만 1 가지 더 많은 문제는 텍스트 파일에 텍스트 필드 값을 쓰려면 어떻게해야합니까? getText() 메서드를 사용하면 오류가 발생하지만 getNume() 메서드를 사용하면 문제가 없습니다. 그러나 getNume()은 getText()가 전에 선언 한 txtid를 사용하는 동안 클래스 이름을 포인터로 사용해야합니다. 어떠한 제안? – user1646355

+0

@ user1646355'getNume()'이 무엇인지 모르겠습니다. 'getText()'는 텍스트 필드에 입력 된 값을 반환합니다. 파일에 쓰려면 닫힌 라이터에 쓸 수 없으므로 새 라이터를 다시 만드는 것을 잊지 마십시오. –

+0

새 질문을 새 질문으로 게시하십시오. 원래 문제가 해결되면 대답을 받아 들여야합니다. – basiljames

0

정확히 문제가 무엇 급여() 생성자

관련 문제