gpt4 book ai didi

java - 变量在 if 语句之外未更新

转载 作者:行者123 更新时间:2023-12-01 13:25:48 24 4
gpt4 key购买 nike

有人可以解释一下,为什么单击“borrowBook”或“returnBook”按钮后变量“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();
}
}

最佳答案

变量确实更新,但这不会影响屏幕上的标签。当 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(); 的调用,以便每当变量发生更改时都会更新标签。

关于java - 变量在 if 语句之外未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21813048/

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