?? sortdialog.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.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Vector;
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;
public class SortDialog extends JDialog {
private JTable table;
private JTextField sortNameTextField;
private final Vector columnNameV = new Vector();
private final DefaultTableModel tableModel = new DefaultTableModel();
private final Dao dao = Dao.getInstance();
/**
* Launch the application
*
* @param args
*/
public static void main(String args[]) {
try {
SortDialog dialog = new SortDialog();
dialog.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the dialog
*/
public SortDialog() {
super();
setModal(true);
getContentPane().setLayout(new BorderLayout());
setResizable(false);
setTitle("菜系管理");
setBounds(100, 100, 500, 375);
final JPanel operatePanel = new JPanel();
getContentPane().add(operatePanel, BorderLayout.NORTH);
final JLabel sortNameLabel = new JLabel();
operatePanel.add(sortNameLabel);
sortNameLabel.setText("菜系名稱:");
sortNameTextField = new JTextField();
operatePanel.add(sortNameTextField);
sortNameTextField.setColumns(20);
final JLabel topPlaceholderLabel = new JLabel();
topPlaceholderLabel.setPreferredSize(new Dimension(20, 40));
operatePanel.add(topPlaceholderLabel);
final JButton addButton = new JButton();// 創(chuàng)建添加菜系名稱按鈕對象
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String sortName = sortNameTextField.getText().trim(); // 獲得菜系名稱,并去掉首尾空格
if (sortName.equals("")) {// 查看是否輸入了菜系名稱
JOptionPane.showMessageDialog(null, "請輸入菜系名稱!", "友情提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
if (sortName.length() > 10) {// 查看菜系名稱的長度是否超過了10個漢字
JOptionPane.showMessageDialog(null, "菜系名稱最多只能為10個漢字!",
"友情提示", JOptionPane.INFORMATION_MESSAGE);
return;
}
if (dao.sSortByName(sortName).size() > 0) {// 查看該菜系名稱是否已經(jīng)存在
JOptionPane.showMessageDialog(null, "該菜系已經(jīng)存在!", "友情提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
int row = tableModel.getRowCount();// 獲得當(dāng)前擁有菜系名稱的個數(shù)
Vector newSortV = new Vector();// 創(chuàng)建一個代表新菜系名稱的向量
newSortV.add(new Integer(row + 1));// 添加序號
newSortV.add(sortName);// 添加菜系名稱
tableModel.addRow(newSortV);// 將新菜系名稱信息添加到表格中
table.setRowSelectionInterval(row, row);// 設(shè)置新添加的菜系名稱為選中的
sortNameTextField.setText(null);// 將菜系名稱文本框設(shè)置為空
//
dao.iSort(sortName);// 將新添加的菜系名稱信息保存到數(shù)據(jù)庫中
JDBC.closeConnection();// 關(guān)閉數(shù)據(jù)庫連接
}
});
addButton.setText("添加");
operatePanel.add(addButton);
final JButton delButton = new JButton();// 創(chuàng)建刪除菜系名稱按鈕對象
public void actionPerformed(ActionEvent e) {
int row = table.getSelectedRow();// 獲得選中的菜系
String delSortName = (String) table.getValueAt(row, 1);// 獲得選中的菜系名稱
int j = JOptionPane
.showConfirmDialog(null, "確定要刪除菜系“" + delSortName
+ "”?", "友情提示", JOptionPane.YES_NO_OPTION);// 彈出確認(rèn)提示
if (j == 0) {// 確認(rèn)刪除
tableModel.removeRow(row);// 從表格中移除菜系信息
int rowCount = table.getRowCount();// 獲得刪除后擁有的菜系數(shù)
if (rowCount > 0) {// 還擁有菜系
if (row < table.getRowCount()) {// 刪除的不是位于表格最后的菜系
for (int i = row; i < table.getRowCount(); i++) {
table.setValueAt(i + 1 + "", i, 0);// 修改位于刪除菜系之后的序號
}
table.setRowSelectionInterval(row, row);// 設(shè)置上移到刪除行索引的菜系為被選中
} else {// 刪除的是位于表格最后的菜系
table.setRowSelectionInterval(row - 1, row - 1);// 設(shè)置當(dāng)前位于表格最后的菜系被選中
}
}
//
dao.dSortByName(delSortName);// 從數(shù)據(jù)庫中刪除菜系
JDBC.closeConnection();// 關(guān)閉數(shù)據(jù)庫連接
}
}
});
delButton.setText("刪除");
operatePanel.add(delButton);
final JScrollPane scrollPane = new JScrollPane();
getContentPane().add(scrollPane);
columnNameV.add("序 號");
columnNameV.add("菜系名稱");
tableModel.setDataVector(dao.sSortName(), 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);
//
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -