0

我是 Java 新手,对它了解不多。我创建了一个接受用户输入的 java 代码。我在我的程序中创建了一个提交按钮。我希望程序应该将用户输入存储在我硬盘中的 .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)

错误说:令牌上的语法错误,错误的构造函数。我应该怎么办?我不知道如何处理这种情况。请帮助我。谢谢你。

4

1 回答 1

1
  • 您在方法中有方法

  • 您需要实现 的actionPerformed方法ActionListener,并且在实现时不能降低该方法的可见性。做了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();
                }
            }

            );
于 2013-08-03T07:50:15.180 回答