事后将整个GUI添加到JFrame

时间:2016-01-11 20:01:39

标签: java swing jframe

今天我有另一个项目涉及我为工作编写的程序,并且已经工作了6个多月。有很多代码和类,所以我会尝试解释我能做的最好的事情,所以你可以(希望)帮助我。

目前我的程序通过读取文件并允许用户对该文件进行一些修改来工作,然后写入新文件。整个过程涉及一个GUI,可以更好地解释为一系列JOptionPanes,其中一些具有嵌入式面板。

这是我的目标:每个上传的文件都带有一定数量的"批次"。我的程序为每个批次循环一次。在循环期间,显示每个相关的JOptionPane GUI。当读取所有批次时,程序结束并且文件完成。

我被要求添加一个功能,其中整个"项目"是一个JFrame内部的新"上传"按钮。这将允许用户多次运行程序而无需一遍又一遍地打开JAR。如果他们选择"上传"他们基本上开始了该计划。

这是我的主要课程:

package nachamultifive;

import java.util.ArrayList;

import javax.swing.JFileChooser;

import nachamultifive.Buffered_Reader_Writer.BatchCounter;
import nachamultifive.Buffered_Reader_Writer.FileValidation;
import nachamultifive.Buffered_Reader_Writer.MainWriter;
import nachamultifive.GUIs.FileHandling;
import nachamultifive.GUIs.ReturnBuilderGUI;

public class NachaMain 
{

    public static JFileChooser saveFile;//The output file save location.
    public static JFileChooser uploadFile;//The uploaded NACHA file. 

    public static int batchTotal;//The total number of batches in the file. 
    public static ArrayList<String> batchHeaders;//An array of all batch headers.
    public static int batchCounter;//The counter that displays the current batch number in sequence.

    public static String location;

    public static void main(String args[]){

        FileHandling fHandling = new FileHandling();//The class that handles upload/save of files. 

        fHandling.getFile();//Allows the user to upload a file.
        fHandling.setDirectory();//Allows the user to choose the save location. 
        saveFile=fHandling.saveFile;//Sets the file save location as static with the main class.
        uploadFile=fHandling.uploadFile;//Sets the uploaded file as static with the main class.

        BatchCounter bCounter = new BatchCounter();//The class that handles counting the batches. 

        bCounter.getBatches();//Counts the total number of batches.
        batchTotal=BatchCounter.BatchTotal;//Sets the total number of batches as static with the main class.
        batchHeaders=bCounter.batchHeaders;//Sets the batch header array as static with the main class. 

        MainWriter mWriter = new MainWriter();//The class that handles all writing functions for the new file. 

        mWriter.writeNacha();//Writes the output file. 

        location = MainWriter.location;
        System.out.println("NachaMain Location=" + location);

        FileValidation fValidation = new FileValidation();//The class that handles validating the output ACH file. 

        fValidation.validateNacha();//Method to validate the ACH file. 

        ReturnBuilderGUI gui = new ReturnBuilderGUI();//Class used for GUI's.

        gui.displayFileOption();//Method used to display the ACH output and error report name. 

        gui.showSavedErrors();//Method to display the error report. 

    }   
}

基本上每个类调用都会修改输入文件。在mWriter类的内部,你会看到这段代码:

ReturnBuilderGUI gui = new ReturnBuilderGUI();//The GUI class.
                    ReturnBuilderGUI.displayGUI();//Calls the GUI to display the initial double list GUI. 

调用该类调用该循环的整个GUI。 (mWriter类为每个批处理循环)。当调用ReturnBuilder类时,这是基本的代码布局:

public static void displayGUI(){//Method to display the GUI. 

        final JButton createReturnButton = new JButton("Create Return");
        createReturnButton.addActionListener(new ActionListener(){

            public void actionPerformed(final ActionEvent ae){

                if(verifyBatch==true){

                        initialScreenDecisions="NONE";//The user did not choose to add any entry details to the output list.
                        MainWriter.finishedCounter=true;//The boolean counter to trigger that the return is finished goes to true.
                        while(MainWriter.entryDetails.size()>0){//Removes all entry details from the input list.
                            MainWriter.entryDetails.remove(0);
                        }

                        while(output.size()>0){//Removes all entry details from the output list..
                            output.remove(0);
                        }

                        JOptionPane.getRootFrame().dispose();

                }else {

                    JOptionPane.showMessageDialog(null, "No batches have been completed!"); 
                }
            }
        });

        final Object[] createR = new Object[] { "Confirm",createReturnButton }; 

            int result = JOptionPane.showOptionDialog(null, getPanel(),"Return Builder", JOptionPane.OK_CANCEL_OPTION, 
                JOptionPane.PLAIN_MESSAGE, null, createR, "default");

        System.out.println(verifyBatch);


        //Creates a JOptionPane for the first GUI featuring 7 buttons and 2 lists.. 
    }

JOptionsPane内部的getPanel()方法调用具有一些按钮和列表的面板。根据用户选择的内容,将出现更多的JOptionPane,为用户提供更多选项。完成后,初始mWriter类将再次循环(假设输入文件中有更多批次),并且将再次调用ReturnBuilder类重新启动该过程。

现在,我不能为我的生活找到一种方法,让所有这些都发生在JFrame内部,在所有这些其他事情发生之前和之后,而不必重构我的代码。

我不知道我是否给了你足够的信息。我现在唯一的想法是,我觉得我需要在ReturnBuilder类中创建一个JFrame并将JOptionsPane添加到它,但是当稍后再次调用ReturnBuilder类时,我确定JFrame将再次打开并且重复。

有什么想法吗?

1 个答案:

答案 0 :(得分:3)

我真的希望你需要使用Cardlayout并在每一步翻转到下一张牌。完成后,通过翻转到第一张卡进行重置。 CardLayout是循环的,因此会自动翻到第一张卡片。

public class CardExample extends JFrame {

    CardExample() {

        JPanel main = new JPanel(new BorderLayout());
        CardLayout cl = new CardLayout();
        main.setLayout(cl);
        for (int i = 0; i < 4; i++)
            main.add(new StepPanel(i));

        JButton next = new JButton("Next");
        next.addActionListener(e -> cl.next(main));

        add(main);
        add(next, BorderLayout.PAGE_END);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    class StepPanel extends JPanel {

        StepPanel(int i){

            add(new JLabel("Step " + i));
        }
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(() -> new CardExample());
    }
}

所有这些都取代了JOptionPane s,这对于逐步用户交互通常更为舒适(例如,参见安装程序)。只需自定义我称之为StepPanel的内容,最后您可以使用&#34;加载和重置&#34;按钮而不是&#34; next&#34;。