什么';我的对话框有问题,我应该如何在不同的类和单独的.java文件中更改arraylist?

什么';我的对话框有问题,我应该如何在不同的类和单独的.java文件中更改arraylist?,java,swing,user-interface,arraylist,dialog,Java,Swing,User Interface,Arraylist,Dialog,我正在尝试编写一个GUI程序,该程序将导入一个文本文件,读取其内容并将其放入ArrayList 我在一个单独的.java文件中编写面板,该文件包含导入按钮并执行导入功能,而Arraylist位于不同的.java文件(main方法文件)上 如何使ActionListener从不同的class/.java文件读取文件内容并将其导入ArrayList 我的导入对话框alos导致错误,有什么问题吗?该代码在CLI中工作正常,但在GUI程序中工作不正常 这是GUI框架的主要方法,它将从不同的.java文件

我正在尝试编写一个GUI程序,该程序将导入一个文本文件,读取其内容并将其放入ArrayList

我在一个单独的.java文件中编写面板,该文件包含导入按钮并执行导入功能,而Arraylist位于不同的.java文件(main方法文件)上

如何使ActionListener从不同的class/.java文件读取文件内容并将其导入ArrayList

我的导入对话框alos导致错误,有什么问题吗?该代码在CLI中工作正常,但在GUI程序中工作不正常

这是GUI框架的主要方法,它将从不同的.java文件添加面板:

public class FrameGUI 
{

    public static void main(String[] args) throws Exception
    {

        //Creates ArrayList and initiating JFrame
        ArrayList<String> stringFile = new ArrayList<String>();
        JFrame frame = new JFrame("Title");


        *//Rest of the following codes here*        


    }  //---End of Main Method---


} ---End of the First .java file
public class ImportPanel extends JPanel
{

    private JButton importButton;

    public ImportPanel(ArrayList<String> StringFile)
    {

    //Setting up JButton and link it to inner class of
    //ActionListener
    importButton = new JButton("Import Data");
    importButton.addActionListener(new importListener());


    *//***Adding the button into panel, set background color and panel size*

}  //---End of main method---


    //Private inner class:
    private class importListener implements ActionListener
    {

        public void actionPerformed (ActionEvent event)
        {

            //---Import Function---
            //The following statement will ask the user for input first 
            //BEFORE it displays title and JFrame
            //Setting the file type, showing the dialog box:
            JFileChooser chooser = new JFileChooser();
            FileNameExtensionFilter filter = new FileNameExtensionFilter("TEXT FILES",
                    "txt","text"); //only allows user to import .txt files
            chooser.setFileFilter(filter);
            int returnVal = chooser.showOpenDialog(null);


            //Execute the following and import data
            //if the file type match:
            Scanner fileScan;
            String stringAdd;

            if (returnVal == JFileChooser.APPROVE_OPTION) {
                File inputFile = chooser.getSelectedFile();
                fileScan = new Scanner(inputFile);

                //Scan the first line as title:
                //-disable-frame = new JFrame (fileScan.nextLine());


                //Parsing the data into Arraylist
                while (fileScan.hasNext())
                {

                    stringAdd = fileScan.nextLine();
                    StringFile.add(new String(stringAdd));

                } }  //End of the if statement- 

        }



    }  //---End of private inner class---


}  //End of the second .java file
公共类FrameGUI
{
公共静态void main(字符串[]args)引发异常
{
//创建ArrayList并初始化JFrame
ArrayList stringFile=新的ArrayList();
JFrame=新JFrame(“标题”);
*//以下代码的其余部分在此处*
}/---主方法结束---
}---第一个.java文件的结尾
这是第二个.java文件,其中包含带有导入按钮的面板,执行操作,读取.txt文件的内容并将其导入arraylist:

public class FrameGUI 
{

    public static void main(String[] args) throws Exception
    {

        //Creates ArrayList and initiating JFrame
        ArrayList<String> stringFile = new ArrayList<String>();
        JFrame frame = new JFrame("Title");


        *//Rest of the following codes here*        


    }  //---End of Main Method---


} ---End of the First .java file
public class ImportPanel extends JPanel
{

    private JButton importButton;

    public ImportPanel(ArrayList<String> StringFile)
    {

    //Setting up JButton and link it to inner class of
    //ActionListener
    importButton = new JButton("Import Data");
    importButton.addActionListener(new importListener());


    *//***Adding the button into panel, set background color and panel size*

}  //---End of main method---


    //Private inner class:
    private class importListener implements ActionListener
    {

        public void actionPerformed (ActionEvent event)
        {

            //---Import Function---
            //The following statement will ask the user for input first 
            //BEFORE it displays title and JFrame
            //Setting the file type, showing the dialog box:
            JFileChooser chooser = new JFileChooser();
            FileNameExtensionFilter filter = new FileNameExtensionFilter("TEXT FILES",
                    "txt","text"); //only allows user to import .txt files
            chooser.setFileFilter(filter);
            int returnVal = chooser.showOpenDialog(null);


            //Execute the following and import data
            //if the file type match:
            Scanner fileScan;
            String stringAdd;

            if (returnVal == JFileChooser.APPROVE_OPTION) {
                File inputFile = chooser.getSelectedFile();
                fileScan = new Scanner(inputFile);

                //Scan the first line as title:
                //-disable-frame = new JFrame (fileScan.nextLine());


                //Parsing the data into Arraylist
                while (fileScan.hasNext())
                {

                    stringAdd = fileScan.nextLine();
                    StringFile.add(new String(stringAdd));

                } }  //End of the if statement- 

        }



    }  //---End of private inner class---


}  //End of the second .java file
公共类ImportPanel扩展了JPanel
{
私有JButton importButton;
公共导入面板(ArrayList StringFile)
{
//设置JButton并将其链接到
//ActionListener
importButton=新JButton(“导入数据”);
addActionListener(新的importListener());
*//***将按钮添加到面板中,设置背景颜色和面板大小*
}/---主方法结束---
//私有内部类:
私有类importListener实现ActionListener
{
已执行的公共无效操作(操作事件)
{
//---导入功能---
//下面的语句将首先要求用户输入
//在显示标题和JFrame之前
//设置文件类型,显示对话框:
JFileChooser chooser=新的JFileChooser();
FileNameExtensionFilter=新FileNameExtensionFilter(“文本文件”,
“txt”,“text”);//仅允许用户导入.txt文件
选择器.setFileFilter(过滤器);
int returnVal=chooser.showOpenDialog(null);
//执行以下命令并导入数据
//如果文件类型匹配:
扫描仪文件扫描;
字符串字符串添加;
if(returnVal==JFileChooser.APPROVE_选项){
File inputFile=chooser.getSelectedFile();
fileScan=新扫描仪(inputFile);
//将第一行扫描为标题:
//-disable frame=newjframe(fileScan.nextLine());
//将数据解析为Arraylist
while(fileScan.hasNext())
{
stringAdd=fileScan.nextLine();
添加(新字符串(stringAdd));
}}//if语句的结尾-
}
}/---私有内部类结束---
}//第二个.java文件的结尾

在提出问题之前,我已经尝试过搜索,我知道为了在不同的类上更改变量,我们需要添加一个叫做引用的东西。但是在这种情况下,当类位于两个不同的.java文件而不是一个文件上时,我不知道如何添加以及在何处添加。

在您的
ImportPanel
类中,您如何在
importListener
内部类中使用该
StringFile
ArrayList
对象?
这里的
StringFile
ImportPanel()
构造函数的本地对象。检查是否理解java中的按引用传递。在您的情况下,我更喜欢使用单个
公共静态ArrayList StringFile对象,并在所有其他类中使用它。

要更快获得更好的帮助,请发布一个。SSCCE可以有多个类,但它们都必须位于同一源文件中,并且只有一个类(具有
main
方法的类)声明为
public