gpt4 book ai didi

java - 问卷 GUI(我的第一个 GUI)跳过了我的问题?

转载 作者:行者123 更新时间:2023-12-01 09:08:07 30 4
gpt4 key购买 nike

我正在尝试创建一个简单的 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

最佳答案

如果您想循环浏览问题,则无法一次加载所有问题。

当您单击输入按钮时,类似这样的内容应该加载下一个问题。

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");
}
}});
}
}

关于java - 问卷 GUI(我的第一个 GUI)跳过了我的问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41087241/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com