好吧,完成了我的大多数问题(在Java中......今天......)
在这个GUI调查问卷中,我设法逐步完成我的问题,得到我的值...但由于某种原因,它在循环的每次迭代期间抛出一个超出范围的异常...它实际上并没有导致程序结束或任何事情,但要求我翻阅问题并给出回复,我看到我的控制台显示每个循环计数的界限错误,直到问到最后一个问题。 如果我注释掉我的calcPersonnelRisk(),它在索引计数器是列表的大小时运行,它会在没有任何错误的情况下运行问题。困惑,因为该方法只是从我的答案中将值分配给我的变量,并且不知道为什么使用该代码会搞砸它。作为一名程序员,我仍然处于初级阶段,并且仍在发展我有效调试的能力......但是乍一看任何输入都是值得赞赏的。提前致谢!
public class ORMRiskCalculator extends JFrame{
private JButton yesButton, noButton, exitButton, enterButton;
private JLabel message;
private JTextField textField;
private JFrame frmOrmRiskCalculator;
private final ButtonGroup buttonGroup = new ButtonGroup();
private int questionIndex;
private ArrayList<String> questions = new ArrayList<String>();
private ArrayList<Integer> responses = new ArrayList<Integer>();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ORMRiskCalculator window = new ORMRiskCalculator();
window.frmOrmRiskCalculator.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public ORMRiskCalculator() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmOrmRiskCalculator = new JFrame();
frmOrmRiskCalculator.getContentPane().setBackground(new Color(0, 102, 153));
frmOrmRiskCalculator.setTitle("ORM Risk Calculator");
frmOrmRiskCalculator.setBounds(100, 100, 650, 500);
frmOrmRiskCalculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmOrmRiskCalculator.getContentPane().setLayout(null);
message = new JLabel("Create a new ORM Report?");
message.setVerticalAlignment(SwingConstants.CENTER);
message.setHorizontalAlignment(SwingConstants.CENTER);
message.setFont(new Font("Tahoma", Font.BOLD, 20));
message.setBounds(10, 10, 614, 109);
frmOrmRiskCalculator.getContentPane().add(message);
yesButton = new JButton("YES");
yesButton.setFont(new Font("Tahoma", Font.BOLD, 14));
yesButton.setForeground(Color.BLACK);
yesButton.setBackground(Color.GREEN);
yesButton.setBounds(80, 240, 200, 50);
frmOrmRiskCalculator.getContentPane().add(yesButton);
yesButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
questions.clear();
responses.clear();
getAnswers();
}
});
noButton = new JButton("NO");
noButton.setBackground(Color.RED);
noButton.setFont(new Font("Tahoma", Font.BOLD, 14));
noButton.setBounds(80, 330, 200, 50);
frmOrmRiskCalculator.getContentPane().add(noButton);
noButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
textField = new JTextField();
textField.setBounds(350, 275, 100, 50);
textField.setFont(new Font("Tahoma", Font.BOLD, 20));
frmOrmRiskCalculator.getContentPane().add(textField);
textField.setColumns(10);
enterButton = new JButton("Enter");
enterButton.setFont(new Font("Tahoma", Font.BOLD, 14));
enterButton.setBounds(450, 275, 100, 50);
buttonGroup.add(enterButton);
frmOrmRiskCalculator.getContentPane().add(enterButton);
exitButton = new JButton("EXIT");
exitButton.setBackground(Color.GRAY);
exitButton.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 18));
exitButton.setBounds(80, 425, 470, 25);
frmOrmRiskCalculator.getContentPane().add(exitButton);
exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
shutDown();
}
});
}
private void setupQuestions(){
questions.add("How many mission personnel had less than normal rest?");
questions.add("How many mission personnel have an illness that may impact the mission?");
questions.add("How many mission personnel are taking medications that may affect duty?");
questions.add("How many mission personnel are scheduled to work longer than ten hours?");
questions.add("How many mission personnel worked greater than ten hours on the previous shift?");
questions.add("How many mission personnel swapped from night shift to day shift in the last 24 hrs?");
questions.add("How many mission personnel are still in a training status or are a TEMP RCOs)?");
questions.add("How many mission personnel are on duty during flight operations?");
}
protected void getAnswers() {
setupQuestions();
yesButton.setVisible(false);
noButton.setVisible(false);
message.setFont(new Font("Tahoma", Font.BOLD, 14));
message.setText(questions.get(questionIndex++));
enterButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int r= Integer.parseInt(textField.getText().trim());
responses.add(r);
textField.setText("");
if(questionIndex < questions.size())
message.setText(questions.get(questionIndex++));
else {message.setText("Done!"); calcPersonnelRisk();}
//do finishing stuff, move to the next method!
}});
}//end getAnswers
protected void calcPersonnelRisk() {
int lessThanNormalRest, illness, meds, lengthOfDay, previousDutyDay, crewExperience,
shiftSwap, minManning;
//probably better in this instance to use TreeMaps for future iterations of this program?
lessThanNormalRest = responses.get(0);
illness = responses.get(1);
meds = responses.get(2);
lengthOfDay=responses.get(3);
previousDutyDay=responses.get(4);
shiftSwap=responses.get(5);
crewExperience = responses.get(6);
if(responses.get(7) >= 4)minManning = 2;
else minManning=0;
PersonnelRisk pRisk = new PersonnelRisk(lessThanNormalRest,illness,meds,lengthOfDay,
previousDutyDay,crewExperience,shiftSwap,minManning);
message.setFont(new Font("Tahoma", Font.BOLD, 20));
message.setText("Your calculated personnel Risk is: "+pRisk.calcRisk()
+"\n\t Run again?" );
yesButton.setVisible(true);
noButton.setVisible(true);
}
private void shutDown(){
message.setFont(new Font("Tahoma", Font.BOLD, 26));
message.setText("Good bye!");//why doesn't this work? Sleeps, and closes,
//doesn't show my text message.
try {
Thread.sleep(1000);//also tried a wait(), notify(), but it seemed like the notify never allowed it to get to the exit command
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
System.exit(0);
}//end shutDown method
}//endORMRiskCalculator
答案 0 :(得分:0)
抱歉,我快速看了一眼,但是如果我明白发生了什么,那么肯定按钮会触发动作,清除问题列表并调用getAnswer女巫试图从问题列表中获取值...所以...