因此,如果用户不按下任何按钮,则不会触发动作侦听器,并且最终会出现异常。因此,我想在我的 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