很抱歉这样一般的问题,但我无法弄清楚我哪里出错了。 jbuttons:提交,清除和退出不起作用。我是否需要将actionListeners放在其他位置?
此外,有人可以检查我的代码是否与我的提交按钮一起使用submitAction()
?这几乎是最后一种方法。它应该从用户提交书信息(isbn& author)并将其保存到book对象。我希望能够通过isbn或作者LATER进行搜索,因此我需要确保该程序将在书中单独识别它们。感谢您的帮助!
package library.presentation;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import library.domain.Book;
public class AddBooks extends JFrame {
public static void main(String args[])
{
java.awt.EventQueue.invokeLater(() -> {
new AddBooks().setVisible(true);
});
}
private static final long serialVersionUID = 1L;
private JPanel northPanel = new JPanel(); //north panel
private JLabel northLabel = new JLabel(""); //north label
private JPanel centerPanel = new JPanel(); //center panel
private JPanel bookLabelPanel = new JPanel(); //internal in center panel
private JLabel[] bookLabel = new JLabel[2]; //create array JLabel
private String[] bookString = { //create String array
" ISBN: ", " Author(s): "
};
private JPanel bookTextFieldPanel = new JPanel(); //create internal center panel
private JTextField[] bookTextField = new JTextField[2]; //create array JTextField
private JPanel submitButtonPanel = new JPanel(); //create internal center panel
private JButton submitButton = new JButton("Submit"); //create submit button
private JPanel southPanel = new JPanel(); //create south panel
private JButton clearButton = new JButton("Clear");
private JButton exitButton = new JButton("Exit"); //create exit button
private Book book; //create object from other class in actionlistener
private String[] data; //create string array to store data
public boolean isCorrect() //to check book information in text field
{
data = new String[2];
for (int i = 0; i < bookLabel.length; i++) {
if (!bookTextField[i].getText().equals("")) {
data[i] = bookTextField[i].getText();
}
else {
return false;
}
}
return true;
}
public void setText()
{
int i = 0;
bookTextField[i].setText(null);
}
//constructor of addBooks
public AddBooks()
{
super("Add Books"); //title - internal frame
Container container = getContentPane(); //GUI components display area
northPanel.setLayout(new FlowLayout(FlowLayout.CENTER)); //set layout
northLabel.setFont(new Font("Book Antigua", Font.BOLD, 16)); //set font - north panel
northPanel.add(northLabel); //add label - north panel
container.add("North", northPanel); //add north panel to container
centerPanel.setLayout(new BorderLayout()); //set layout
centerPanel.setBorder(BorderFactory.createTitledBorder("Add a new book:")); //set border panel
bookLabelPanel.setLayout(new GridLayout(11, 1, 1, 1)); //set layout
for (int i = 0; i < bookLabel.length; i++) { // oop to add strings to labels, set font, add labels to panel
bookLabelPanel.add(bookLabel[i] = new JLabel(bookString[i]));
bookLabel[i].setFont(new Font("Book Antigua", Font.BOLD, 12));
}
centerPanel.add("West", bookLabelPanel); //set layout
bookTextFieldPanel.setLayout(new GridLayout(11, 1, 1, 1)); //set layout
for (int i = 0; i < bookTextField.length; i++) { //loop to add string to labels, set font, add labels to panel
bookTextFieldPanel.add(bookTextField[i] = new JTextField(25));
bookTextField[i].setFont(new Font("Book Antigua", Font.PLAIN, 12));
}
centerPanel.add("East", bookTextFieldPanel); //set layout
submitButtonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT)); //set layout
submitButton.setFont(new Font("Book Antigua", Font.BOLD, 12)); //set font
submitButtonPanel.add(submitButton); //add submit button
centerPanel.add("South", submitButtonPanel); //add submit button panel
container.add("Center", centerPanel); //add to container
southPanel.setLayout(new FlowLayout(FlowLayout.RIGHT)); //set layout
exitButton.setFont(new Font("Book Antigua", Font.BOLD, 12)); //set font
clearButton.setFont(new Font("Book Antigua", Font.BOLD, 12)); //set font
southPanel.add(clearButton); //add clear button
southPanel.add(exitButton); //add exit button
southPanel.setBorder(BorderFactory.createEtchedBorder()); //set border
container.add("South", southPanel); //add to container
setVisible(true);
pack();
submitButton.addActionListener(new ActionListener() {
public void submitAction(java.awt.event.ActionEvent evt)
{
try {
String isbn = null;
String author = null;
int i = 0;
Book book = new Book(isbn, author);
book.setIsbn(bookTextField[i].getText());
book.setAuthor(bookTextField[i].getText());
book.setText(isbn + "" + author);
System.out.println("You have submitted a book! " + book.toString());
// Book -> BookMgr.storeBook; //code to pass to BookMgr
} catch (Exception e) {
System.out.println("Exception:" + e);
}
}
@Override
public void actionPerformed(ActionEvent e)
{
throw new UnsupportedOperationException("Not supported yet.");
}
});
clearButton.addActionListener(new ActionListener() {
public void clearAction(java.awt.event.ActionEvent evt)
{
try {
setText();
} catch (Exception e) {
System.out.println("Exception:" + e);
}
}
@Override
public void actionPerformed(ActionEvent e)
{
throw new UnsupportedOperationException("Not supported yet.");
}
});
exitButton.addActionListener(new ActionListener() {
public void exitAction(java.awt.event.ActionEvent evt)
{
System.exit(0);
}
@Override
public void actionPerformed(ActionEvent e)
{
throw new UnsupportedOperationException("Not supported yet.");
}
});
} //end of constructor
} //end of class AddBooks
答案 0 :(得分:0)
永远不会调用方法exitAction
,clearAction
或submitAction
。点击后,系统只会调用JButton
的{{1}} ActionListener
方法,因为它可以作为点击事件的回调函数/观察者函数
这里的解决方案很简单,让相应的actionPerformed
方法只调用您创建的三种方法之一。此外,最好将三个方法移出ActionListeners,并在actionPerformed
数据上运行AddBooks
。
AddBooks