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

📄 searchdialog.java~3~

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

import javax.swing.ButtonGroup;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.BorderLayout;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.event.ActionListener;
import javax.swing.JRadioButton;
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;

public class SearchDialog extends JDialog implements ActionListener {
    public final int OK = 1;
    public final int CANCEL = -1;
    public final int CLOSE = 0;
    private int actionCode = CANCEL;
    private JRadioButton radID = new JRadioButton("  图书编号  ");
    private JTextField txtID = new JTextField();
    private JRadioButton radBookName = new JRadioButton("  图书名称  ");
    private JTextField txtBookName = new JTextField();
    private JRadioButton radBookPress = new JRadioButton("  出  版  社  ");
    private JTextField txtBookPress = new JTextField();
    private JButton btnOk = new JButton("确定(Yes)");
    private JButton btnCancel = new JButton("取消(Esc)");
    private JFrame f = null;
    public SearchDialog(JFrame f, String s, boolean b) {
        super(f, s, b);
        this.f = f;
        ButtonGroup group = new ButtonGroup();
        group.add(radID);
        group.add(radBookName);
        group.add(radBookPress);
        txtID.setEditable(false);
        radBookName.setSelected(true);
        txtBookPress.setEditable(false);
        radID.addActionListener(this);
        radBookName.addActionListener(this);
        radBookPress.addActionListener(this);
        //面板p中显示查询条件
        JPanel p = new JPanel();
        p.setLayout(new GridBagLayout());
        LayoutUtil.add(p, GridBagConstraints.NONE,
                       GridBagConstraints.CENTER, 0, 0, 0, 0, 1, 1, radID);
        LayoutUtil.add(p, GridBagConstraints.HORIZONTAL,
                       GridBagConstraints.CENTER, 100, 0, 1, 0, 1, 1, txtID);
        LayoutUtil.add(p, GridBagConstraints.NONE,
                       GridBagConstraints.WEST, 0, 0, 0, 1, 1, 1, radBookName);
        LayoutUtil.add(p, GridBagConstraints.HORIZONTAL,
                       GridBagConstraints.CENTER, 100, 0, 1, 1, 1, 1,
                       txtBookName);
        LayoutUtil.add(p, GridBagConstraints.NONE,
                       GridBagConstraints.CENTER, 0, 0, 0, 2, 1, 1,
                       radBookPress);
        LayoutUtil.add(p, GridBagConstraints.HORIZONTAL,
                       GridBagConstraints.CENTER, 100, 0, 1, 2, 1, 1,
                       txtBookPress);

        btnOk.setIcon(new ImageIcon(this.getClass().getResource(
                "image/ok20.gif")));
        btnCancel.setIcon(new ImageIcon(this.getClass().getResource(
                "image/cancel20.gif")));
        btnOk.addActionListener(this);
        btnCancel.addActionListener(this);

        //面板ap中显示确定 取消按钮
        JPanel ap = new JPanel();
        ap.add(btnOk);
        ap.add(btnCancel);
        getContentPane().add(p, BorderLayout.CENTER);
        getContentPane().add(ap, BorderLayout.SOUTH);
        setSizeAndPosition(300, 200);
        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 setTxtEditable(boolean b1, boolean b2, boolean b3) {
        txtID.setEditable(b1);
        txtBookName.setEditable(b2);
        txtBookPress.setEditable(b3);
    }

    public void actionPerformed(ActionEvent e) {
        Object s = e.getSource();
        if (s == this.radID) {
            setTxtEditable(true, false, false);
        } else if (s == this.radBookName) {
            setTxtEditable(false, true, false);
        } else if (s == this.radBookPress) {
            setTxtEditable(false, false, true);
        } else if (s == this.btnOk) {
            this.actionCode = this.OK;
            this.setVisible(false);
        } else if (s == this.btnCancel) {
            this.actionCode = this.CANCEL;
            this.setVisible(false);
        }
    }

    public int getActionCode() {
        return actionCode;
    }

    public String getSQL() {
        String sql = "select * from bookInfo where ";
        if (radID.isSelected()) {
            sql = sql + " bookID like '%" + txtID.getText().trim() + "%'";
        } else if (radBookName.isSelected()) {
            sql = sql + " bookName like '%" + txtBookName.getText().trim() +
                  "%'";
        } else if (radBookPress.isSelected()) {
            sql = sql + " bookPress like '%" + txtBookPress.getText().trim() +
                  "%'";
        }
        return sql;
    }
}

⌨️ 快捷键说明

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