这是我的代码的一部分,没有实际的主类,因为它工作正常。我正在尝试编写一个程序,如果您按是(item3),它会说“您知道书名吗?”,但如果您按否(item4),它会询问“您想还书吗”。到目前为止,无论我按下哪个按钮,它都会返回“你知道书名吗?”。我知道这是因为我在这里将 isClicked 更改为 true:'private boolean isClicked = true;' 但我不知道如何改变它,让它做我想要的。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Welcome extends JFrame {
private JLabel title1, title2;
private JButton item3, item4;
public Welcome(){
super("Welcome to Andrew's Library");
setLayout(new FlowLayout());
setBackground(Color.RED);
boolean isClicked = false;
title1 = new JLabel("Welcome to Andrew's Library!!!");
title2 = new JLabel("Would you like to check out a book?");
item3 = new JButton("YES");
item4 = new JButton("NO");
add(title1);
add(title2);
add(item3);
add(item4);
HandlerClass handler = new HandlerClass();
item3.addActionListener(handler);
item4.addActionListener(handler);
}
private class HandlerClass implements ActionListener{
private boolean isClicked = true;
public void actionPerformed(ActionEvent event){
if(isClicked){
title1.setText("Do you know the title of the book?");
title2.setText(null);
} else {
title1.setText(null);
title2.setText(null);
item3.setVisible(false);
item4.setText("Do you want to return a book?");
}
}
}
}