package Moxing;
import java.awt.*;
import java.awt.event.*;
import java.sql.ResultSet;
import javax.swing.*;
import Shujuku.Sjk_cxkcxx;
import Shujuku.Sjk_js_tjkc;
import Shujuku.Sjk_tk;
import Shujuku.Sjk_xg;
public class Model_xkxg extends JFrame implements ActionListener,MouseListener{
JLabel kcmc,sksj,rkls;
JTextField wbk1,wbk2,wbk3;
JPanel mb1,mb2,mb3,mb4;
JLabel bq3_1,bq3_2,bq3_3,bq3_4,bq3_5,bq3_6;
JTextField wbk3_1,wbk3_2,wbk3_3,wbk3_4,wbk3_5,wbk3_6;
JButton xg,qx,cx,tj,gb,sc,kc;
JTable bg;
JScrollPane gdt;
Model_xkxx xkxx;
public Model_xkxg()
{
sstj();
biao();
bdcz();
this.setTitle("课程信息修改");
this.setSize(800,600);
this.setVisible(true);
this.setResizable(false);
int width = Toolkit.getDefaultToolkit().getScreenSize().width;//获取窗口的宽度
int height = Toolkit.getDefaultToolkit().getScreenSize().height;//获取窗口的高度
this.setLocation(width/2-320,height/2-340);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void sstj()
{
kcmc = new JLabel("课程号 ");
wbk1 = new JTextField(10);
cx = new JButton("查询");
cx.setActionCommand("cx");
cx.addActionListener(this);
cx.setOpaque(true);
cx.setBackground(Color.white); //设置按钮的背景颜色
cx.setBounds(310,0,50,30);
cx.setForeground(Color.red);
mb1 = new JPanel();
mb1.add(kcmc);mb1.add(wbk1);mb1.add(cx);
mb1.setBorder(BorderFactory.createTitledBorder("搜索条件 "));
this.add(mb1,BorderLayout.NORTH);
}
public void biao()
{
bg = new JTable();
bg.addMouseListener(this);
xkxx = new Model_xkxx();
xkxx.Cx();
bg.setModel(xkxx);
gdt = new JScrollPane(bg);
mb2 = new JPanel();
mb2.add(gdt,BorderLayout.NORTH);
mb2.setBorder(BorderFactory.createTitledBorder(""));
this.add(mb2,BorderLayout.CENTER);
}
public void bdcz()
{
bq3_1 = new JLabel("课程编号 ");
bq3_2 = new JLabel("课程名称 ");
bq3_3 = new JLabel("开学学期 ");
bq3_4 = new JLabel("学时 ");
bq3_5 = new JLabel("学分 ");
bq3_6 = new JLabel(" ");
wbk3_1 = new JTextField(10);
wbk3_2 = new JTextField(10);
wbk3_3 = new JTextField(10);
wbk3_4 = new JTextField(10);
wbk3_5 = new JTextField(10);
wbk3_6 = new JTextField(10);
JPanel mb3_1 = new JPanel();
JPanel mb3_2 = new JPanel();
JPanel mb3_3 = new JPanel();
JPanel mb3_4 = new JPanel();
JPanel mb3_5 = new JPanel();
JPanel mb3_6 = new JPanel();
mb3_1.add(bq3_1); mb3_1.add(wbk3_1);
mb3_2.add(bq3_2); mb3_2.add(wbk3_2);
mb3_3.add(bq3_3); mb3_3.add(wbk3_3);
mb3_4.add(bq3_4); mb3_4.add(wbk3_4);
mb3_5.add(bq3_5); mb3_5.add(wbk3_5);
// mb3_6.add(bq3_6); mb3_6.add(wbk3_6);
JPanel mb4_1 = new JPanel();
JPanel mb4_2 = new JPanel();
mb4 = new JPanel();
xg = new JButton("修改");
xg.setActionCommand("xg");
xg.addActionListener(this);
xg.setOpaque(true);
xg.setBackground(Color.white); //设置按钮的背景颜色
xg.setForeground(Color.red);//设置按钮上字的颜色
qx = new JButton("取消");
qx.setActionCommand("qx");
qx.addActionListener(this);
qx.setOpaque(true);
qx.setBackground(Color.white); //设置按钮的背景颜色
qx.setForeground(Color.red);
tj = new JButton("添加");
tj.setActionCommand("tj");
tj.addActionListener(this);
tj.setOpaque(true);
tj.setBackground(Color.white); //设置按钮的背景颜色
tj.setForeground(Color.red);
sc = new JButton("删除");
sc.setActionCommand("sc");
sc.addActionListener(this);
sc.setOpaque(true);
sc.setBackground(Color.white); //设置按钮的背景颜色
sc.setForeground(Color.red);
gb = new JButton("关闭");
gb.setActionCommand("gb");
gb.addActionListener(this);
gb.setOpaque(true);
gb.setBackground(Color.white); //设置按钮的背景颜色
gb.setForeground(Color.red);
kc = new JButton("查看所有课程");
kc.setActionCommand("kc");
kc.addActionListener(this);
kc.setOpaque(true);
kc.setBackground(Color.white); //设置按钮的背景颜色
kc.setForeground(Color.red);
mb4.setLayout(new GridLayout(1,2,30,40));
mb4_1.setLayout(new GridLayout(1,1,30,40));
mb4_2.setLayout(new GridLayout(1,2,30,40));
mb4_1.add(kc); mb4_1.add(tj);mb4.add(sc); mb4.add(xg);mb4_2.add(qx);mb4_2.add(gb);
mb3 = new JPanel();
mb3.setLayout(new GridLayout(3,3,20,20));
mb3.add(mb3_1); mb3.add(mb3_2); mb3.add(mb3_3);
mb3.setBorder(BorderFactory.createTitledBorder("表单操作"));
mb3.add(mb3_4); mb3.add(mb3_5); mb3.add(mb3_6);
mb3.add(mb4_1); mb3.add(mb4);mb3.add(mb4_2);
this.add(mb3,BorderLayout.SOUTH);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("kc"))
{
xkxx = new Model_xkxx();
xkxx.Cx();
bg.setModel(xkxx);
}
if(e.getActionCommand().equals("cx"))
{
String kch = wbk1.getText();
String sql = "select * from Xkxt_xk where 课程号=?";
Sjk_cxkcxx cx = new Sjk_cxkcxx();
cx.sqlyj(sql,kch);
bg.setModel(cx);
}
if(e.getActionCommand().equals("qx"))
{
wbk3_1.setText("");
wbk3_2.setText("");
wbk3_3.setText("");
wbk3_4.setText("");
wbk3_5.setText("");
//this.dispose();
}
if(e.getActionCommand().equals("gb"))
{
this.dispose();
}
if(e.getActionCommand().equals("xg"))
{
int i = this.bg.getSelectedRow();//把点击表格的第几列复制给i
if(i == -1)
{
JOptionPane.showMessageDialog(null, "请选择你要修改的课!╮(╯▽╰)╭", "错误",JOptionPane.ERROR_MESSAGE);
return;
}
String[] jilu = new String[5];
jilu[0] = wbk3_1.getText();
jilu[1] = wbk3_2.getText();
jilu[2] = wbk3_3.getText();
jilu[3] = wbk3_4.getText();
jilu[4] = wbk3_5.getText();
String sql = "update Xkxt_xk set 课程名=?,开学学期=?,学时=?,学分=? where 课程号=?";
Sjk_xg xg = new Sjk_xg();
xg.query(sql,jilu);
xkxx = new Model_xkxx();
xkxx.Cx();
bg.setModel(xkxx);
JOptionPane.showMessageDialog(null, "修改成功!!!╮(╯▽╰)╭");
}
if(e.getActionCommand().equals("tj"))
{
String[] jilu = new String[5];
jilu[0] = wbk3_1.getText();
jilu[1] = wbk3_2.getText();
jilu[2] = wbk3_3.getText();
jilu[3] = wbk3_4.getText();
jilu[4] = wbk3_5.getText();
for(int i = 0;i<jilu.length;i++)
{
if(jilu[i].equals(""))
{
JOptionPane.showMessageDialog(null, "不能为空!!!请完善!╮(╯▽╰)╭", "错误",JOptionPane.ERROR_MESSAGE);
return;
}
}
String sql = "insert into Xkxt_xk values(?,?,?,?,?)";
Sjk_js_tjkc tj = new Sjk_js_tjkc();
tj.query(sql,jilu);
xkxx = new Model_xkxx();
xkxx.Cx();
bg.setModel(xkxx);
JOptionPane.showMessageDialog(null, "添加成功!!!╮(╯▽╰)╭");
wbk3_1.setText("");
wbk3_2.setText("");
wbk3_3.setText("");
wbk3_4.setText("");
wbk3_5.setText("");
}
if(e.getActionCommand().equals("sc"))
{
int i = this.bg.getSelectedRow();//把点击表格的第几列复制给i
if(i == -1)
{
JOptionPane.showMessageDialog(null, "请选择你要删除的课!╮(╯▽╰)╭", "错误",JOptionPane.ERROR_MESSAGE);
return;
}
String kch = (String)this.xkxx.getValueAt(i,0);
System.out.println(kch);
String sql = "delete from Xkxt_xk where 课程号 =?";
Sjk_tk tk = new Sjk_tk();
tk.query(sql,kch);
JOptionPane.showMessageDialog(null, "删除成功!!!╮(╯▽╰)╭");
xkxx = new Model_xkxx();
xkxx.Cx();
bg.setModel(xkxx);
}
}
@Override
public void mouseClicked(MouseEvent e) { //鼠标点击表获得元素
if(e.getSource()==this.bg)
{
int i = this.bg.getSelectedRow();
String kch = (String)this.xkxx.getValueAt(i,0);
String kcm = (String)this.xkxx.getValueAt(i,1);
String kxxq = (String)this.xkxx.getValueAt(i,2);
String xs = (String)this.xkxx.getValueAt(i,