.getText在用户能够输入之前抓取文本

时间:2015-11-20 01:08:22

标签: java swing

我的程序有三个功能(退出除外):读取文件,在文件中搜索并写入文件。我有一个完整的功能作为控制台应用程序,我正在为它的GUI(Swing)工作。我有一个主窗口,其中创建了所有组件并且功能正常(按钮单击 - >通过控制台运行)。我之后现在完全切断了t =我对控制台的需求。

我正在处理Read函数的这个更改(目前),所以我可以让它正常工作。我遇到的问题是用户无法在JTextField&内部指定所需的文件(类型路径)。在获取值(null)并将其分配给文件变量之前,单击与所需函数对应的buttonn。

这是包含所有内容的类,除了包含特定函数的类,我通过main链接到它:

public class SimpleDBGUI{
static File targetFile;     //Declare File var to be used in methods below for holding user's desired file

public void mainWindow(){


    //Create main window for Program
    JFrame mainWindow = new JFrame("Simple Data Base");     //Init frame
    mainWindow.setSize(500, 180);       //Set frame size
    mainWindow.setVisible(true);        //Make frame visible

    //Create panel for the main window of the GUI
    JPanel simpleGUI = new JPanel( new GridBagLayout());
    GridBagConstraints gbCons = new GridBagConstraints();
    mainWindow.getContentPane().add(simpleGUI);         //Adds JPanel container to the ContentPane of the JFrame

    //Create button linking to read function
    JButton readButton = new JButton("Read");       //Init button, and give text
    gbCons.fill = GridBagConstraints.BOTH;
    gbCons.gridx = 0;
    gbCons.gridy = 2;
    gbCons.weightx = .1;
    readButton.setActionCommand("Read");
    readButton.addActionListener( new ButtonClickListener());
    simpleGUI.add(readButton, gbCons);      //Adds the "Read" button to the JPanel

    //Create button linking to the search function
    JButton searchButton = new JButton("Search");       //Init button with text
    gbCons.fill = GridBagConstraints.BOTH;
    gbCons.gridx = 1;
    gbCons.gridy = 2;
    gbCons.weightx = .1;
    searchButton.setActionCommand("Search");
    searchButton.addActionListener( new ButtonClickListener());
    simpleGUI.add(searchButton, gbCons);        //Adds the "Search" button to the JPanel

    //Create button linking to the write function
    JButton writeButton = new JButton("Write");     //Init button with text
    gbCons.fill = GridBagConstraints.BOTH;
    gbCons.gridx = 2;
    gbCons.gridy = 2;
    gbCons.weightx = .1;
    writeButton.setActionCommand("Write");
    writeButton.addActionListener( new ButtonClickListener());
    simpleGUI.add(writeButton, gbCons);         //Adds the "Write" button to the JPanel


    //Create label prompting user to specify desired function
    JLabel promptText = new JLabel("Click 'Read' to read a file, 'Search' to search within a file, 'Write' to write to a file:");       //Give user a prompt to select desired function
    gbCons.fill = GridBagConstraints.BOTH;
    gbCons.gridx = 0;
    gbCons.gridy = 0;
    gbCons.gridwidth = 3;
    simpleGUI.add(promptText, gbCons);      //Add prompt to the JPanel

    //Create button to exit program
    JButton exitButton = new JButton("Exit");
    gbCons.fill = GridBagConstraints.BOTH;
    gbCons.gridx = 0;
    gbCons.gridy = 3;
    gbCons.gridwidth = 3;
    exitButton.setActionCommand("Exit");
    exitButton.addActionListener( new ButtonClickListener());
    simpleGUI.add(exitButton, gbCons);      //Add "Exit" button to the JPanel

    //Create TextField for user to input a desired file
    JTextField sdbTarget = new JTextField();
    gbCons.fill = GridBagConstraints.BOTH;
    gbCons.gridx = 0;
    gbCons.gridy = 1;
    gbCons.gridwidth = 3;
    simpleGUI.add(sdbTarget, gbCons);               //Adds TextField to GUI
    targetFile = new File(sdbTarget.getText());     //Writes input string to a File var, works but out of time resulting in null value
}

public class ButtonClickListener implements ActionListener{         //Sets the EventListener for every function

    public void actionPerformed(ActionEvent event){

        //targetFile = File(sdbTarget.getText());           //Was a proposed solution, but kept giving type resolving errors
        try{                                                                            //Leftovers from the console version of the app,
            Scanner inputChoice = new Scanner(System.in);                               //these were left so my Search and Write functions would still work in the console
            File file = new File("C:/Users/Joshua/Desktop/jOutFiles/SimpleDB.txt");     //
            FileWriter writer = new FileWriter(file, true);                             //

            String function = event.getActionCommand();                                 

//Reads the ActionCommand into a string for use in performing desired function
                if( function.equals("Read")){                                               //Read function
                    Desktop desktop = Desktop.getDesktop();                                 //For the GUI version, supposed to open "targetFile" upon readButtonClick
                    desktop.open(targetFile);                                               //
//                  SimpleDBReader sdbrObject = new SimpleDBReader();                       //Bit left from console version, still functional
//                  sdbrObject.sdbReader(inputChoice, file);                                //
                }else if( function.equals("Search")){                                       //Search Function
                    SimpleDBSearch sdbsObject = new SimpleDBSearch();
                    sdbsObject.sdbSearch(inputChoice, writer, file);
                }else if( function.equals("Write")){                                        //Write function
                    SimpleDBWriter sdbwObject = new SimpleDBWriter();
                    sdbwObject.sdbWriter(inputChoice, file);
                }else if( function.equals("Exit")){
                    System.exit(0);
                }
            }catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:3)

像大多数GUI一样,Swing是事件驱动的,也就是说,某些事情会发生并且你会对它做出反应。您的代码在方法上是相当线性的,您可以创建UI组件,但是,即使在向用户呈现UI之前,您也可以targetFile = new File(sdbTarget.getText());

但是sdbTarget尚未应用任何值。

但更好的解决方案是使用您的ActionListenerButtonClickListener来检查值

String target = sdbTarget.getText();
if (target != null && !target.trim().isEmpty()) {
    File targetFile = new File(target)
    String function = event.getActionCommand();                                 //Reads the ActionCommand into a string for use in performing desired function
    if (function.equals("Read")) {                                              //Read function
        Desktop desktop = Desktop.getDesktop();                                 //For the GUI version, supposed to open "targetFile" upon readButtonClick
        desktop.open(targetFile);                                               
        //...

这样,每次调用targetFile时,您始终会评估actionPerformed