📄 publishermanage.java
字号:
package com.lib.gui;
import java.awt.BorderLayout;
import java.util.Iterator;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import com.lib.Main;
import com.lib.db.Book;
import com.lib.db.Publisher;
import com.lib.db.oper.BookOper;
import com.lib.db.oper.PublisherOper;
public class PublisherManage extends JFrame {
private JPanel jContentPane = null;
private JToolBar jToolBar = null;
private JTextField jTextField_id = null;
private JToolBar jToolBar_id = null;
private JToolBar jToolBar_publisher = null;
private JLabel jLabel_publisher = null;
private JTextField jTextField_publisher = null;
private JToolBar jToolBar_control = null;
private JButton jButton_clear = null;
private JButton jButton_edit = null;
private JButton jButton_add = null;
private JButton jButton_del = null;
private JLabel jLabel_id = null;
private JTable jTable = null;
private JPanel jPanel = null;
private JScrollPane jScrollPane = null;
private void search() {
TableModel model = getJTable().getModel();
DefaultTableModel tablemodel = (DefaultTableModel) model;
Iterator iter = PublisherOper.getInfo().iterator();
int counts = tablemodel.getRowCount();
for (int i = counts - 1; i >= 0; i--) {
tablemodel.removeRow(i);
}
while (iter.hasNext()) {
Publisher value = (Publisher) iter.next();
Object[] obj = { value.getPublisher_id(), value.getPublisher() };
tablemodel.addRow(obj);
}
}
/**
* This is the default constructor
*/
public PublisherManage() {
super();
initialize();
}
public static void main(String args[]) {
PublisherManage publisherManage = new PublisherManage();
}
/**
* This method initializes this
*
* @return void
*/
public Main main = null;
private void initialize() {
this.setSize(640, 480);
this.setContentPane(getJContentPane());
this.setTitle("出版社维护");
this.setLocation(200, 50);
this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
//DBConnection.closeConnection();
if (main != null) {
main.setVisible(true);
}
}
});
search();
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getJToolBar(), java.awt.BorderLayout.NORTH);
jContentPane.add(getJPanel(), java.awt.BorderLayout.CENTER);
}
return jContentPane;
}
/**
* This method initializes jToolBar
*
* @return javax.swing.JToolBar
*/
private JToolBar getJToolBar() {
if (jToolBar == null) {
jToolBar = new JToolBar();
jToolBar.setFloatable(false);
// jToolBar.setOrientation(javax.swing.JToolBar.VERTICAL);
jToolBar.add(getJToolBar_id());
jToolBar.add(getJToolBar_publisher());
jToolBar.add(getJToolBar_control());
}
return jToolBar;
}
/**
* This method initializes jTextField_id
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField_id() {
if (jTextField_id == null) {
jTextField_id = new JTextField();
jTextField_id.setEditable(false);
}
return jTextField_id;
}
/**
* This method initializes jToolBar_id
*
* @return javax.swing.JToolBar
*/
private JToolBar getJToolBar_id() {
if (jToolBar_id == null) {
jLabel_id = new JLabel();
jLabel_id.setText("编号");
jToolBar_id = new JToolBar();
jToolBar_id.setFloatable(false);
jToolBar_id.add(jLabel_id);
jToolBar_id.add(getJTextField_id());
}
return jToolBar_id;
}
/**
* This method initializes jToolBar_publisher
*
* @return javax.swing.JToolBar
*/
private JToolBar getJToolBar_publisher() {
if (jToolBar_publisher == null) {
jLabel_publisher = new JLabel();
jLabel_publisher.setText("出版社");
jToolBar_publisher = new JToolBar();
jToolBar_publisher.add(jLabel_publisher);
jToolBar_publisher.setFloatable(false);
jToolBar_publisher.add(getJTextField_publisher());
}
return jToolBar_publisher;
}
/**
* This method initializes jTextField_publisher
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField_publisher() {
if (jTextField_publisher == null) {
jTextField_publisher = new JTextField();
}
return jTextField_publisher;
}
/**
* This method initializes jToolBar_control
*
* @return javax.swing.JToolBar
*/
private JToolBar getJToolBar_control() {
if (jToolBar_control == null) {
jToolBar_control = new JToolBar();
jToolBar_control.setFloatable(false);
jToolBar_control.add(getJButton_clear());
jToolBar_control.add(getJButton_add());
jToolBar_control.add(getJButton_edit());
jToolBar_control.add(getJButton_del());
}
return jToolBar_control;
}
/**
* This method initializes jButton_clear
*
* @return javax.swing.JButton
*/
private JButton getJButton_clear() {
if (jButton_clear == null) {
jButton_clear = new JButton();
jButton_clear.setText("清空");
jButton_clear.setIcon(new ImageIcon("E:/img/standard_094_20.png"));
jButton_clear
.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
jTextField_id.setText(null);
jTextField_publisher.setText(null);
}
});
}
return jButton_clear;
}
// 检查编号文本框是否为空,说明是否已选择了一条记录
private boolean checkEmpty() {
String id = getJTextField_id().getText().trim();
if (id.isEmpty()) {
JOptionPane.showMessageDialog(null, "请选择一条出版社记录来进行操作!!");
return false;
}
return true;
}
//检查是否可删除
private boolean checkDeleteable() {
String id = getJTextField_id().getText().trim();
if (!BookOper.queryPublisher(id).isEmpty()) {
JOptionPane.showMessageDialog(null, "出版社的删除只有其所属图书都已删除的情况下才可以!!");
return false;
}
return true;
}
/**
* This method initializes jButton_edit
*
* @return javax.swing.JButton
*/
private JButton getJButton_edit() {
if (jButton_edit == null) {
jButton_edit = new JButton();
jButton_edit.setText("修改");
jButton_edit.setIcon(new ImageIcon("E:/img/standard_138_20.png"));
jButton_edit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
if (checkEmpty()) {
int ch = JOptionPane.showConfirmDialog(null,
"你真的要修改该纪录?", "修改数据",
JOptionPane.YES_NO_OPTION);
if (ch == JOptionPane.YES_OPTION) {
Publisher value = getIputText();
if (PublisherOper.update(value)) {
JOptionPane.showMessageDialog(null, "修改成功");
search();
} else {
JOptionPane.showMessageDialog(null, "数据修改失败");
}
}
}
}
});
}
return jButton_edit;
}
/**
* This method initializes jButton_add
*
* @return javax.swing.JButton
*/
private JButton getJButton_add() {
if (jButton_add == null) {
jButton_add = new JButton();
jButton_add.setText("添加");
jButton_add.setIcon(new ImageIcon("E:/img/standard_159_20.png"));
jButton_add.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
Publisher value = getIputText();
//System.out.println(value);
int ch = JOptionPane.showConfirmDialog(null, "你真的要添加该纪录?",
"添加数据", JOptionPane.YES_NO_OPTION);
if (ch == JOptionPane.YES_OPTION) {
if (PublisherOper.add(value)) {
JOptionPane.showMessageDialog(null, "添加成功");
search();
} else {
JOptionPane.showMessageDialog(null, "添加失败!");
}
}
}
});
}
return jButton_add;
}
private Publisher p;
private Publisher getIputText() {
String id = getJTextField_id().getText().trim();
String publisher = getJTextField_publisher().getText().trim();
p = new Publisher(id, publisher);
return p;
}
/**
* This method initializes jButton_del
*
* @return javax.swing.JButton
*/
private JButton getJButton_del() {
if (jButton_del == null) {
jButton_del = new JButton();
jButton_del.setText("删除");
jButton_del.setIcon(new ImageIcon("E:/img/standard_127_20.png"));
jButton_del.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
if (checkEmpty() && checkDeleteable()) {
//出版社的删除只有其所属图书都已删除的情况下才可以!!!
PublisherOper db = new PublisherOper();
String[] names = getJTableValue();
//System.out.println(names[0]);
int ch = JOptionPane.showConfirmDialog(null,
"你真的要删除该记录?", "删除数据",
JOptionPane.YES_NO_OPTION);
if (ch == JOptionPane.YES_OPTION) {
if (db.delete(names[0])) {
JOptionPane.showMessageDialog(null, "数据删除成功");
// row = getJTable().getSelectedRow();
// TableModel td = getJTable().getModel();
// DefaultTableModel model = (DefaultTableModel)
// td;
// model.removeRow(row);
search();
} else {
JOptionPane.showMessageDialog(null, "数据删除失败");
}
}
}
}
});
}
return jButton_del;
}
/**
* This method initializes jTable
*
* @return javax.swing.JTable
*/
private JTable getJTable() {
if (jTable == null) {
Object[][] data = {};
String[] column = { "编号", "出版社" };
DefaultTableModel model = new DefaultTableModel(data, column);
jTable = new JTable(model);
jTable.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent e) {
String[] value1 = getJTableValue();
jTextField_id.setText(value1[0]);
jTextField_publisher.setText(value1[1]);
}
});
}
return jTable;
}
int row = 0;
public String[] getJTableValue() {
row = getJTable().getSelectedRow();
//System.out.println("row " + row);
int columns = getJTable().getColumnCount();
String[] data = new String[2];
data[0] = getJTable().getValueAt(row, 0).toString();
Iterator iter = PublisherOper.query(data[0]).iterator();
while (iter.hasNext()) {
Publisher value = (Publisher) iter.next();
data[1] = value.getPublisher();
}
return data;
}
/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
if (jPanel == null) {
jPanel = new JPanel();
jPanel.add(getJScrollPane(), null);
}
return jPanel;
}
/**
* This method initializes jScrollPane
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setViewportView(getJTable());
}
return jScrollPane;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -