📄 operationmodel.java
字号:
package GraphicsUI;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
public class OperationModel implements ActionListener {
JFrame jf;
JLabel pid;
JLabel id;
JLabel namel;
JTextField namet;
JButton btn;
JButton exit;
JLabel ctype;
CheckboxGroup c;
JPanel cp;
JPanel cpb;
Checkbox c1;
Checkbox c2;
Checkbox c3;
JLabel gtype;
CheckboxGroup g;
JPanel gp;
JPanel gpb;
Checkbox g1;
Checkbox g2;
Checkbox g3;
Toolkit kit;
Dimension windowSize;
JPanel west;
JPanel east;
JPanel f1;
JPanel f2;
String[] identity = { "证件类型", "身份证", "学生证", "驾驶证", "护照", "军官证" };
JComboBox jcb;
JTextField idjtf;
JLabel ticket;
JPanel jbtn;
Object temp;
Connection conn;
Statement stmt;
public OperationModel(Object t) {
temp = t;
kit = Toolkit.getDefaultToolkit();
windowSize = kit.getScreenSize();
ticket = new JLabel("机票选择");
ticket.setHorizontalAlignment(SwingConstants.CENTER);
ticket.setFont(new Font("", Font.PLAIN, 15));
jf = new JFrame();
jf.setResizable(false);
pid = new JLabel("航班号:");
pid.setHorizontalAlignment(SwingConstants.CENTER);
namel = new JLabel("乘客姓名:");
namel.setHorizontalAlignment(SwingConstants.CENTER);
namet = new JTextField(10);
c = new CheckboxGroup();
ctype = new JLabel("机票类型");
g1 = new Checkbox("儿童票", c, false);
g2 = new Checkbox("成人票", c, true);
g3 = new Checkbox("折扣票", c, false);
g = new CheckboxGroup();
gtype = new JLabel("机舱类型");
c1 = new Checkbox("特等舱", g, false);
c2 = new Checkbox("公务舱", g, false);
c3 = new Checkbox("经济舱", g, true);
btn = new JButton("");
btn.addActionListener(this);
exit = new JButton("退出");
jf.setBounds(windowSize.width / 2 - 300, windowSize.height / 2 - 100,
600, 200);
gp = new JPanel();
gp.setLayout(new GridLayout(3, 1));
gp.add(c1);
gp.add(c2);
gp.add(c3);
gpb = new JPanel();
gpb.setLayout(new FlowLayout());
gpb.add(gtype);
gpb.add(gp);
cp = new JPanel();
cp.setLayout(new GridLayout(3, 1));
cp.add(g1);
cp.add(g2);
cp.add(g3);
cpb = new JPanel();
cpb.setLayout(new FlowLayout());
cpb.add(ctype);
cpb.add(cp);
f1 = new JPanel();
f1.setLayout(new FlowLayout());
f2 = new JPanel();
f2.setLayout(new FlowLayout());
f1.add(namel);
f1.add(namet);
f2.add(gpb);
f2.add(cpb);
id = new JLabel("");
west = new JPanel();
idjtf = new JTextField(10);
jcb = new JComboBox(identity);
west.setLayout(new GridLayout(3, 2));
west.add(pid);
west.add(id);
west.add(namel);
west.add(namet);
west.add(jcb);
west.add(idjtf);
jf.add(west, BorderLayout.CENTER);
east = new JPanel();
east.setLayout(new BorderLayout());
east.add(ticket, BorderLayout.NORTH);
east.add(f1, BorderLayout.WEST);
east.add(f2, BorderLayout.EAST);
jf.add(east, BorderLayout.EAST);
jbtn = new JPanel();
// jbtn.setLayout(new FlowLayout());
jbtn.add(btn);
jbtn.add(exit);
jf.add(jbtn, BorderLayout.SOUTH);
jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
exit.addActionListener(this);
jf.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
OrderOperation.flag = false;
ReturnOperation.flag = false;
jf.dispose();
}
});
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == exit) {
OrderOperation.flag = false;
ReturnOperation.flag = false;
jf.dispose();
}
if (e.getSource() == btn) {
if (btn.getText().equals("订票")) {
try {
// int t = 0;
System.out.println("btn is OK");
conn = DriverManager.getConnection(
"jdbc:microsoft:sqlserver://localhost:1433", "sa",
"");
stmt = conn.createStatement();
System.out.println(c.getSelectedCheckbox().getLabel());
System.out.println(g.getSelectedCheckbox().getLabel());
System.out.println(jcb.getSelectedItem());
System.out.println(namet.getText().trim());
System.out.println(id.getText());
int rs = stmt
.executeUpdate("insert into ordersituation values ('"
+ namet.getText().trim()
+ "','"
+ jcb.getSelectedItem()
+ "','"
+ idjtf.getText()
+ "','"
+ c.getSelectedCheckbox().getLabel()
+ "','"
+ g.getSelectedCheckbox().getLabel()
+ "','" + id.getText() + "')");
if (rs != 0) {
JOptionPane.showMessageDialog(null, "订票成功", "提示",
JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null, "航班号:\t"
+ id.getText() + "\n" + "姓名:\t"
+ namet.getText() + "\n" + "证件类型:\t"
+ jcb.getSelectedItem() + "\n" + "证件号:\t"
+ idjtf.getText() + "\n" + "舱型:\t"
+ c.getSelectedCheckbox().getLabel() + "\n"
+ "票型:\t" + g.getSelectedCheckbox().getLabel()
+ "\n", "成功", JOptionPane.INFORMATION_MESSAGE);
} else
JOptionPane.showMessageDialog(null, "订票失败", "错误",
JOptionPane.ERROR_MESSAGE);
} catch (SQLException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}
}
if (btn.getText().equals("退票")) {
try {
// int t = 0;
System.out.println("btn is OK");
conn = DriverManager.getConnection(
"jdbc:microsoft:sqlserver://localhost:1433", "sa",
"");
stmt = conn.createStatement();
System.out.println(c.getSelectedCheckbox().getLabel());
System.out.println(g.getSelectedCheckbox().getLabel());
System.out.println(jcb.getSelectedItem());
System.out.println(namet.getText().trim());
System.out.println(id.getText());
int rs = stmt
.executeUpdate("delete from ordersituation where idtype='"
+ jcb.getSelectedItem()
+ "' and id = '"
+ idjtf.getText() + "'");
if (rs != 0) {
JOptionPane.showMessageDialog(null, "退票成功", "提示",
JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null, "航班号:\t"
+ id.getText() + "\n" + "姓名:\t"
+ namet.getText() + "\n" + "证件类型:\t"
+ jcb.getSelectedItem() + "\n" + "证件号:\t"
+ idjtf.getText() + "\n" + "舱型:\t"
+ c.getSelectedCheckbox().getLabel() + "\n"
+ "票型:\t" + g.getSelectedCheckbox().getLabel()
+ "\n", "成功", JOptionPane.INFORMATION_MESSAGE);
} else
JOptionPane.showMessageDialog(null, "退票失败", "错误",
JOptionPane.ERROR_MESSAGE);
} catch (SQLException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -