?? desknumdialog.java
字號:
package com.mwq.frame.manage;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JButton;
import javax.swing.JDialog;
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.table.DefaultTableModel;
import com.mwq.dao.Dao;
import com.mwq.dao.JDBC;
import com.mwq.mwing.MTable;
import com.mwq.tool.Validate;
public class DeskNumDialog extends JDialog {
private JTable table;
private JTextField seatingTextField;
private JTextField numTextField;
private final Vector columnNameV = new Vector();
private final DefaultTableModel tableModel = new DefaultTableModel();
private final Dao dao = Dao.getInstance();
private JTable openedDeskTable;
/**
* Launch the application
*
* @param args
*/
public static void main(String args[]) {
try {
DeskNumDialog dialog = new DeskNumDialog(null);
dialog.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the dialog
*/
public DeskNumDialog(JTable rightTable) {
super();
setModal(true);
getContentPane().setLayout(new BorderLayout());
setResizable(false);
setTitle("臺號管理");
setBounds(100, 100, 500, 375);
this.openedDeskTable = rightTable;
final JPanel operatePanel = new JPanel();
getContentPane().add(operatePanel, BorderLayout.NORTH);
final JLabel numLabel = new JLabel();
operatePanel.add(numLabel);
numLabel.setText("臺 號:");
numTextField = new JTextField();
numTextField.setColumns(6);
operatePanel.add(numTextField);
final JLabel seatingLabel = new JLabel();
operatePanel.add(seatingLabel);
seatingLabel.setText(" 座位數:");
seatingTextField = new JTextField();
seatingTextField.setColumns(5);
operatePanel.add(seatingTextField);
final JLabel topPlaceholderLabel = new JLabel();
topPlaceholderLabel.setPreferredSize(new Dimension(20, 40));
operatePanel.add(topPlaceholderLabel);
final JButton addButton = new JButton();//創建添加臺號按鈕對象
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String num = numTextField.getText().trim();// 獲取臺號,并去掉首尾空格
String seating = seatingTextField.getText().trim();// 獲取座位數,并去掉首尾空格
if (num.equals("") || seating.equals("")) {// 查看用戶是否輸入了臺號和座位數
JOptionPane.showMessageDialog(null, "請輸入臺號和座位數!", "友情提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
if (num.length() > 5) {// 查看臺號的長度是否超過了5位
JOptionPane.showMessageDialog(null, "臺號最多只能為5個字符!", "友情提示",
JOptionPane.INFORMATION_MESSAGE);
numTextField.requestFocus();// 為臺號文本框請求獲得焦點
return;
}
if (!Validate.execute("[1-9]{1}([0-9]{0,1})", seating)) {// 驗證座位數是否在1——19之間
String[] infos = { "座位數輸入錯誤!", "座位數必須在 1——99 之間!" };
JOptionPane.showMessageDialog(null, infos, "友情提示",
JOptionPane.INFORMATION_MESSAGE);
seatingTextField.requestFocus();// 為座位數文本框請求獲得焦點
return;
}
if (dao.sDeskByNum(num) != null) {// 查看該臺號是否已經存在
JOptionPane.showMessageDialog(null, "該臺號已經存在!", "友情提示",
JOptionPane.INFORMATION_MESSAGE);
numTextField.requestFocus();// 為臺號文本框請求獲得焦點
return;
}
int row = table.getRowCount();// 獲得當前擁有臺號的個數
Vector newDeskNumV = new Vector();// 創建一個代表新臺號的向量
newDeskNumV.add(new Integer(row + 1));// 添加添加序號
newDeskNumV.add(num);// 添加臺號
newDeskNumV.add(seating);// 添加座位數
tableModel.addRow(newDeskNumV);// 將新臺號信息添加到表格中
table.setRowSelectionInterval(row, row);// 設置新添加的臺號為選中的
numTextField.setText(null);// 將臺號文本框設置為空
seatingTextField.setText(null);// 將座位數文本框設置為空
//
dao.iDesk(num, seating);// 將新添加的臺號信息保存到數據庫中
JDBC.closeConnection();// 關閉數據庫連接
}
});
addButton.setText("添加");
operatePanel.add(addButton);
final JButton delButton = new JButton();//創建刪除臺號按鈕對象
delButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int selectedRow = table.getSelectedRow();// 獲得選中的餐臺
if (selectedRow == -1) {// 未選中任何餐臺
JOptionPane.showMessageDialog(null, "請選擇要刪除的餐臺!", "友情提示",
JOptionPane.INFORMATION_MESSAGE);
} else {
String deskNum = table.getValueAt(selectedRow, 1)
.toString();// 獲得選中餐臺的編號
for (int row = 0; row < openedDeskTable.getRowCount(); row++) {// 查看該餐臺是否正在被使用
if (deskNum.equals(openedDeskTable.getValueAt(row, 1))) {
JOptionPane.showMessageDialog(null,
"該餐臺正在使用,不能刪除!", "友情提示",
JOptionPane.INFORMATION_MESSAGE);
return;// 該餐臺正在被使用,不能刪除,返回
}
}
String infos[] = new String[] {// 組織確認信息
"確定要刪除餐臺:",
" 臺 號:" + deskNum,
" 座位數:"
+ table.getValueAt(selectedRow, 2)
.toString() };
int i = JOptionPane.showConfirmDialog(null, infos, "友情提示",
JOptionPane.YES_NO_OPTION);// 彈出確認提示
if (i == 0) {// 確認刪除
dao.dDeskByNum(deskNum);// 從數據庫中刪除
tableModel.setDataVector(dao.sDesk(), columnNameV);// 刷新表格
int rowCount = table.getRowCount();// 獲得刪除后擁有的餐臺數
if (rowCount > 0) {// 還擁有餐臺
if (selectedRow == rowCount)// 刪除的為最后一個餐臺
selectedRow -= 1;// 將選中的餐臺前移一行
table.setRowSelectionInterval(selectedRow,
selectedRow);// 設置當前選中的餐臺
}
JDBC.closeConnection();// 關閉數據庫連接
}
}
}
});
delButton.setText("刪除");
operatePanel.add(delButton);
final JScrollPane scrollPane = new JScrollPane();
getContentPane().add(scrollPane);
String columnNames[] = new String[] { "序 號", "臺 號", "座位數" };
for (int i = 0; i < columnNames.length; i++) {
columnNameV.add(columnNames[i]);
}
tableModel.setDataVector(dao.sDesk(), columnNameV);
JDBC.closeConnection();
table = new MTable(tableModel);
if (table.getRowCount() > 0)
table.setRowSelectionInterval(0, 0);
scrollPane.setViewportView(table);
final JLabel leftPlaceholderLabel = new JLabel();
leftPlaceholderLabel.setPreferredSize(new Dimension(20, 20));
getContentPane().add(leftPlaceholderLabel, BorderLayout.WEST);
final JPanel exitPanel = new JPanel();
final FlowLayout flowLayout = new FlowLayout();
flowLayout.setAlignment(FlowLayout.RIGHT);
exitPanel.setLayout(flowLayout);
getContentPane().add(exitPanel, BorderLayout.SOUTH);
final JButton exitButton = new JButton();
exitPanel.add(exitButton);
exitButton.setText("退出");
exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
});
final JLabel bottomPlaceholderLabel = new JLabel();
bottomPlaceholderLabel.setPreferredSize(new Dimension(10, 40));
exitPanel.add(bottomPlaceholderLabel);
final JLabel rightPlaceholderLabel = new JLabel();
rightPlaceholderLabel.setPreferredSize(new Dimension(20, 20));
getContentPane().add(rightPlaceholderLabel, BorderLayout.EAST);
//
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -