因此,如果用户没有按任何按钮,则不会触发动作侦听器,最终会出现异常。所以我想在我的FrameClass中放置一个默认的String并在单击一个按钮时更改该String,而不是在我的主类中我做一个循环,一直循环直到默认的String被更改,所以我认为它是一个无限循环。这样做可以吗?
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
答案 0 :(得分:1)
我在几次编辑之前运行了你的代码,它在我的Windows 8工作站Java 7上运行良好。
在进一步了解GUI设计之前,请阅读The Use of Multiple JFrames, Good/Bad Practice?
的答案我修改了你的代码以使用JFrame,而不是扩展一个。您只应在覆盖其中一个组件方法时扩展Swing组件。
您只需要定义一次Button侦听器。您可以在按钮上设置监听器。
我将JFrame默认关闭操作更改为关闭时退出。
我添加了一个主要方法,因此我可以运行您的代码。
以下是带有更改的代码。
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;
import javax.swing.SwingUtilities;
/**
*
* @author E-TECH
*/
public class ButtonsFrame{
private JButton scPerf, weekSc, both, cancel;
// private SchedulePerWeek week;
// private CoursesPerWeek course;
private JFrame frame;
private JPanel panel;
private String choice;
private File file;
public ButtonsFrame() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
scPerf = new JButton("Generate schedule/faculty");
weekSc = new JButton("Generate weekly class schedule");
both = new JButton("Generate Both");
cancel = new JButton("Cancel");
choice = "nothing";
ButtonListener listener = new ButtonListener();
scPerf.addActionListener(listener);
weekSc.addActionListener(listener);
both.addActionListener(listener);
cancel.addActionListener(listener);
frame.setResizable(false);
frame.setUndecorated(true);
frame.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
panel = new JPanel();
panel.add(scPerf);
panel.add(weekSc);
panel.add(both);
panel.add(cancel);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (event.getSource() == scPerf) {
frame.dispose();
choice = "faculty";
}
if (event.getSource() == weekSc) {
frame.dispose();
choice = "course";
}
if (event.getSource() == both) {
frame.dispose();
choice = "both";
}
if (event.getSource() == cancel) {
frame.dispose();
choice = "cancel";
}
}
}
public boolean Activated() {
return scPerf.isSelected() || weekSc.isSelected();
}
public String getChoice() {
return choice;
}
public File getFile() {
return file;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new ButtonsFrame();
}
});
}
}