Как я могу заставить мою программу ждать взаимодействия пользователя с моим JFrame? - PullRequest
0 голосов
/ 07 марта 2019

Я пытаюсь написать приложение для обработки файлов, но программа не будет ждать, пока пользователь выберет файл, прежде чем перемещать и завершать функцию.Я пытался использовать 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;
}

}

Я включил всекода на всякий случай.

...