-1

したがって、ユーザーがボタンを押さないと、アクションリスナーはトリガーされず、例外が発生します。したがって、FrameClass にデフォルトの文字列を配置し、ボタンがクリックされるたびにその文字列を変更することを考えました。メイン クラスでは、デフォルトの文字列が変更されるまでループし続けるループを実行するので、無限ループだと思います。そんなことしていいの?

package gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRootPane;

/**
 * 
 * @author E-TECH
 */
public class ButtonsFrame extends JFrame {

    private JButton ScPerF, weekSc, both,cancel;
    private SchedulePerWeek week;
    private CoursesPerWeek course;
    private JPanel panel;
    private String choice;
    private File file;

    public ButtonsFrame() {
        ScPerF = new JButton("Generate schedule/faculty");
        weekSc = new JButton("Generate weekly class schedule");
        both = new JButton("Generate Both");
        cancel = new JButton("Cancel");
        choice="nothing";

        ScPerF.addActionListener(new ButtonListener());
        weekSc.addActionListener(new ButtonListener());
        both.addActionListener(new ButtonListener());
        cancel.addActionListener(new ButtonListener());
        setResizable(false);
        setUndecorated(true);
        getRootPane().setWindowDecorationStyle(JRootPane.NONE);

        panel = new JPanel();
        panel.add(ScPerF);
        panel.add(weekSc);
        panel.add(both);
        panel.add(cancel);
        getContentPane().add(panel);
        setVisible(true);
        pack();
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    }

    private class ButtonListener implements ActionListener {

        public void actionPerformed(ActionEvent event) {
            if (event.getSource() == ScPerF) {
                dispose();

                choice = "faculty";

            }
            if (event.getSource() == weekSc) {
                dispose();

                choice = "course";
            }
            if (event.getSource() == both) {
                dispose();
                choice = "both";
            }
            if (event.getSource()==cancel){
                dispose();
                choice="cancel";
            }


        }

    }

    public boolean Activated() {
        return ScPerF.isSelected() || weekSc.isSelected();
    }

    public String getChoice() {
        return choice;
    }

    public File getFile() {
        return file;
    }

}

public class SchedulePerWeek {
    HSSFSheet weekSh,courseSh;
    int instructor_count;
    HSSFWorkbook wb;

    public SchedulePerWeek() {


        ExcelReader reader = new ExcelReader();

        HSSFSheet sh = reader.getSortedSheet();
        String choice=reader.getChoice();

        if(choice.equals("cancel")||choice.equals("nothing")){///i fixed the exception with this condition by closing the program instead of continuing,but i want to wait for the user instead of just exiting the program
            System.exit(1);
}
        wb = new HSSFWorkbook();
/////
///more code
4

1 に答える 1