1

我正在创建一个有助于解决会计账簿问题的应用程序。该应用程序有 12 章。所有章节都包含 15-20 个问题。问题显示在包含各种组合框和格式化文本框的 JPanel 中。假设我解决了一半的问题并想保存,以便下次我可以加载解决了一半的问题。

保存应通过单击菜单栏中的保存来完成。并从菜单栏加载

所有菜单栏和问题表都在工作,但我无法保存任何东西。我正在使用jFilechooser ...

他们有什么办法吗?

如何保存带有填充组合框项目和文本框的面板。他们是否有任何方式知道用户对任何项目进行了任何更改,以便在关闭问题时我可以再次要求先保存然后退出。

提前致谢。

我的一些代码:

private void openBtnMouseClicked(java.awt.event.MouseEvent evt) {
opening();
}
public void opening() {
        JFileChooser chooser=new JFileChooser();
        int choice=chooser.showOpenDialog(this);       
        javax.swing.JComboBox[] sourceALE = {aaCombo, baCombo, caCombo, daCombo, eaCombo, faCombo, gaCombo, haCombo, iaCombo, jaCombo, kaCombo,
                            alCombo, blCombo, clCombo, dlCombo, elCombo, flCombo, glCombo, hlCombo, ilCombo, jlCombo, klCombo,
                            aeCombo, beCombo, ceCombo, deCombo, eeCombo, feCombo, geCombo, heCombo, ieCombo, jeCombo, keCombo};
                            javax.swing.JTextField[] sourceP = {aeval1, beval, ceval, deval, eeval, feval, geval, heval, ieval, jeval, keval};
                           String [] comboboxes={"aaCombo", "baCombo", "caCombo", "daCombo", "eaCombo", "faCombo", "gaCombo", "haCombo", "iaCombo", "jaCombo", "kaCombo","alCombo", "blCombo", "clCombo", "dlCombo", "elCombo", "flCombo", "glCombo", "hlCombo", "ilCombo", "jlCombo","klCombo","aeCombo", "beCombo", "ceCombo", "deCombo", "eeCombo", "feCombo", "geCombo", "heCombo", "ieCombo", "jeCombo", "keCombo"};
        if(choice==JFileChooser.APPROVE_OPTION) {

            File file = chooser.getSelectedFile();
            try {
                System.out.println("Hey");
                Scanner scanner = new Scanner(new FileReader(file));
                while ( scanner.hasNextLine() ){
                    Scanner scan = new Scanner(scanner.nextLine());
                    scan.useDelimiter("=");
                    if ( scan.hasNext() ){
                        String item=scan.next();
                        int value=scan.nextInt();
                        String color=scan.next();
                        for(int g=0;g<comboboxes.length;g++){
                            if(item.equals(comboboxes[g])) {
                                if(value<3)
                                sourceALE[g].setSelectedIndex(value);
                                if(color.equals("red"))
                                sourceALE[g].setForeground(red);
                                if(color.equals("green"))
                                sourceALE[g].setForeground(green);
                                if(color.equals("blah"))
                                sourceALE[g].setForeground(blah);
                            }
                        }
                    }
                    scan.close();
                }
                scanner.close();
            } catch (FileNotFoundException ex) {
                Logger.getLogger(q1.class.getName()).log(Level.SEVERE, null, ex);
            }
                }
                public void opening() {
        JFileChooser chooser=new JFileChooser();
        int choice=chooser.showOpenDialog(this);       
        javax.swing.JComboBox[] sourceALE = {aaCombo, baCombo, caCombo, daCombo, eaCombo, faCombo, gaCombo, haCombo, iaCombo, jaCombo, kaCombo,
                            alCombo, blCombo, clCombo, dlCombo, elCombo, flCombo, glCombo, hlCombo, ilCombo, jlCombo, klCombo,
                            aeCombo, beCombo, ceCombo, deCombo, eeCombo, feCombo, geCombo, heCombo, ieCombo, jeCombo, keCombo};
                            javax.swing.JTextField[] sourceP = {aeval1, beval, ceval, deval, eeval, feval, geval, heval, ieval, jeval, keval};
                           String [] comboboxes={"aaCombo", "baCombo", "caCombo", "daCombo", "eaCombo", "faCombo", "gaCombo", "haCombo", "iaCombo", "jaCombo", "kaCombo","alCombo", "blCombo", "clCombo", "dlCombo", "elCombo", "flCombo", "glCombo", "hlCombo", "ilCombo", "jlCombo","klCombo","aeCombo", "beCombo", "ceCombo", "deCombo", "eeCombo", "feCombo", "geCombo", "heCombo", "ieCombo", "jeCombo", "keCombo"};
        if(choice==JFileChooser.APPROVE_OPTION) {

            File file = chooser.getSelectedFile();
            try {
                System.out.println("Hey");
                Scanner scanner = new Scanner(new FileReader(file));
                while ( scanner.hasNextLine() ){
                    Scanner scan = new Scanner(scanner.nextLine());
                    scan.useDelimiter("=");
                    if ( scan.hasNext() ){
                        String item=scan.next();
                        int value=scan.nextInt();
                        String color=scan.next();
                        for(int g=0;g<comboboxes.length;g++){
                            if(item.equals(comboboxes[g])) {
                                if(value<3)
                                sourceALE[g].setSelectedIndex(value);
                                if(color.equals("red"))
                                sourceALE[g].setForeground(red);
                                if(color.equals("green"))
                                sourceALE[g].setForeground(green);
                                if(color.equals("blah"))
                                sourceALE[g].setForeground(blah);
                            }
                        }
                    }
                    scan.close();
                }
                scanner.close();
            } catch (FileNotFoundException ex) {
                Logger.getLogger(q1.class.getName()).log(Level.SEVERE, null, ex);
            }
                }
        System.out.println("OUT");
            }
            }
4

2 回答 2

2

我想知道您是否主要将这个程序创建为 NetBeans 生成的 GUI,而没有首先创建程序的非 GUI 内容。所以首先我要问,你是从你的 GUI 代码中分离出你的程序逻辑吗?如果不是,那么这应该是您的首要任务,因为实际上数据的保存和恢复应该与它的 GUI 表示无关。

解决问题的关键是良好的面向对象编程设计。例如,考虑创建一个非 GUI 问题类,该类具有多个字段,包括字符串问题字段、可能答案的 ArrayList 和正确答案的字段字符串。考虑为每章和章节的集合设置这些问题的 ArrayList (ArrayList)。然后,您需要找出保存问题和用户答案的​​最佳方式。这可能是通过序列化、XML(JAXB?)或我的建议:数据库。

只有在您弄清楚了所有这些之后,您才应该担心如何围绕您的非 GUI 逻辑代码集成 GUI,这将是 GUI 的“模型”。

编辑
对您的评论的回复:

抱歉,我没有提供足够的项目信息。我是在 NETBEANS 7 GUI 中做的。我猜 netbeans java 程序是 MVC

抱歉,没有。如果您所做的只是使用 NetBeans 创建一个 GUI,那么您将创建一个笨拙的神级. 要拥有好的 MVC,你必须从一开始就这样设计它。同样,这意味着在创建 GUI之前创建程序逻辑,在您的情况下,这包括创建多个非 GUI 类,如我上面所指出的。NetBeans 中没有“自动”MVC。

我可能错了,因为我对 MVC 了解不多。

是的,不幸的是你错了。请阅读:模型-视图-控制器。这将帮助您进行当前和未来的设计。

截至目前,我能够保存和检索。但我想要你的建议。每个问题都经过硬编码。

这可能应该得到解决,因为您不会对问题进行硬编码。事实上,问题文本甚至不应该是代码的一部分,而是数据的一部分。考虑将问题放在逗号分隔的文本文件或数据库中。

我试图回答 5 个组件并将其保存在记事本中。在加载它时,我能够填充所有这些组件。它的工作。但它的编码相当硬,当然是糟糕的编程。但是我有一些时间限制,所以你能建议任何类似于这个逻辑的东西。如果通过某些函数或方法我可以知道我的 JPanel 中存在的所有组件变量名称。像所有 15-20 组合框和相同的文本框。我可以制作一个对所有人都通用的功能

Sorry, I wish I could, but I can't, not unless you first fix your design issues. Good luck!

于 2011-06-28T21:39:38.427 回答
2

1) It is good design to separate your data from the presentation. Are you doing this already? What I mean is you should be looking to store answers in an object outside of the jPanel class. (As 'Hovercraft Full Of Eels' suggested)

2) Consider making the data objects Serializable objects. If you're not planning on storing the data across a network or anything complicated/peculiar this should work out for you as it will facilitate save/load operations

3) Once you have your data separate from the GUI a check for changes becomes very easy. Assume that on any operation that takes you away from the page it "saves" its state. If the current state is different from the saved state then prompt the user to save (if it doesn't by default)

于 2011-06-28T21:41:38.380 回答