我正在尝试创建一个简单的GUI,向用户询问一系列问题,然后将其响应存储在以后进行分配。我已经确定我的错误在我的方法中基本上执行简单的7个问题。我已将7个问题添加到ArrayList中,为我的答案创建了一个空的ArrayList,但是当我运行该方法时,它唯一显示的是最后一个问题的文本。
显然是一个Java菜鸟,但是100%的GUI / swing / AWT菜鸟,真的觉得我不知道我在做什么,所以任何人都愿意提出的任何时间或努力都是为了教育我非常感谢,我提前感谢你!!
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();
//int's needed to create a personnel Risk
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() {
setupQuestions();
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) {
calcPersonnelRisk();
}
});
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 are still in a training status(less than fulll-time, T-RCOs)??");
questions.add("How many mission personnel are on duty during flight operations?");
}
private void calcPersonnelRisk() {
yesButton.setVisible(false);
noButton.setVisible(false);
message.setFont(new Font("Tahoma", Font.BOLD, 14));
for (int i=0; i<questions.size(); i++){
message.setText(questions.get(i));
enterButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int r= Integer.parseInt(textField.getText().trim());
responses.add(r);
}});
}
}
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 :(得分:1)
如果您想循环查看问题,则无法一次性加载所有问题。
当您单击“输入”按钮时,类似这样的内容应该会加载下一个问题。
private int questionIndex; // store which question youre on
private void calcPersonnelRisk() {
yesButton.setVisible(false);
noButton.setVisible(false);
message.setFont(new Font("Tahoma", Font.BOLD, 14));
// load the first question
message.setText(questions.get(questionIndex++));
enterButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int r= Integer.parseInt(textField.getText().trim());
responses.add(r);
// load the next question
if (questionIndex < questions.size()) {
message.setText(questions.get(questionIndex++));
} else {
message.setText("Done");
}
}});
}
}