我正在使用GUI实现的不同方法开发一个库系统类。
我成功地创建了所有文本字段和按钮等。
现在,问题是按钮不起作用。
要么在创建这两个类的对象时都有问题,要么在从TabbedPane中获取操作事件时存在一些我无法理解的不同之处。
这是我的代码示例。没有把所有的代码都放进去,因为太长了。
公共类RunProgram扩展JFrame实现ActionListener{
private static final int WIDTH = 400;
private static final int HEIGHT = 430;
private static final int NUMBER_OF_DIGITS = 30;
private long accessionNumber = 1001;
LibrarySystem ITLib = new LibrarySystem(); //object of LibrarySystem class to be able to use methods in actionListener method.
公共RunProgram(){//构造函数
setTitle("Welcome to Library");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(WIDTH, HEIGHT);
setLayout(new BorderLayout());
Container container = getContentPane();
JTabbedPane Pane = new JTabbedPane();
........
public void actionPerformed(ActionEvent e){
String actionCommand = e.getActionCommand();
switch (actionCommand) {
case "Add Book":
String t = title.getText();
String a1 = author1.getText();
String a2 = author2.getText();
String p = publisher.getText();
int py = stringToInt(yop.getText());
String isbn = ISBN.getText();
Book newBook = new Book(t, a1, a2, p, py, isbn, accessionNumber);
if (ITLib.addBook(newBook)) {
title.setText("Book added successfully.");
aNum.setText(Long.toString(accessionNumber));
accessionNumber++;
} else
title.setText("Book already exists!");
break;
case "Delete Book": {
long an = stringToLong(aNum.getText());
if (ITLib.deleteBook(an))
aNum.setText("Book deleted successfully.");
else
aNum.setText("Book doesn't exist!");
break; }
public static void main(String[] args) {
RunProgram RLibrary = new RunProgram();
RLibrary.setVisible(true);
}
请帮忙。(附:我只是一个初学者,我们在保护代码方面做得不多,所以大多数编码都是直接方法。)