level 5
小博博博博博1
楼主
我想做一个关于中超的系统,现在在只完成了两部分,一个是系统的主界面,另一个是添加球队信息的界面,我想通过点击主界面上的菜单选项,执行添加球队信息的界面。两部分单独运行可以实现,但是在主界面点击添加球队之后,却无法将球队信息添加到数据库中。程序贴在下面。
主界面程序:
package 中超联赛联赛积分管理系统;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.swing.*;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;
public class MainFrame extends JFrame implements ActionListener {
static Connection con;
static PreparedStatement sql;
public Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql:"
+ "//127.0.0.1:3306/db_team", "root", "123456");
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
private JPanel p;
private JLabel lbl;
private JMenuBar menubar;
private JMenu menuteam, menuplayer, menuintegral;
private JMenuItem miTquire, miTadd, miTedit, miTdelete, miPquire, miPadd, miPedit, miPdelete, miIquire, miIadd,
miIedit, miIdelete;
public MainFrame() {
super("中超联赛联赛积分管理系统");
p = new JPanel();
Icon icon = new ImageIcon("C:/Users/lenovo/Desktop/毕业设计/中超标志.jpg");
lbl = new JLabel(icon);
lbl.setBounds(0, 0, this.getWidth(), this.getHeight());
p.add(lbl);
menubar = new JMenuBar();
this.setJMenuBar(menubar);
menuteam = new JMenu("球队管理");
menuplayer = new JMenu("球员管理");
menuintegral = new JMenu("积分管理");
menubar.add(menuteam);
menubar.add(menuplayer);
menubar.add(menuintegral);
miPquire = new JMenuItem("查询球员");
miPadd = new JMenuItem("添加球员");
miPedit = new JMenuItem("修改球员");
miPdelete = new JMenuItem("删除球员");
miTquire = new JMenuItem("查询球队");
miTadd = new JMenuItem("添加球队");
miTedit = new JMenuItem("修改球队");
miTdelete = new JMenuItem("删除球队");
miIquire = new JMenuItem("查询积分");
miIadd = new JMenuItem("录入积分");
miIedit = new JMenuItem("修改积分");
miIdelete = new JMenuItem("删除积分");
menuteam.add(miTquire);
menuteam.add(miTadd);
menuteam.add(miTedit);
menuteam.add(miTdelete);
menuplayer.add(miPquire);
menuplayer.add(miPadd);
menuplayer.add(miPedit);
menuplayer.add(miPdelete);
menuintegral.add(miIquire);
menuintegral.add(miIadd);
menuintegral.add(miIedit);
menuintegral.add(miIdelete);
miTquire.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
Tquire tqu = new Tquire();
}
});
miTadd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
Tadd tad = new Tadd();
tad.getConnection();
}
});
this.add(p);
this.setSize(810, 560);
this.setLocation(100, 50);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setVisible(true);
}
public static void main(String[] args) {
new MainFrame();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
Object source = e.getSource();
if (source == miTquire) {
new Tquire();
}
if (source == miTadd) {
Tadd tad = new Tadd();
tad.getConnection();
}
}
}
添加球队信息界面代码:
package 中超联赛联赛积分管理系统;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.*;
public class Tadd extends JFrame implements ActionListener {
static Connection con;
static PreparedStatement sql;
public Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql:"
+ "//127.0.0.1:3306/db_team", "root", "123456");
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
private JPanel p;
private JLabel lblTname, lblTcoach, lblTlocation;
private JTextField txtTname, txtTcoach, txtTlocation;
private JButton btnok, btncancl;
public Tadd() {
super("球队信息添加");
p = new JPanel();
lblTname = new JLabel("球队名称");
lblTcoach = new JLabel("主教练");
lblTlocation = new JLabel("主场位置");
txtTname = new JTextField(20);
txtTcoach = new JTextField(20);
txtTlocation = new JTextField(20);
btnok = new JButton("添加");
btncancl = new JButton("取消");
p.setLayout(null);
lblTname.setBounds(90, 20, 100, 25);
lblTcoach.setBounds(90, 70, 100, 25);
lblTlocation.setBounds(90, 120, 100, 25);
txtTname.setBounds(200, 20, 100, 25);
txtTcoach.setBounds(200, 70, 100, 25);
txtTlocation.setBounds(200, 120, 100, 25);
btnok.setBounds(90, 200, 60, 25);
btncancl.setBounds(200, 200, 60, 25);
p.add(lblTname);
p.add(txtTname);
p.add(lblTcoach);
p.add(txtTcoach);
p.add(lblTlocation);
p.add(txtTlocation);
p.add(btnok);
p.add(btncancl);
btnok.addActionListener(this);
btncancl.addActionListener(this);
this.add(p);
this.setSize(400, 300);
this.setLocation(100, 100);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
Tadd ta = new Tadd();
ta.getConnection();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
if (e.getSource() == btnok) {
String strTname = txtTname.getText();
String strTcoach = txtTcoach.getText();
String strTlocation = txtTlocation.getText();
if (strTname.equals("")) {
JOptionPane.showMessageDialog(btnok, "球队名不能为空!");
return;
}
if (strTcoach.equals("")) {
JOptionPane.showMessageDialog(btnok, "教练姓名不能为空!");
return;
}
if (strTlocation.equals("")) {
JOptionPane.showMessageDialog(btnok, "主场位置不能为空!");
return;
}
try {
sql = con.prepareStatement("INSERT INTO tb_team(Tname,Tcoach,Tlocation) " + "values(?,?,?)");
sql.setString(1, strTname);
sql.setString(2, strTcoach);
sql.setString(3, strTlocation);
sql.executeUpdate();
} catch (Exception ex) {
ex.printStackTrace();
}
this.setVisible(false);
}
if (e.getSource() == btncancl) {
txtTname.setText("");
txtTcoach.setText("");
txtTlocation.setText("");
this.setVisible(false);
}
}
}
我该怎么改 是应该使用多线程吗?
2018年04月03日 14点04分
1
主界面程序:
package 中超联赛联赛积分管理系统;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.swing.*;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;
public class MainFrame extends JFrame implements ActionListener {
static Connection con;
static PreparedStatement sql;
public Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql:"
+ "//127.0.0.1:3306/db_team", "root", "123456");
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
private JPanel p;
private JLabel lbl;
private JMenuBar menubar;
private JMenu menuteam, menuplayer, menuintegral;
private JMenuItem miTquire, miTadd, miTedit, miTdelete, miPquire, miPadd, miPedit, miPdelete, miIquire, miIadd,
miIedit, miIdelete;
public MainFrame() {
super("中超联赛联赛积分管理系统");
p = new JPanel();
Icon icon = new ImageIcon("C:/Users/lenovo/Desktop/毕业设计/中超标志.jpg");
lbl = new JLabel(icon);
lbl.setBounds(0, 0, this.getWidth(), this.getHeight());
p.add(lbl);
menubar = new JMenuBar();
this.setJMenuBar(menubar);
menuteam = new JMenu("球队管理");
menuplayer = new JMenu("球员管理");
menuintegral = new JMenu("积分管理");
menubar.add(menuteam);
menubar.add(menuplayer);
menubar.add(menuintegral);
miPquire = new JMenuItem("查询球员");
miPadd = new JMenuItem("添加球员");
miPedit = new JMenuItem("修改球员");
miPdelete = new JMenuItem("删除球员");
miTquire = new JMenuItem("查询球队");
miTadd = new JMenuItem("添加球队");
miTedit = new JMenuItem("修改球队");
miTdelete = new JMenuItem("删除球队");
miIquire = new JMenuItem("查询积分");
miIadd = new JMenuItem("录入积分");
miIedit = new JMenuItem("修改积分");
miIdelete = new JMenuItem("删除积分");
menuteam.add(miTquire);
menuteam.add(miTadd);
menuteam.add(miTedit);
menuteam.add(miTdelete);
menuplayer.add(miPquire);
menuplayer.add(miPadd);
menuplayer.add(miPedit);
menuplayer.add(miPdelete);
menuintegral.add(miIquire);
menuintegral.add(miIadd);
menuintegral.add(miIedit);
menuintegral.add(miIdelete);
miTquire.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
Tquire tqu = new Tquire();
}
});
miTadd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
Tadd tad = new Tadd();
tad.getConnection();
}
});
this.add(p);
this.setSize(810, 560);
this.setLocation(100, 50);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setVisible(true);
}
public static void main(String[] args) {
new MainFrame();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
Object source = e.getSource();
if (source == miTquire) {
new Tquire();
}
if (source == miTadd) {
Tadd tad = new Tadd();
tad.getConnection();
}
}
}
添加球队信息界面代码:
package 中超联赛联赛积分管理系统;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.*;
public class Tadd extends JFrame implements ActionListener {
static Connection con;
static PreparedStatement sql;
public Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql:"
+ "//127.0.0.1:3306/db_team", "root", "123456");
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
private JPanel p;
private JLabel lblTname, lblTcoach, lblTlocation;
private JTextField txtTname, txtTcoach, txtTlocation;
private JButton btnok, btncancl;
public Tadd() {
super("球队信息添加");
p = new JPanel();
lblTname = new JLabel("球队名称");
lblTcoach = new JLabel("主教练");
lblTlocation = new JLabel("主场位置");
txtTname = new JTextField(20);
txtTcoach = new JTextField(20);
txtTlocation = new JTextField(20);
btnok = new JButton("添加");
btncancl = new JButton("取消");
p.setLayout(null);
lblTname.setBounds(90, 20, 100, 25);
lblTcoach.setBounds(90, 70, 100, 25);
lblTlocation.setBounds(90, 120, 100, 25);
txtTname.setBounds(200, 20, 100, 25);
txtTcoach.setBounds(200, 70, 100, 25);
txtTlocation.setBounds(200, 120, 100, 25);
btnok.setBounds(90, 200, 60, 25);
btncancl.setBounds(200, 200, 60, 25);
p.add(lblTname);
p.add(txtTname);
p.add(lblTcoach);
p.add(txtTcoach);
p.add(lblTlocation);
p.add(txtTlocation);
p.add(btnok);
p.add(btncancl);
btnok.addActionListener(this);
btncancl.addActionListener(this);
this.add(p);
this.setSize(400, 300);
this.setLocation(100, 100);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
Tadd ta = new Tadd();
ta.getConnection();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
if (e.getSource() == btnok) {
String strTname = txtTname.getText();
String strTcoach = txtTcoach.getText();
String strTlocation = txtTlocation.getText();
if (strTname.equals("")) {
JOptionPane.showMessageDialog(btnok, "球队名不能为空!");
return;
}
if (strTcoach.equals("")) {
JOptionPane.showMessageDialog(btnok, "教练姓名不能为空!");
return;
}
if (strTlocation.equals("")) {
JOptionPane.showMessageDialog(btnok, "主场位置不能为空!");
return;
}
try {
sql = con.prepareStatement("INSERT INTO tb_team(Tname,Tcoach,Tlocation) " + "values(?,?,?)");
sql.setString(1, strTname);
sql.setString(2, strTcoach);
sql.setString(3, strTlocation);
sql.executeUpdate();
} catch (Exception ex) {
ex.printStackTrace();
}
this.setVisible(false);
}
if (e.getSource() == btncancl) {
txtTname.setText("");
txtTcoach.setText("");
txtTlocation.setText("");
this.setVisible(false);
}
}
}
我该怎么改 是应该使用多线程吗?