Click Nbfs

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

/*

* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this


license

* Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template

*/

package lab05;

import java.util.ArrayList;

import javax.swing.JFileChooser;

import javax.swing.JOptionPane;

import javax.swing.table.DefaultTableModel;

/**

* @author HP

*/

public class GUI extends javax.swing.JFrame {

DefaultTableModel dtm;

MangSinhVien a;

/**

* Creates new form GUI

*/

public GUI() {

initComponents();

dtm= new DefaultTableModel();

dtm.setColumnIdentifiers(new Object[]{"Ngành","Mã SV","Họ Tên","Gioi Tinh","Môn 1","Môn


2","Môn 3","Điểm TB","Xếp loại"});

tblHienThi.setModel( dtm);

a= new MangSinhVien();

/**
* This method is called from within the constructor to initialize the form.

* WARNING: Do NOT modify this code. The content of this method is always

* regenerated by the Form Editor.

*/

@SuppressWarnings("unchecked")

// <editor-fold defaultstate="collapsed" desc="Generated Code">

private void initComponents() {

buttonGroup1 = new javax.swing.ButtonGroup();

buttonGroup2 = new javax.swing.ButtonGroup();

lblNganh = new javax.swing.JLabel();

lblMaSV = new javax.swing.JLabel();

lblTen = new javax.swing.JLabel();

lblGioiTinh = new javax.swing.JLabel();

lblMon1 = new javax.swing.JLabel();

lblMon2 = new javax.swing.JLabel();

lblMon3 = new javax.swing.JLabel();

rbtCNTT = new javax.swing.JRadioButton();

rbtKinhTe = new javax.swing.JRadioButton();

txtMa = new javax.swing.JTextField();

txtTen = new javax.swing.JTextField();

rbtNam = new javax.swing.JRadioButton();

rbtNu = new javax.swing.JRadioButton();

txtMon1 = new javax.swing.JTextField();

txtMon2 = new javax.swing.JTextField();

txtMon3 = new javax.swing.JTextField();

jScrollPane1 = new javax.swing.JScrollPane();

tblHienThi = new javax.swing.JTable();

btnDocFile = new javax.swing.JButton();

btnThem = new javax.swing.JButton();

btnUpdate = new javax.swing.JButton();


Delete = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

setFont(new java.awt.Font("Arial", 0, 18)); // NOI18N

lblNganh.setFont(new java.awt.Font("Segoe UI", 0, 18)); // NOI18N

lblNganh.setText("Nganh");

lblMaSV.setFont(new java.awt.Font("Segoe UI", 0, 18)); // NOI18N

lblMaSV.setText("Ma SV");

lblTen.setFont(new java.awt.Font("Segoe UI", 0, 18)); // NOI18N

lblTen.setText("Ten");

lblGioiTinh.setFont(new java.awt.Font("Segoe UI", 0, 18)); // NOI18N

lblGioiTinh.setText("Gioi Tinh");

lblMon1.setFont(new java.awt.Font("Segoe UI", 0, 18)); // NOI18N

lblMon1.setText(" java");

lblMon2.setFont(new java.awt.Font("Segoe UI", 0, 18)); // NOI18N

lblMon2.setText("css");

lblMon3.setFont(new java.awt.Font("Segoe UI", 0, 18)); // NOI18N

lblMon3.setText("html");

buttonGroup1.add(rbtCNTT);

rbtCNTT.setSelected(true);

rbtCNTT.setText("CNTT");

rbtCNTT.setAutoscrolls(true);

rbtCNTT.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {

rbtCNTTActionPerformed(evt);

});

buttonGroup1.add(rbtKinhTe);

rbtKinhTe.setText("Kinh te");

rbtKinhTe.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

rbtKinhTeActionPerformed(evt);

});

txtMa.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

txtMaActionPerformed(evt);

});

txtTen.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

txtTenActionPerformed(evt);

});

buttonGroup2.add(rbtNam);

rbtNam.setSelected(true);

rbtNam.setText("Nam");

rbtNam.setAutoscrolls(true);

buttonGroup2.add(rbtNu);
rbtNu.setText("Nu");

tblHienThi.setModel(new javax.swing.table.DefaultTableModel(

new Object [][] {

{null, null, null, null},

{null, null, null, null},

{null, null, null, null},

{null, null, null, null}

},

new String [] {

"Title 1", "Title 2", "Title 3", "Title 4"

));

tblHienThi.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(java.awt.event.MouseEvent evt) {

tblHienThiMouseClicked(evt);

});

jScrollPane1.setViewportView(tblHienThi);

btnDocFile.setFont(new java.awt.Font("Segoe UI", 0, 18)); // NOI18N

btnDocFile.setText("Doc File");

btnDocFile.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

btnDocFileActionPerformed(evt);

});

btnThem.setFont(new java.awt.Font("Segoe UI", 0, 18)); // NOI18N

btnThem.setText("Them");

btnThem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {

btnThemActionPerformed(evt);

});

btnUpdate.setFont(new java.awt.Font("Segoe UI", 0, 18)); // NOI18N

btnUpdate.setText("Update");

btnUpdate.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

btnUpdateActionPerformed(evt);

});

Delete.setText("Delete");

Delete.setActionCommand("Delete");

Delete.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButtonDeleteActionPerformed(evt);

});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addContainerGap()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGap(6, 6, 6)
.addComponent(btnDocFile, javax.swing.GroupLayout.PREFERRED_SIZE, 93,
javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(18, 18, 18)

.addComponent(btnThem)

.addGap(18, 18, 18)

.addComponent(btnUpdate)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addComponent(Delete, javax.swing.GroupLayout.PREFERRED_SIZE, 87,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(0, 0, Short.MAX_VALUE))

.addGroup(layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(lblNganh)

.addComponent(lblMaSV)

.addComponent(lblTen)

.addComponent(lblGioiTinh)

.addComponent(lblMon1, javax.swing.GroupLayout.PREFERRED_SIZE, 44,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(lblMon2)

.addComponent(lblMon3))

.addGap(24, 24, 24)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,
false)

.addGroup(layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADI
NG)

.addComponent(rbtCNTT)

.addComponent(rbtNam))

.addGap(56, 56, 56)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADI
NG)

.addComponent(rbtNu)

.addComponent(rbtKinhTe)))
.addComponent(txtMa)

.addComponent(txtTen)

.addComponent(txtMon1)

.addComponent(txtMon2)

.addComponent(txtMon3))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 532,


Short.MAX_VALUE)))

.addContainerGap())

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addContainerGap()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)

.addGroup(layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(lblNganh)

.addComponent(rbtCNTT)

.addComponent(rbtKinhTe))

.addGap(18, 18, 18)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(lblMaSV)

.addComponent(txtMa, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(lblTen)

.addComponent(txtTen, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lblGioiTinh)

.addComponent(rbtNam)

.addComponent(rbtNu))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(lblMon1)

.addComponent(txtMon1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(lblMon2)

.addComponent(txtMon2, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(lblMon3)

.addComponent(txtMon3, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))

.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0,
Short.MAX_VALUE))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(btnDocFile)

.addComponent(btnThem)

.addComponent(btnUpdate))

.addComponent(Delete, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))

.addContainerGap(42, Short.MAX_VALUE))

);

pack();

}// </editor-fold>
private void rbtCNTTActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

lblMon1.setText("Java");

lblMon2.setText("Css");

lblMon3.setVisible(true);

txtMon3.setVisible(true);

private void txtMaActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

private void txtTenActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

private void btnDocFileActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

JFileChooser fc= new JFileChooser();

fc.showOpenDialog(null);

String tenFile=fc.getSelectedFile().getPath();

a.docFile(tenFile);

loadData(a.getA());

private void tblHienThiMouseClicked(java.awt.event.MouseEvent evt) {

// TODO add your handling code here:

int i = tblHienThi.getSelectedRow();

ArrayList<SinhVien> b = a.getA();
SinhVien y = b.get(i);

if(y instanceof CNTT){

rbtCNTT.setSelected(true);

txtMon1.setText(""+((CNTT) y).getJava());

txtMon2.setText(""+((CNTT) y).getCss());

txtMon3.setText(""+((CNTT) y).getHtml());

lblMon3.setVisible(true);

txtMon3.setVisible(true);

else{

rbtKinhTe.setSelected(true);

txtMon1.setText(""+((KinhTe) y).getNlkt());

txtMon2.setText(""+((KinhTe) y).getMarketing());

lblMon3.setVisible(false);

txtMon3.setVisible(false);

txtMa.setText(y.getMa());

txtTen.setText(y.getHoTen());

if(y.isGioiTinh())

rbtNam.setSelected(true);

else rbtNu.setSelected(true);

private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {

private void jButtonDeleteActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

}
private void btnThemActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

String ma= txtMa.getText();

String HoTen= txtTen.getText();

boolean gt = rbtNam.isSelected();

double mon1= Double.parseDouble(txtMon1.getText());

double mon2= Double.parseDouble(txtMon2.getText());

double html=0;

try{

html = Double.parseDouble(txtMon3.getText());

}catch (Exception e){

SinhVien y;

if(rbtCNTT.isSelected()) y = new CNTT(mon1, mon2,html,ma,HoTen,gt);

else y = new KinhTe(mon1, mon2,ma, HoTen, gt);

if(a.them(y))

loadData(a.getA());

else JOptionPane.showMessageDialog(null,"Da co ma "+ma+" trong DS, KHONG THEM");

private void rbtKinhTeActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

lblMon1.setText("NLKT");

lblMon2.setText("Marketing");

lblMon3.setVisible(false);

txtMon3.setVisible(false);

// public void loadData(ArrayList<SinhVien> b){


// }

/**

* @param args the command line arguments

*/

public static void main(String args[]) {

/* Set the Nimbus look and feel */

//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">

/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.

* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html

*/

try {

for (javax.swing.UIManager.LookAndFeelInfo info :


javax.swing.UIManager.getInstalledLookAndFeels()) {

if ("Nimbus".equals(info.getName())) {

javax.swing.UIManager.setLookAndFeel(info.getClassName());

break;

} catch (ClassNotFoundException ex) {

java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);

} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);

} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);

} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);

//</editor-fold>
/* Create and display the form */

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new GUI().setVisible(true);

});

// Variables declaration - do not modify

private javax.swing.JButton Delete;

private javax.swing.JButton btnDocFile;

private javax.swing.JButton btnThem;

private javax.swing.JButton btnUpdate;

private javax.swing.ButtonGroup buttonGroup1;

private javax.swing.ButtonGroup buttonGroup2;

private javax.swing.JScrollPane jScrollPane1;

private javax.swing.JLabel lblGioiTinh;

private javax.swing.JLabel lblMaSV;

private javax.swing.JLabel lblMon1;

private javax.swing.JLabel lblMon2;

private javax.swing.JLabel lblMon3;

private javax.swing.JLabel lblNganh;

private javax.swing.JLabel lblTen;

private javax.swing.JRadioButton rbtCNTT;

private javax.swing.JRadioButton rbtKinhTe;

private javax.swing.JRadioButton rbtNam;

private javax.swing.JRadioButton rbtNu;

private javax.swing.JTable tblHienThi;

private javax.swing.JTextField txtMa;

private javax.swing.JTextField txtMon1;


private javax.swing.JTextField txtMon2;

private javax.swing.JTextField txtMon3;

private javax.swing.JTextField txtTen;

// End of variables declaration

private void loadData(ArrayList<SinhVien> a) {

while(dtm.getRowCount()>0) dtm.removeRow(0);

for(SinhVien x:a){

if(x instanceof CNTT)

dtm.addRow(new Object[]{"CNTT",x.getMa(),x.getHoTen(),x.isGioiTinh(),((CNTT)
x).getJava(), ((CNTT) x).getCss(), ((CNTT) x).getHtml(), x.getDiemTB(), x.xepLoai()});

if(x instanceof KinhTe)

dtm.addRow(new Object[]{"KinhTe",x.getMa(),x.getHoTen(),x.isGioiTinh(),((KinhTe)
x).getNlkt(), ((KinhTe) x).getMarketing()," ", x.getDiemTB(), x.xepLoai()});

You might also like