Code: 1. Nút Thoát: int i =JOptionPane.showConfirmDialog(null, Ban co muon thoat khong?, Thong bao, JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE); if (i==0) System.exit(0); 2. Nút Đăng nhập: if (jTextFieldTen.getText().compareTo(Admin)==0 jPasswordFieldMK.getText().compareTo(Admin)==0) { DLG_ManHinhChinh dlg=new DLG_ManHinhChinh(null); dlg.setModal(true); setVisible(false); dlg.show(); setVisible(true); jTextFieldTen.setText(); jPasswordFieldMK.setText(); } else JOptionPane.showMessageDialog(null, Ban go sai tenmat khau); }
1.2 (Xem huong dan dung ham: E:\Lap trinh Windows\Document\java\basic\basic) - Tạo Project - Tạo Dialog – hình - Code: Nút Thoát: int i =JOptionPane.showConfirmDialog(null, "Ban co muon thoat khong?", "Thong bao!", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE); if (i==0) System.exit(0); Nút Đăng nhập: if (jTextFieldTen.getText().compareTo("Admin")==0 && jPasswordFieldMK.getText().compareTo("Admin")==0) { } } DLG_ManHinhChinh dlg=new DLG_ManHinhChinh(null); dlg.setModal(true); setVisible(false); dlg.show(); setVisible(true); jTextFieldTen.setText(""); jPasswordFieldMK.setText(""); else JOptionPane.showMessageDialog(null, "Ban go sai ten/mat khau!!!"); 1.3 Bài tập số Dùng jTexField Dùng jComboBox Dùng jLabel Dùng jPanel Dùng jRadioButton Dùng jCheckBox Dùng jButton Code: + Viet mot ham cat chuoi: public String catChuoi(String s) { String[] t; t=s.split(":"); return t[1]; } + Nut Doc Du Lieu: try { BufferedReader in = new BufferedReader(new FileReader("ThongTinSV.txt")); String s = new String(); //while ((s = in.readLine()) != null) //JOptionPane.showMessageDialog(null, s); s = in.readLine();//doc mot dong du lieu //String[] t; //t=s.split(":");// cat chuoi s cac mang chuoi t[0]=s0 va t[1]=s1, //s0 la chuoi fia truoc dau :, s1 la chuoi sau dau : jTextFieldHoVaLot.setText(catChuoi(s)); s=in.readLine(); jTextFieldTen.setText(catChuoi(s)); s=in.readLine(); jComboBoxNgaySinh.addItem(catChuoi(s)); s=in.readLine(); jComboBoxTonGiao.addItem(catChuoi(s)); s=in.readLine(); jTextFieldDiaChi.setText(catChuoi(s)); s=in.readLine(); jTextFieldDienThoai.setText(catChuoi(s)); s=in.readLine(); jTextFieldMail.setText(catChuoi(s)); s=in.readLine(); if (catChuoi(s).compareTo("nam")==0)//so sanh chuoi //neu s1=s2 thi lenh compareTo tra ve gia tri la { } else { } jRadioButtonNam.setSelected(true); jRadioButtonNu.setSelected(true); s=in.readLine(); if (s.indexOf("Anh")>-1)//tim chuoi s chuoi "Anh" jCheckBoxAnhVan.setSelected(true); if(s.indexOf("Han")>-1) jCheckBoxHanVan.setSelected(true); if(s.indexOf("Phap")>-1) jCheckBoxPhapVan.setSelected(true); } catch (IOException loi) { JOptionPane.showMessageDialog(null, "Da co loi: File khong ton tai"); } } + Nut Ghi Du Lieu xuong File: try { FileWriter out = new FileWriter("ThongTinSV.txt"); String s=new String(); s=jTextFieldHoVaLot.getText();//lay chuoi out.write("Ho va lot: " + s+ "\r\n"); s=jTextFieldTen.getText(); out.write("Ten: " + s+ "\r\n"); s=jComboBoxNgaySinh.getSelectedItem().toString(); out.write("Ngay sinh: " + s+ "\r\n"); s=jComboBoxTonGiao.getSelectedItem().toString(); out.write("Ton giao: " + s+ "\r\n"); s=jTextFieldDiaChi.getText(); out.write("Dia chi: " + s+ "\r\n"); s=jTextFieldDienThoai.getText(); out.write("Dien thoai: " + s+ "\r\n"); s=jTextFieldMail.getText(); out.write("Email: " + s+ "\r\n"); if(jRadioButtonNam.isSelected()==true) 0ut.write("Gioi tinh:nam\r\n"); else out.write("Gioi tinh:nu\r\n"); s=""; if(jCheckBoxAnhVan.isSelected()==true) s+="Anh Van, "; if(jCheckBoxHanVan.isSelected()==true) s+="Han Van, "; if(jCheckBoxPhapVan.isSelected()==true) s+="Phap Van, "; out.write("Ngoai ngu: "+s); JOptionPane.showMessageDialog(null, "Da luu, khong tin thi mo coi!"); //tao file co ten output.txt //out.write("Da tao duoc mot file\r\n"); //ghi dong du lieu vao file va xuong dong //out.write("Chao Chi Linh"); out.close();//dong file } catch(IOException loi) { JOptionPane.showMessageDialog(null, "Co loi ghi File"); } } 1.4 Tạo menu, đồng hồ, ngày tháng năm (dùng gói Swing; gói AWT không tạo được) JmenuBar gắn vào Frame JMenu gắn vào JmenuBar JmenuItem gắn vào Jmenu JMenu Dv JmenuItem JButton - Tạo JpopupMenu (khi R – Click Frame xuất menu Frame) - R – Click vào Frame Events Add Events… - Chọn kiện mouseReleased - Code cho kiện này: JPopupMenu pp = getJPopupMenu(); //JPopupMenu la ten jPopUpMenu if (pp.isPopupTrigger(e)) pp.show(e.getComponent(),e.getX(),e.getY()); - Nút Test:R-Click vào nút Test Add actionPerformed - Code: //mo hop thoai chon File JFileChooser chooser = new JFileChooser(); chooser.showOpenDialog(null); //mo hop thoai mau JColorChooser hopthoaimau = new JColorChooser(); hopthoaimau.showDialog(null,"Hop thoai mau!", Color.red); - Tạo Timer ( ) + Tạo lớp Timer + Trong hàm init… private void initialize() { this.setSize(309, 156); this.setJMenuBar(getJJMenuBar()); this.setContentPane(getJContentPane()); this.setTitle("JFrame"); Timer t = new Timer(1000,this);//nhan Ctrl + SpaceBar chon Timer - Swing t.start(); } + Viết hàm actionPerformed public void actionPerformed(ActionEvent e) { //JOptionPane.showConfirmDialog(null,"Lap lai chu ki"); Date now = new Date(); //jLabelGio.setText(now.toString()); //Tao dong ho //Dùng jLabel để hiển thị jLabelGio.setText(now.getHours() + ":" + now.getMinutes() + ":"+ now.getSeconds()); //Tao ProgressBar - tien trinh //Dùng công cụ jProg jProgressBarTest.setValue(jProgressBarTest.getValue()+2); if(jProgressBarTest.getValue()==jProgressBarTest.getMaximum()) { jProgressBarTest.setValue(0); } } 1.5 Tạo kết nối sở DL - Dùng MySQL tạo CSDL (VD: tên LopHoc) - Các câu lệnh SQL: + Câu lệnh tạo CSDL: (Trước tạo bảng CSDL vừa tạo, ta phải gõ câu lệnh: + Câu lệnh tạo bảng: + Câu lệnh xóa bảng: + Câu lệnh chèn dòng vào bảng: + Câu lệnh xem bảng: - Copy file vào thư mục … theo đường dẫn C:\Program Files\Java\jre1.6.0\lib\ext - mở Eclip, tạo lớp - Gõ Code: public class Connect { public static void main (String[] args) { Connection = null; try { String userName = "root";//ten dang nhap vao CSDL String password = "262006";//MK dang nhap CSDL String url = "jdbc:mysql://127.0.0.1:3306/LopHoc";//duong dan den Database = DriverManager.getConnection(url,userName,password); System.out.println("Da ket noi duoc CSDL"); /* //chen dong vao bang SinhVien Statement s = con.createStatement(); s.execute("insert into SinhVien (hoten,namsinh) values ('Duong Van Nghia',1970)"); s.close(); */ Statement s = con.createStatement();//tao doi tuong s.execute("select * from SinhVien");//thi hanh cau truy van ResultSet rs = s.getResultSet(); while (rs.next()) { int MSSV = rs.getInt("MSSV");//neu kho nho MSSV thi thay la so TT cot la String HoTen = rs.getString("Hoten"); int NamSinh = rs.getInt(3); System.out.println(" MSSV:"+ MSSV + " Ho Ten:" + HoTen + " Nam Sinh:" + NamSinh); } s.close(); con.close(); System.out.println("Da dong ket noi"); } } catch (Exception e) { System.err.println("Khong ket noi duoc CSDL"); } } 1.6 Kết nối với CSDL hiển thị DL lên Frame: - Tạo Java Project mới: - Tạo Visual Class: - Chọn gói đồ họa Swing: - Tạo Jframe, Jtable - Viết code: import import import import import import import import import import import import import import java.awt.BorderLayout; java.sql.Connection; java.sql.DriverManager; java.sql.ResultSet; java.sql.Statement; java.util.Vector; javax.swing.JOptionPane; javax.swing.JPanel; javax.swing.JFrame; javax.swing.JScrollPane; java.awt.Rectangle; javax.swing.JTable; javax.swing.table.DefaultTableModel; java.awt.Dimension; public class KetNoiCSDL_SangCN extends JFrame { private static final long serialVersionUID = 1L; private JPanel jContentPane = null; Connection conn =null; // Doi tuong de ket noi CSDL @jve:decl-index=0: private JScrollPane jScrollPane = null; private JTable jTableDanhSach = null; // DefaultTableModel model = null; //dung de quan ly du lieu table private JOptionPane jOptionPane = null; index=0:visual-constraint="564,56" // @jve:decl- public void ketNoi() { try { String useName ="root"; //Tai khoan dang nhap SQL String password ="262006"; String url ="jdbc:mysql://127.0.0.1:3306/LopHoc"; conn = DriverManager.getConnection(url,useName,password); cong"); JOptionPane.showMessageDialog(null,"Ket Noi Thanh values('GDCD')"); Statement s =conn.createStatement(); //s.execute("insert into Khoa(TenKhoa) s.execute("select * from SinhVien"); ResultSet rs = s.getResultSet(); Vector dongtieude = new Vector(); dongtieude.add("Ma so SV"); dongtieude.add("Ho va ten"); model = new DefaultTableModel(dongtieude, 0); jTableDanhSach.setModel(model); //dua model vao table danh sach while(rs.next()) { int MSSV=rs.getInt("MSSV"); String Hoten =rs.getString("Hoten"); //Luu y ten khoa phai giong ben CSDL //JOptionPane.showMessageDialog(null,"MKH : " + MKH +"Khoa : " + Khoa); //lay du lieu tung dong //dong.add("cot1"); //dong.add("cot2"); Vector dong = new Vector(); dong.add(Integer.toString(MSSV)); dong.add(Hoten); model.addRow(dong); //dua dong vao table } rs.close(); s.close(); duoc"); conn.close(); } catch (Exception e) { JOptionPane.showMessageDialog(null,"Khong Ket Noi CSDL } } /** * This is the default constructor */ public KetNoiCSDL_SangCN() { super(); initialize(); } /** * This method initializes this * * @return void */ private void initialize() { this.setSize(497, 344); this.setContentPane(getJContentPane()); this.setTitle("JFrame"); ketNoi(); } /** * This method initializes jContentPane * * @return javax.swing.JPanel */ private JPanel getJContentPane() { if (jContentPane == null) { jContentPane = new JPanel(); jContentPane.setLayout(null); jContentPane.add(getJScrollPane(), null); } return jContentPane; } /** * This method initializes jScrollPane * * @return javax.swing.JScrollPane */ private JScrollPane getJScrollPane() { if (jScrollPane == null) { jScrollPane = new JScrollPane(); jScrollPane.setBounds(new Rectangle(66, 51, 319, 191)); jScrollPane.setViewportView(getJTableDanhSach()); } return jScrollPane; } /** * This method initializes jTableDanhSach * * @return javax.swing.JTable */ private JTable getJTableDanhSach() { if (jTableDanhSach == null) { jTableDanhSach = new JTable(); } return jTableDanhSach; } /** * This method initializes jOptionPane * * @return javax.swing.JOptionPane */ private JOptionPane getJOptionPane() { if (jOptionPane == null) { jOptionPane = new JOptionPane(); } return jOptionPane; } } // @jve:decl-index=0:visual-constraint="10,10" [...]... Tạo 1 Java Project mới: - Tạo 1 Visual Class: - Chọn gói đồ họa Swing: - Tạo 1 Jframe, 1 Jtable - Viết code: import import import import import import import import import import import import import import java. awt.BorderLayout; java. sql.Connection; java. sql.DriverManager; java. sql.ResultSet; java. sql.Statement; java. util.Vector; javax.swing.JOptionPane; javax.swing.JPanel; javax.swing.JFrame; javax.swing.JScrollPane;... java. sql.DriverManager; java. sql.ResultSet; java. sql.Statement; java. util.Vector; javax.swing.JOptionPane; javax.swing.JPanel; javax.swing.JFrame; javax.swing.JScrollPane; java. awt.Rectangle; javax.swing.JTable; javax.swing.table.DefaultTableModel; java. awt.Dimension; public class KetNoiCSDL_SangCN extends JFrame { private static final long serialVersionUID = 1L; private JPanel jContentPane = null; Connection... this.setTitle("JFrame"); ketNoi(); } /** * This method initializes jContentPane * * @return javax.swing.JPanel */ private JPanel getJContentPane() { if (jContentPane == null) { jContentPane = new JPanel(); jContentPane.setLayout(null); jContentPane.add(getJScrollPane(), null); } return jContentPane; } /** * This method initializes jScrollPane * * @return javax.swing.JScrollPane */ private JScrollPane getJScrollPane() { if... jScrollPane.setViewportView(getJTableDanhSach()); } return jScrollPane; } /** * This method initializes jTableDanhSach * * @return javax.swing.JTable */ private JTable getJTableDanhSach() { if (jTableDanhSach == null) { jTableDanhSach = new JTable(); } return jTableDanhSach; } /** * This method initializes jOptionPane * * @return javax.swing.JOptionPane */ private JOptionPane getJOptionPane() { if (jOptionPane == null) { jOptionPane...- Copy file vào thư mục … theo đường dẫn C:\Program Files \Java\ jre1.6.0\lib\ext - mở Eclip, tạo 1 lớp - Gõ Code: public class Connect { public static void main (String[] args) { Connection con = null; try { String userName = "root";//ten dang nhap vao CSDL String ... import import java. awt.BorderLayout; java. sql.Connection; java. sql.DriverManager; java. sql.ResultSet; java. sql.Statement; java. util.Vector; javax.swing.JOptionPane; javax.swing.JPanel; javax.swing.JFrame;... javax.swing.JOptionPane; javax.swing.JPanel; javax.swing.JFrame; javax.swing.JScrollPane; java. awt.Rectangle; javax.swing.JTable; javax.swing.table.DefaultTableModel; java. awt.Dimension; public class KetNoiCSDL_SangCN... chèn dòng vào bảng: + Câu lệnh xem bảng: - Copy file vào thư mục … theo đường dẫn C:Program Files Java jre1.6.0libext - mở Eclip, tạo lớp - Gõ Code: public class Connect { public static void main