有人可以解释一下,为什么变量“bookAvailable1”和“bookAvailable2”在单击“借阅”或“返回书”按钮后不会更新。我需要创建一个虚拟库,并且在用户借阅或退回书籍后我不会更新库存。我该怎么办?先谢谢。 这是我的代码:
import java.awt.BorderLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class MyFrame extends JFrame {
JPanel panelChB, panelText;
private JLabel title, bookTitle, bookTitle1;
private JButton borrowBook, returnBook;
private JCheckBox ch1, ch2;
int bookAvailable1 = 3;
int bookAvailable2 = 3;
boolean haveBook1 = false;
boolean haveBook2 = false;
public MyFrame() {
setSize(400, 150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setTitle("Books");
setResizable(false);
setLayout(new BorderLayout());
setVisible(true);
panelChB = new JPanel();
panelChB.setLayout(new BoxLayout(panelChB, BoxLayout.Y_AXIS));
ch1 = new JCheckBox();
ch2 = new JCheckBox();
borrowBook = new JButton("BorrowBook");
borrowBook.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (ch1.isSelected() || ch2.isSelected()) {
if (ch1.isSelected() && bookAvailable1 != 0) {
String infoMessage = "You borrowed \"Walk\"";
JOptionPane.showMessageDialog(null, infoMessage,
"Message: ", JOptionPane.INFORMATION_MESSAGE);
bookAvailable1--;
haveBook1 = true;
}
if (ch2.isSelected() && bookAvailable2 != 0) {
String infoMessage = "You borrowed \"Red and Black\"";
JOptionPane.showMessageDialog(null, infoMessage,
"Message: ", JOptionPane.INFORMATION_MESSAGE);
bookAvailable2--;
haveBook2 = true;
}
} else {
String infoMessage = "No book selected";
JOptionPane.showMessageDialog(null, infoMessage,
"Message: ", JOptionPane.INFORMATION_MESSAGE);
}
}
});
returnBook = new JButton("Return");
returnBook.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (ch1.isSelected() || ch2.isSelected()) {
if (ch1.isSelected() && haveBook1 == true) {
String infoMessage = "Book \"Walk\" returned";
JOptionPane.showMessageDialog(null, infoMessage,
"Message: ", JOptionPane.INFORMATION_MESSAGE);
bookAvailable1++;
haveBook1 = false;
}
if (ch2.isSelected() && haveBook2 == true) {
String infoMessage = "Book \"Red and Black\" returned";
JOptionPane.showMessageDialog(null, infoMessage,
"Message: ", JOptionPane.INFORMATION_MESSAGE);
bookAvailable2++;
haveBook2 = false;
}
} else {
String infoMessage = "No book selected";
JOptionPane.showMessageDialog(null, infoMessage,
"Message: ", JOptionPane.INFORMATION_MESSAGE);
}
}
});
//System.out.println(available1);
panelChB.add(ch1);
panelChB.add(ch2);
panelChB.add(borrowBook);
panelChB.add(returnBook);
//System.out.println(available1);
panelText = new JPanel();
panelText.setLayout(new BoxLayout(panelText, BoxLayout.Y_AXIS));
bookTitle = new JLabel("Walk - stock " + bookAvailable1);
bookTitle1 = new JLabel("Red and Black - stock " + bookAvailable2);
panelText.add(bookTitle);
panelText.add(bookTitle1);
add(panelChB, BorderLayout.WEST);
add(panelText, BorderLayout.CENTER);
}
public static void main(String[] args) {
JFrame jf = new MyFrame();
}
}
答案 0 :(得分:1)
变量执行更新,但这不会影响屏幕上的标签。到bookAvailable1--;
执行时,标签已经初始化,并且不会再自动更改。
替换它:
bookTitle = new JLabel("Walk - stock " + bookAvailable1);
bookTitle1 = new JLabel("Red and Black - stock " + bookAvailable2);
使用:
bookTitle = new JLabel();
bookTitle1 = new JLabel();
updateStockDisplay();
并添加一个新方法:
private void updateStockDisplay() {
bookTitle.setText("Walk - stock " + bookAvailable1);
bookTitle1.setText("Red and Black - stock " + bookAvailable2);
}
现在,在每个ActionListener的末尾为借用/返回按钮添加对updateStockDisplay();
的调用,以便每当变量发生变化时都会更新标签。
答案 1 :(得分:0)
您的hasBook2和hasBook1变量为false,因此它不会输入if部分。最后做setVisible(true)。