如何使程序等待用户与JFrame交互?

时间:2019-03-06 23:12:16

标签: java actionlistener wait notify

我正在尝试编写文件处理应用程序,但是该程序不会在移动和完成该功能之前等待用户选择文件。我尝试使用wait()和notify()使其停止,但该程序现在冻结,并且按钮d和e从不显示。

import javax.swing.*;
import java.awt.event.*;
import java.io.File;

public class pdfEditor {

static JFrame inter = new JFrame("The Point Updater");
static JLabel reminder = new JLabel("Please select a function:");
static boolean i = false;
JButton a, b, c, d, e;
JFileChooser fc;

public static void main(String[] args) {

    //Sets the window
    inter.setSize(750, 250);
    inter.setLocation(100, 150);
    inter.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    inter.setLayout(null);

    //Label for commands for the user
    reminder.setBounds(50, 50, 650, 30);

    //add a button
    JButton b = new JButton("Update Trainings");
    b.setBounds(50, 150, 135, 30);

    JButton c = new JButton("Update Employees");
    c.setBounds(200, 150, 140, 30);

    JButton a = new JButton("Export Points");
    a.setBounds(355, 150, 135, 30);

    //add them to the frame
    inter.add(reminder);
    inter.add(a);
    inter.add(b);
    inter.add(c);

    inter.setVisible(true);

    //Process selection
    //TODO add catches for unformatted spreadsheets
    a.addActionListener(new ActionListener() { //If export Points button is selected

        @Override
        public void actionPerformed(ActionEvent arg0) {
                reminder.setText("Kashikomarimashita!");
                exportPoints();
        }          
    });

    b.addActionListener(new ActionListener() { //If update trainings is selected

        @Override
        public void actionPerformed(ActionEvent arg0) {
                reminder.setText("Make sure the type is Individual Completions and the columns are set to Training, Employee and Date.");
                File file = null;
                try {
                    file = requestInputSpreadsheet();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                updateTraining(file);
        }          
    });

    c.addActionListener(new ActionListener() { //If update employees is selected

        @Override
        public void actionPerformed(ActionEvent arg0) {
                reminder.setText("Please import a employee list from iScout or Quickbase.");
                File file = null;
                try {
                    file = requestInputSpreadsheet();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                updateEmployees(file);
        }          
    });
}

//Asks the user for a spreadsheet to be used in processing.
public static File requestInputSpreadsheet() throws InterruptedException{

    //makes file chooser
    JFileChooser fc = new JFileChooser();
    fc.addChoosableFileFilter(new SpreadsheetFilter());
    fc.setAcceptAllFileFilterUsed(false);

    //makes new buttons and label
    JLabel name = new JLabel();
    name.setBounds(180, 100, 270, 30);
    JButton d = new JButton("Choose File...");
    d.setBounds(50, 100, 135, 30);
    JButton e = new JButton("Go!");
    e.setBounds(450, 100, 50, 30);

    inter.add(d);
    SwingUtilities.updateComponentTreeUI(inter);

    //switch for the file chooser if file was chosen successfully
    i = false;
    File file = null;

    d.addActionListener(new ActionListener() { //begins file choosing process

        @Override
        public void actionPerformed(ActionEvent arg0) {

            int returnVal = fc.showOpenDialog(inter);

            if (returnVal == JFileChooser.APPROVE_OPTION) {

                //processes file and displays name
                File file = fc.getSelectedFile();
                name.setName(file.getName());

                inter.add(name);
                inter.add(e);
                SwingUtilities.updateComponentTreeUI(inter);
            } 

        }          
    });

    e.addActionListener(new ActionListener() { //returns the selected file

        @Override
        public void actionPerformed(ActionEvent arg0) {
            i = true;
            synchronized (e) {
                 e.notify();
            }
        }          
    });

    synchronized(e) {
          e.wait();
    }

    //removes the button!
    inter.remove(d);
    inter.remove(e);
    SwingUtilities.updateComponentTreeUI(inter);

    if (i == true) {
        return file;
    }
    return null;

}




    //Updates completed training list and awards points based on a spreadsheet exported from the database
    public static boolean updateTraining(File file) {

// still working on the processing
        if (file == null) {
        return false;
    } else {
    System.out.println("Updated Training!!");
    return true;
    }
}

//Updates the employee list using an employee list exported from the database
public static boolean updateEmployees(File file) {
    if (file == null) {
        return false;
    } else {
    System.out.println("Updated Employees!!");
    return true;
    }
}

//Creates and exports a spreadsheet with employee names and current points
public static boolean exportPoints() {
    System.out.println("Exported Points!");
    return true;
}

}

我包括了所有代码,以防万一。

0 个答案:

没有答案