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

📄 bookeditor.java~2~

📁 一个简单图书管理系统(非常适合于初学者)
💻 JAVA~2~
字号:
package edu.njust.cs;

import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.BorderLayout;
import javax.swing.JOptionPane;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import javax.swing.JDialog;
import javax.swing.JTextField;
import java.awt.event.WindowEvent;
import java.awt.GridBagLayout;
import java.awt.Point;
import javax.swing.JLabel;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
//内部类,用于显示/编辑图书信息的一个模式对话框
  public class BookEditor extends JDialog implements ActionListener {
       final int OK = 1;
       final int CANCEL = -1;
       final int CLOSE = 0;
       private int actionCode = CANCEL;
       private JLabel labID = new JLabel("  图书编号  ");
       private JTextField txtID = new JTextField();
       private JLabel labBookName = new JLabel("  图书名称  ");
       private JTextField txtBookName = new JTextField();
       private JLabel labBookPrice = new JLabel("  单        价  ");
       private JTextField txtBookPrice = new JTextField();
       private JLabel labBookPress = new JLabel("    出  版  社    ");
       private JTextField txtBookPress = new JTextField();
       private JButton btnOk = new JButton("确定");
       private JButton btnCancel = new JButton("取消");
       private JFrame f = null;
       public BookEditor(JFrame f, String s, boolean b) {
           super(f, s, b);
           this.f = f;
           //面板p中显示图书信息
           JPanel p = new JPanel();
           p.setLayout(new GridBagLayout());
           LayoutUtil.add(p, GridBagConstraints.NONE,
                          GridBagConstraints.CENTER, 0, 0, 0, 0, 1, 1, labID);
           LayoutUtil.add(p, GridBagConstraints.HORIZONTAL,
                          GridBagConstraints.CENTER, 100, 0, 1, 0, 1, 1, txtID);
           LayoutUtil.add(p, GridBagConstraints.NONE,
                          GridBagConstraints.CENTER, 0, 0, 2, 0, 1, 1,
                          labBookName);
           LayoutUtil.add(p, GridBagConstraints.HORIZONTAL,
                          GridBagConstraints.CENTER, 100, 0, 3, 0, 1, 1,
                          txtBookName);
           LayoutUtil.add(p, GridBagConstraints.NONE,
                          GridBagConstraints.CENTER, 0, 0, 0, 1, 1, 1,
                          labBookPrice);
           LayoutUtil.add(p, GridBagConstraints.HORIZONTAL,
                          GridBagConstraints.CENTER, 100, 0, 1, 1, 1, 1,
                          txtBookPrice);
           LayoutUtil.add(p, GridBagConstraints.NONE,
                          GridBagConstraints.CENTER, 0, 0, 2, 1, 1, 1,
                          labBookPress);
           LayoutUtil.add(p, GridBagConstraints.HORIZONTAL,
                          GridBagConstraints.CENTER, 100, 0, 3, 1, 1, 1,
                          txtBookPress);
           //注册事件侦听器
           btnOk.addActionListener(this);
           btnCancel.addActionListener(this);
           btnOk.setIcon(new ImageIcon("/image/ok20.gif"));
           btnCancel.setIcon(new ImageIcon("/image/cancel20.gif"));

           //面板ap中显示4个按钮
           JPanel ap = new JPanel();
           ap.setLayout(new GridBagLayout());

           LayoutUtil.add(ap, GridBagConstraints.HORIZONTAL,
                          GridBagConstraints.CENTER, 100, 0, 2, 0, 1, 1,
                          new JLabel());
           LayoutUtil.add(ap, GridBagConstraints.NONE,
                          GridBagConstraints.CENTER, 0, 0, 3, 0, 1, 1,
                          this.btnOk);
           LayoutUtil.add(ap, GridBagConstraints.NONE,
                          GridBagConstraints.CENTER, 0, 0, 4, 0, 1, 1,
                          this.btnCancel);
           getContentPane().add(p, BorderLayout.CENTER);
           getContentPane().add(ap, BorderLayout.SOUTH);
           //将对话框窗口定位在父窗口的居中位置
           setSizeAndPosition(550, 120);
           this.addWindowListener(new WindowAdapter() {
               public void windowClosing(WindowEvent e) {
                   actionCode = CLOSE;
               }
           });
       }

       //将对话框窗口定位在父窗口的居中位置
       public void setSizeAndPosition(int w, int h) {
           this.setSize(w, h);
           Dimension d = f.getSize();
           Point pp = f.getLocation();
           this.setLocation(pp.x + (d.width - w) / 2,
                            pp.y + (d.height - h) / 2);
       }

       public void actionPerformed(ActionEvent e) {
           Object s = e.getSource();
           if (s == this.btnCancel) {
               this.actionCode = this.CANCEL;
               this.setVisible(false);
           } else if (s == this.btnOk) {
               okClicked();
           }
       }

       public String getID() {
           return txtID.getText().trim();
       }

       public String getBookName() {
           return txtBookName.getText().trim();
       }

       //将图书单价由字符串类型转化为Double类型
       //如果不是合法的数值字符串,取值Double.NEGATIVE_INFINITY
       public Double getBookPrice() {
           double result = Double.NEGATIVE_INFINITY;
           try {
               result = Double.parseDouble(this.txtBookPrice.getText().trim());
           } catch (Exception e) {
               result = Double.NEGATIVE_INFINITY;
           }
           return new Double(result);
       }

       public String getBookPress() {
           return txtBookPress.getText().trim();
       }

       public int getActionCode() {
           return this.actionCode;
       }

       public void okClicked() {
           //图书编号不为空,且价格为数值类型时 隐藏对话框
           if (!this.txtID.getText().trim().equals("")) {
               try {
                   Double.parseDouble(this.txtBookPrice.getText().trim());
               } catch (Exception e) {
                   JOptionPane.showMessageDialog(f, "价格非法!", "提示",
                                                 JOptionPane.
                                                 INFORMATION_MESSAGE);
                   return;
               }
               this.actionCode = this.OK;
               this.setVisible(false);
           } else {
               JOptionPane.showMessageDialog(f, "请设定图书编号!", "提示",
                                             JOptionPane.INFORMATION_MESSAGE);
           }

       }
   }

⌨️ 快捷键说明

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