package Operations;

import Util.BasicOperation;

import javax.swing.*;
import java.awt.event.ActionEvent;

public class AddBook {
    public static void main() {
        JFrame frame = new JFrame("添加书籍");
        frame.setSize(400,400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        panel.setLayout(null);

        JButton cancel = new JButton("取消");
        cancel.setBounds(300,0,100,25);
        cancel.setContentAreaFilled(false);
        cancel.setBorder(null);
        panel.add(cancel);
        cancel.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.dispose();
            }
        });

        JLabel label = new JLabel("书名");
        label.setBounds(100,95,40,25);
        panel.add(label);

        JTextField textField = new JTextField(20);
        textField.setBounds(140,95,120,25);
        panel.add(textField);

        JLabel label1 =  new JLabel("作者");
        label1.setBounds(100,130,40,25);
        panel.add(label1);

        JTextField textField1 = new JTextField(20);
        textField1.setBounds(140,130,120,25);
        panel.add(textField1);

        // 选择框
        JLabel label2 = new JLabel("默认：文学");
        label2.setBounds(100,165,100,25);
        panel.add(label2);

        JButton addBook = new JButton("添加");
        addBook.setBounds(100,200,160,25);
        panel.add(addBook);
        addBook.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String name = textField.getText();
                String author = textField1.getText();
                String type = "文学";
                boolean isLent = false;
                BasicOperation basicOperation = new BasicOperation();
                if (basicOperation.addBook(name,author,type,isLent)) {
                    JOptionPane.showMessageDialog(panel,"添加成功");
                    frame.dispose();
                } else {
                    JOptionPane.showMessageDialog(panel,"添加失败");
                }
            }
        });

        frame.add(panel);
        frame.setVisible(true);
    }
}
