⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 addbookframe.java

📁 本光盘包含了本书各章中出现的所有程序的源代码。 1. 如果是Java程序
💻 JAVA
字号:
package com.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.dao.BookDAO;
import com.dao.IBookDAO;
import com.domain.Book;

public class AddBookFrame extends JDialog implements ActionListener{
	
	private JLabel nameLabel = new JLabel("书名:");
	private JLabel authorLabel = new JLabel("作者:");
	private JLabel priceLabel = new JLabel("价格:");
	private JTextField nameField = new JTextField(30);
	private JTextField authorField = new JTextField(30);
	private JTextField priceField = new JTextField(30);
	private JButton okBtn = new JButton("确定");
	private JButton cancelBtn = new JButton("取消");
	
	private boolean ok = false;
	private Book book;
	
	public AddBookFrame(){
		initComponents();
		initEvents();
	}
	
	private void initComponents() {
		JPanel sp = new JPanel();
		JPanel wp = new JPanel();
		JPanel cp = new JPanel();

		sp.setLayout(new FlowLayout(FlowLayout.RIGHT));
		wp.setLayout(new GridLayout(3, 1, 0, 7));
		cp.setLayout(new GridLayout(3, 1, 0, 7));

		sp.add(okBtn);
		sp.add(cancelBtn);

		wp.add(nameLabel);
		wp.add(authorLabel);
		wp.add(priceLabel);

		cp.add(nameField);
		cp.add(authorField);
		cp.add(priceField);

		this.getContentPane().add(sp, BorderLayout.SOUTH);
		this.getContentPane().add(wp, BorderLayout.WEST);
		this.getContentPane().add(cp);
        this.setLocationRelativeTo(null);
		this.setVisible(true);
		this.pack();
	}
	
	private void initEvents() {
		okBtn.addActionListener(this);
		cancelBtn.addActionListener(this);
	}
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == okBtn) {
			String name = nameField.getText();
			if (name != null)
				name = name.trim();
			if (name == null || name.length() == 0) {
				javax.swing.JOptionPane.showMessageDialog(this, "请先输入正确的书的名称");
				nameField.requestFocus();
				return;
			}

			String author = authorField.getText();
			if (author != null)
				author = author.trim();
			if (author == null || author.length() == 0) {
				javax.swing.JOptionPane.showMessageDialog(this, "请先输入正确的书的作者");
				authorField.requestFocus();
				return;
			}

			double price = 0;
			try {
				price = Double.parseDouble(priceField.getText());
			} catch (Exception ee) {
				javax.swing.JOptionPane.showMessageDialog(this, "请先输入正确的书的价格");
				priceField.requestFocus();
				return;
			}

			book = new Book(name, author, price);
            //调用原有DAO类的Create方法
			IBookDAO bookDAO = new BookDAO();
			try {
				bookDAO.create(book);
			} catch (Exception ee) {
			}			
			System.out.println("添加成功!");
			ok = true;
		} else {
			ok = false;
		}
		this.dispose();
	}//actionPerformed
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -