Java Eclipse Swing:sự Kiện Click Button Tạo Form Mới Bị Lỗi.

0101001

New Member
29/5/19
3
0
1
19
package kiemtracuoiky;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

public class tiendien extends JFrame implements ActionListener{

JLabel lb1,lb2,lb3,lb4;
JButton bt1,bt2,bt3,bt4;
Container cont;
JPanel pn1,pn2,pn3,pn4;
//
Connection connect;
Statement stm;
ResultSet rst;

public tiendien(String s) {
super(s);
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection connect=DriverManager.getConnection("jdbc:sqlserver://DESKTOP-NRR3UE0\\SQLEXPRESS:1433;databaseName=java;integratedSecurity=true;","sa","1234560123");
//Connection connect=DriverManager.getConnection("jdbc:sqlserver://DESKTOP-NRR3UEO:9124;instance=DESKTOP-NRR3UE0\\SQLEXPRESS;databaseName=java","sa","");
stm=connect.createStatement();
lb1=new JLabel("");
lb1.setVisible(false);
lb2=new JLabel("Quản lý điện nhà nước");
lb3=new JLabel("");
lb3.setVisible(false);
lb4=new JLabel("");
lb4.setVisible(false);
bt1=new JButton("Đăng ký mới");
bt2=new JButton("Danh sách");
bt3=new JButton("Nhâp chỉ số mới");
bt4=new JButton("Tháng cước");
bt1.addActionListener(this);
bt2.addActionListener(this);
bt3.addActionListener(this);
bt4.addActionListener(this);
cont=this.getContentPane();
cont.setLayout(new GridLayout(3,3));
cont.add(lb1);
cont.add(lb2);
cont.add(lb3);
cont.add(bt1);
cont.add(bt2);
cont.add(bt3);
cont.add(bt4);
this.setSize(400,300);
this.setLocation(200, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}catch(Exception e){
System.out.println(e.getMessage());
}
}
public static void main(String[] args) {
new tiendien("Quản lý tiền điện");
}
@Override
public void actionPerformed(ActionEvent a) {
// TODO Auto-generated method stub
if(a.getSource()==bt1) {
new test("Đăng ký",this,"","0","0","","0","0").setVisible(true);
}
}
}
class test extends JFrame implements ActionListener{
JLabel lb1,lb2,lb3,lb4,lb5,lb6,lb7,lb8;
JTextField tf1,tf2,tf3,tf4,tf5,tf6;
JButton bt1,bt2;
tiendien mst;
//
public test(String s,tiendien st,String a,String b,String c,
String d,String e,String f) {
super(s);
mst=st;
lb1=new JLabel("Tên chủ hộ");
tf1=new JTextField(a,10);
lb1=new JLabel("Số nhà");
tf2=new JTextField(b,10);
lb3=new JLabel("Số công tơ");
tf3=new JTextField(c,10);
lb4=new JLabel("Địa chỉ");
tf4=new JTextField(d,10);
lb5=new JLabel("Chỉ số mới");
tf5=new JTextField(e,10);
lb6=new JLabel("Chỉ số củ");
tf6=new JTextField(f,10);
lb7=new JLabel("Chưa nhập đủ thông tin");
lb7.setVisible(false);
lb8=new JLabel("");
lb8.setVisible(false);
bt1=new JButton("OK");
bt2=new JButton("Cancel");
Container con=this.getContentPane();
con.setLayout(new GridLayout(8,2));
con.add(lb1);con.add(tf1);
con.add(lb2);con.add(tf2);
con.add(lb3);con.add(tf3);
con.add(lb4);con.add(tf4);
con.add(lb5);con.add(tf5);
con.add(lb6);con.add(tf6);
con.add(lb7);con.add(lb8);
con.add(bt1);con.add(bt2);
this.setSize(300,200);
this.setLocation(200, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand().equals("OK")) {
update();
}else {
if(e.getActionCommand().equals("Cancel")) {
this.dispose();
}
}
}
public void update() {
if(tf1.getText().equals("")||tf2.getText().equals("")
|tf3.getText().equals("")|tf4.getText().equals("")) {
lb7.setText("Lỗi");
lb8.setText("Chưa nhập đủ thông tin");
lb7.setForeground(Color.RED);
lb8.setForeground(Color.RED);
}else {
try {
String a=tf1.getText();
float b=Float.parseFloat(tf2.getText());
float c=Float.parseFloat(tf3.getText());
float e=Float.parseFloat(tf5.getText());
float f=Float.parseFloat(tf6.getText());
String sql="";
sql="insert into khachhang(ten,sonha,socongto,diachi)values(a,b,c,d)"
+ "insert into congto(chisomoi,chisocu)values(e,f)";
mst.stm.executeUpdate(sql);
}catch(Exception e) {
e.printStackTrace();
}
}
}
}
 

Attachments