2013-08-03 93 views
0

我是Java新手,對此不太瞭解。我創建了一個接受用戶輸入的java代碼。我在我的程序中創建了一個Submit按鈕。我希望程序應該將用戶輸入存儲在硬盤驅動器中的.txt文件中。下面是代碼:在.txt文件中輸入用戶輸入

import javax.swing.*; 
import java.awt.BorderLayout; 
import java.io.*; 
import java.lang.*; 

public class myfirstapp extends JFrame { 


public JButton submit; 
public JTextField field1; 
public JTextField field2; 
public JTextField field3; 
public JLabel label; 
public JPasswordField passwordfield; 

public void myfirstapp(){ 

    field1 = new JTextField("Enter your Email Id:"); 
    field1.setEditable(false); 
    add(field1); 

    field2 = new JTextField(20); 
    add(field2); 

    field3 = new JTextField("Enter your password below:"); 
    field3.setEditable(false); 
    add(field3); 

    label = new JLabel("Exclusive production of PCIT"); 
    add(label,BorderLayout.SOUTH); 

    passwordfield = new JPasswordField(20); 
    add(passwordfield); 

    submit = new JButton("Get Likes!"); 
    submit.addActionListener(
      new ActionListener(){ 
       private void actionPerformed(ActionEvent event){ 
        public Formatter x; 
        private void openFile(){ 

         try{ 
         x = new Formatter("D:\\gta.txt"); 
        } 
        catch(Exception e){ 
         System.out.println("You got an error"); 
        } 


       } 

       public void addRecords(){ 
        x.submit(); 
       } 
       public void closeFile(){ 
        x.close(); 
        } 
       } 

      ); 
    add(submit); 


}} 

我在這一行收到錯誤:

private void actionPerformed(ActionEvent event) 

錯誤說:語法錯誤上令牌(S),錯位構造函數(S)。 我該怎麼辦?我不知道該如何處理這種情況。善意幫助我。 謝謝。

回答

1
  • 你的方法

  • 您需要實現的ActionListeneractionPerformed法中有法,在實施不能降低方法的可見性。讓它public actionPerformed

正確的做法

submit.addActionListener(
      new ActionListener(){ 
       //x should be a field since its accessed within other methods 
       public Formatter x; 

       //this method should be public 
       public void actionPerformed(ActionEvent event){ 

       } 

       //open file should be a different method and remove it from actionPerformed 
       private void openFile(){ 
        try{ 
         x = new Formatter("D:\\gta.txt"); 
        } 
        catch(Exception e){ 
         System.out.println("You got an error"); 
        } 


       } 

       public void addRecords(){ 
        x.submit(); 
       } 
       public void closeFile(){ 
        x.close(); 
       } 
      } 

      ); 
+0

所以我應該怎麼解決呢?請指導。謝謝。 –