java窗体
为什么jb,jb1,jb3的事件都能用,jb2的事件不能用,调用不了那个类package sdadsa;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.*;
import sdadsa.select.ChangeListener;
public class updt extends JFrame {
String ss;
JLabel jl1;
JLabel jl2;
JLabel jl3;
JLabel jl4;
public JButton jb;
public JButton jb1;
public JButton jb2;
public JButton jb3;
int count=0;
publicJTextField jtf1 = new JTextField(10);
public JTextField jtf2 = new JTextField(10);
JComboBox jcb = new JComboBox();
JComboBox jcb1 = new JComboBox();
publicupdt(String s) throws ClassNotFoundException, SQLException{
String url="jdbc:mysql://localhost:3306/database";
String user="root";
String password="123456";
String sss="select cno from course";
int i=0;
ss=s;
//JPanel p1 = new JPanel(new BorderLayout());
jb3=new JButton("返回上一层");
jb3.setBounds(540, 400, 120, 40);
this.add(jb3);
jb3.addActionListener(new ChangeListener3(this));
JLabel j1=new JLabel("学生信息管理系统");
this.add(j1);
j1.setFont(new java.awt.Font("黑体", 1, 50));
j1.setBounds(130, 40, 1000, 50);
switch(s)
{
case "student":{
jl1=new JLabel("请选择要修改的类型:");
jl2=new JLabel("请输入要修改的学号:");
jl3=new JLabel("请输入要修改的内容:");
jl1.setBounds(158, 118, 1000, 50);
jl1.setFont(new java.awt.Font("宋体", 0, 20));
jl2.setBounds(160, 158, 1000, 50);
jl2.setFont(new java.awt.Font("宋体", 0, 20));
jl3.setBounds(160, 198, 1000, 50);
jl3.setFont(new java.awt.Font("宋体", 0, 20));
jb = new JButton("确定");
jl1.setBounds(160, 118, 1000, 50);
jl1.setFont(new java.awt.Font("宋体", 0, 20));
this.add(jl3);
jcb.insertItemAt("学号",0);
jcb.insertItemAt("姓名", 1);
jcb.insertItemAt("性别", 2);
jcb.insertItemAt("课程", 3);
jcb.insertItemAt("住址", 4);
jcb.insertItemAt("电话", 5);
jcb.insertItemAt("qq", 6);
jcb.insertItemAt("年龄", 7);
jcb.setBounds(360, 126, 130, 30);
//p1.add(jcb, BorderLayout.NORTH);
this.add(jcb);
jb.setBounds(290, 400, 100, 40);
this.add(jl1);
this.add(jtf1);
jtf1.setBounds(360, 170, 152, 25);
this.add(jl2);
jtf2.setBounds(360,210, 152, 25);
this.add(jtf2);
this.add(jb);
jb.setBounds(290, 400, 100, 40);
this.setLayout(null);
this.setBounds(250, 250, 700, 500);
this.setTitle("学生信息管理系统");
this.getContentPane().setBackground(Color.WHITE);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
jb.addActionListener(new ChangeListener(this)); }break;
case "sc":{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("加载驱动成功!");
Connection con=DriverManager.getConnection(url,user,password);
System.out.println("连接数据库成功!");
Statement st=con.createStatement();
System.out.println("创建Statement成功!");
ResultSet rs=st.executeQuery(sss);
System.out.println("数据操作成功!");
System.out.println("------------");
jb1=new JButton("确定");
jl1=new JLabel("请选择要类型:");
jl2=new JLabel("请输入要修改的学号:");
jl3=new JLabel("请输入要修改的内容:");
jl4=new JLabel("请选择课程号:");
jl1.setBounds(220, 118, 1000, 50);
jl1.setFont(new java.awt.Font("宋体", 0, 20));
jl4.setBounds(220, 158, 1000, 50);
jl4.setFont(new java.awt.Font("宋体", 0, 20));
jl2.setBounds(160, 198, 1000, 50);
jl2.setFont(new java.awt.Font("宋体", 0, 20));
jl3.setBounds(160, 238, 1000, 50);
jl3.setFont(new java.awt.Font("宋体", 0, 20));
this.add(jl3);
this.add(jl4);
jcb.insertItemAt("学号",0);
jcb.insertItemAt("课程号", 1);
jcb.insertItemAt("成绩", 2);
jcb.setBounds(360,125, 130, 30);
while(rs.next())
{
jcb1.insertItemAt(rs.getString("cno"),i);count++;
}
jcb1.setBounds(360,170, 130, 30);
this.add(jcb1);
// p1.add(jcb, BorderLayout.NORTH);
this.add(jcb);
this.add(jl1);
this.add(jtf1);
jtf1.setBounds(360, 210, 152, 25);
this.add(jl2);
jtf2.setBounds(360,250, 152, 25);
this.add(jtf2);
this.add(jb1);
jb1.setBounds(290, 400, 100, 40);
this.setLayout(null);
this.setBounds(250, 250, 700, 500);
this.setTitle("学生信息管理系统");
this.getContentPane().setBackground(Color.WHITE);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
jb1.addActionListener(new ChangeListener1(this)); }break;
case "course":{
jb2=new JButton("确定");
jl1=new JLabel("请选择要修改的类型:");
jl2=new JLabel("请输入要修改的课程号:");
jl3=new JLabel("请输入要修改的内容:");
jl1.setBounds(158, 118, 1000, 50);
jl1.setFont(new java.awt.Font("宋体", 0, 20));
jl2.setBounds(140, 158, 1000, 50);
jl2.setFont(new java.awt.Font("宋体", 0, 20));
jl3.setBounds(162, 198, 1000, 50);
jl3.setFont(new java.awt.Font("宋体", 0, 20));
this.add(jl3);
jcb.insertItemAt("学号",0);
jcb.insertItemAt("课程号", 1);
jcb.insertItemAt("学分", 2);
jcb.setBounds(360, 125, 130, 30); //下拉框
//p1.add(jcb, BorderLayout.NORTH);
this.add(jcb);
this.add(jl1); // jl文本
this.add(jtf1); //文本框
jtf1.setBounds(360, 170, 152, 25);
this.add(jl2);
jtf2.setBounds(360,210, 152, 25);
this.add(jtf2);
this.add(jb2);
jb2.setBounds(290, 400, 100, 40);
this.setLayout(null);
this.setBounds(250, 250, 700, 500);
this.setTitle("学生信息管理系统");
this.getContentPane().setBackground(Color.WHITE);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
jb2.addActionListener(new ChangeListener2(this));
System.out.println("jb2窗体部分");}break;
}
}
class ChangeListener implements ActionListener{
JFrame jf;
public ChangeListener(JFrame jf) {
this.jf = jf;
}
@Override
public void actionPerformed(ActionEvent e) {
String sno=jtf1.getText();
String ss=jtf2.getText();
updateceshi3333 u=new updateceshi3333();
int i = 0;
try {
switch(jcb.getSelectedIndex())
{
case 0:i= u.updateceshi3333("student","sno",ss,"sno",sno);break;
case 1:i=u.updateceshi3333("student","sname",ss,"sno",sno);break;
case 2:i=u.updateceshi3333("student","ssex",ss,"sno",sno);break;
case 3:i=u.updateceshi3333("student","sdept",ss,"sno",sno);break;
case 4:i=u.updateceshi3333("student","address",ss,"sno",sno);break;
case 5:i=u.updateceshi3333("student","phone",ss,"sno",sno);break;
case 6:i=u.updateceshi3333("student","qq",ss,"sno",sno);break;
case 7:i=u.updateceshi3333("student","sage",ss,"sno",sno);break;
}
if(i==1)
JOptionPane.showMessageDialog(jf, "修改成功");
else JOptionPane.showMessageDialog(jf, "修改失败,此人不存在");
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
//e1.printStackTrace();
}
}
}
class ChangeListener1 implements ActionListener{
JFrame jf;
public ChangeListener1(JFrame jf) {
this.jf = jf;
}
@Override
public void actionPerformed(ActionEvent e) {
String sno=jtf1.getText();
String ss=jtf2.getText();
updateceshi3333 u=new updateceshi3333();
int i = 0;
String cno = null;
scupdate sc=new scupdate();
try {
cno=sc.scupdate((count-jcb1.getSelectedIndex()));
} catch (ClassNotFoundException | SQLException e2) {
}
try {
switch(jcb.getSelectedIndex())
{
case 0:i=u.updateceshi33331("sc","sno",ss,"sno",sno,cno);break;
case 1:i= u.updateceshi33331("sc","cno",ss,"sno",sno,cno);break;
case 2:i=u.updateceshi33331("sc","grade",ss,"sno",sno,cno);break;
}
if(i==1)
JOptionPane.showMessageDialog(jf, "修改成功");
else JOptionPane.showMessageDialog(jf, "修改失败,请确认后输入");
}
catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
//e1.printStackTrace();
}
}
class ChangeListener2 implements ActionListener{
JFrame jf;
public ChangeListener2(JFrame jf)
{
this.jf = jf;
System.out.println("构造函数");
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("确定");
String sno=jtf1.getText();
String ss=jtf2.getText();
updateceshi3333 u=new updateceshi3333();
int i = 0;
try {
switch(jcb.getSelectedIndex())
{
case 0:i=u.updateceshi3333("course","cno",ss,"sno",sno);break;
case 1:i=u.updateceshi3333("course","cname",ss,"sno",sno);break;
case 2:i=u.updateceshi3333("course","ccredit",ss,"sno",sno);break;
}
if(i==1)
JOptionPane.showMessageDialog(jf, "修改成功");
else JOptionPane.showMessageDialog(jf, "修改失败,此课程号不存在");
}
catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
//e1.printStackTrace();
}
}
}
}
class ChangeListener3 implements ActionListener{
JFrame jf;
public ChangeListener3(JFrame jf) {
this.jf = jf;
}
@Override
public void actionPerformed(ActionEvent e) {
jf.dispose();//销毁登录窗口
newContentFrame1(ss);// 返回上一层
}
}
}
ChangeListener2 类写在了ChangeListener1里了。拥有良好的编码风格是多么重要。。。
页:
[1]