我有一个带有 addButton 的 JFrame。
当用户单击按钮时,必须显示 AddDialog 窗口,但我看不到它并且无法添加行
这是我的代码:
public class AddDialogS extends JDialog{
BookInformation bookinform=new BookInformation();
public AddDialogS(JFrame owner){
super(owner,"Add New Book!", true);
JButton OkButton=new JButton("Ok");
final JTextField nameTF=new JTextField(10);
final JTextField dateTF=new JTextField(10);
final JTextField idTF=new JTextField(10);
OkButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
bookinform.setBookName(nameTF.getText());
bookinform.setBookDate(dateTF.getText());
bookinform.setBookID(idTF.getText());
AddDialogS.this.dispose();
}
});
JPanel panel=new JPanel(new FlowLayout());
panel.add(OkButton);
panel.add(nameTF);
panel.add(dateTF);
panel.add(idTF);
}
public BookInformation getBookInfos(){
return bookinform;
}
}
我的主要课程:
public class MainS extends JFrame{
final AllBooks allBooks=new AllBooks();
final JTable Btable=new JTable(allBooks);
public MainS(){
JButton AddBookButton=new JButton("Add New Book");
AddBookButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
AddDialogS adddialog=new AddDialogS(MainS.this);
BookInformation B_info=adddialog.getBookInfos();
if(B_info != null){
allBooks.AddRow(B_info);
}
}
});
JPanel Bpanel=new JPanel();
Bpanel.setLayout(new FlowLayout());
JScrollPane sp=new JScrollPane(Btable);
Bpanel.add(sp);
Bpanel.add(AddBookButton);
this.add(Bpanel);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(300, 60, 550, 550);
this.setVisible(true);
}
public static void main(String[] args){
new MainS();
}
}
它将向 jtable 添加一个新行。