2013-08-03 59 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

所以我应该怎么解决呢?请指导。谢谢。 –