Bình luận Thành Lâm [T1907A]2020-04-27 15:13:16
/*
- To change this license header, choose License Headers in Project Properties.
- To change this template file, choose Tools | Templates
- and open the template in the editor.
/
package Baitap;
import java.util.Scanner;
/*
*
- @author Thannh Lam
*/
public class Student extends Person {
String rollNo;
float mark;
String email;
public Student() {
}
@Override
public void inputInfo(){
super.inputInfo();
Scanner input = new Scanner(System.in);
System.out.println("Nhập mã sinh viên");
//Code đầy đủ
while(true){
String rollNoInput = input.nextLine();
boolean check = setRollNo(rollNoInput);
if(check){
break;
}
}
//Code ngắn gọn
// while(!setRollNo(input.nextLine()));
System.out.println("Nhập điểm sinh viên: ");
while(true){
float markInput = Float.parseFloat(input.nextLine());
boolean check = setMark(markInput);
if(check){
break;
}
}
System.out.println("Nhập email sinh viên: ");
while(true){
String markEmail = input.nextLine();
boolean check = setEmail(markEmail);
if(check){
break;
}
}
}
public String getRollNo() {
return rollNo;
}
public boolean setRollNo(String rollNo) {
if(rollNo != null && rollNo.length() == 8 ){
this.rollNo = rollNo;
return true;
}else{
System.out.println("Nhập Lại Mã Sinh Viên ");
}
this.rollNo = rollNo;
return false;
}
public float getMark() {
return mark;
}
public boolean setMark(float mark) {
if(mark >=0 && mark <=10) {
this.mark = mark;
return true;
}else{
System.err.println("Nhập lại điểm (điểm >=0 <=10) : ");
return false;
}
}
public String getEmail() {
return email;
}
public boolean setEmail(String email) {
if(email != null && email.contains("@") && !email.contains(" ")){
this.email = email;
return true;
} else{
System.out.println("Nhập lại đại chỉ email");
return false;
}
}
public boolean checkSchoolarship() {
if(mark >=8) {
return true;
}
return false;
}
}
/*
- To change this license header, choose License Headers in Project Properties.
- To change this template file, choose Tools | Templates
- and open the template in the editor.
/
package Baitap;
import java.util.Scanner;
/*
*
- @author Thannh Lam
*/
public class Person {
String name;
String gender;
String birthday;
String address;
public Person() {
}
public Person(String name, String gender, String birthday, String address) {
this.name = name;
this.gender = gender;
this.birthday = birthday;
this.address = address;
}
public void inputInfo() {
Scanner input = new Scanner(System.in);
System.out.println("Nhập tên: ");
name = input.nextLine();
System.out.println("Nhập giới tính: ");
gender = input.nextLine();
System.out.println("Nhập ngày sinh: ");
birthday = input.nextLine();
System.out.println("Nhập địa chỉ: ");
address = input.nextLine();
}
public void showInfo(){
System.out.println("Tên: " + name + "; giới tính: " + gender+ ";ngày sinh: " +birthday+";địa chỉ: " + address);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
/*
- To change this license header, choose License Headers in Project Properties.
- To change this template file, choose Tools | Templates
- and open the template in the editor.
/
package Baitap;
/*
*
- @author Thannh Lam
*/
public class StudentTest {
public static void main(String[] args) {
ArrayList<Student> studentList = new ArrayList<>();
int choose;
Scanner scan = new Scanner(System.in);
do{
showMenu();
System.out.println("Choose : ");
choose = Integer.parseInt(scan.nextLine());
switch(choose){
case 1:
int n;
System.out.println("Nhập số sinh viên cần thêm: ");
n = Integer.parseInt(scan.nextLine());
for(int i = 0; i< n; i++){
Student std = new Student();
std.inputInfo();
studentList.add(std);
}
break;
case 2:
for(int i = 0; i < studentList.size();i++){
studentList.get(i).showInfo();
}
break;
case 3:
int minIndex = 0, maxIndex = 0;
float minMark, maxMark;
minMark = studentList.get(0).getMark();
maxMark = studentList.get(0).getMark();
for(int i = 1; i<studentList.size(); i++){
if(studentList.get(i).getMark() < minMark){
minIndex = i;
minMark = studentList.get(i).getMark();
}
if(studentList.get(i).getMark() > maxMark) {
maxIndex = i;
maxIndex = studentList.get(i).getMark();
}
}
System.out.println("Sinh viên có điểm trung bình cao nhất.");
studentList.get(maxIndex).showInfo();
System.out.println("Sinh Viên có điểm trung bình thấp nhất");
studentList.get(minIndex).showInfo();
break;
case 4:
String rollNoSearch = scan.nextLine();
int count = 0;
System.out.println("Nhập rollNo cần tìm kiếm");
for(Student student: studentList){
if(student.getRollNo().equalsIgnoreCase(rollNoSearch)){
student.showInfo();
}
}
if(count == 0){
System.out.println("Không tìm thấy sinh viên nào");
}
break;
case 5:
//Buoc 1: Sắp xếp thông tin sinh viên theo A-Z
System.out.println("");
break;
case 6:
System.out.println("");
break;
case 7:
System.out.println("GoodBye");
break;
default:
System.err.println("Nhập sai:");
break;
}
}while(choose != 7);
}
static void showMenu(){
System.out.println("1.Nhập vào N sinh viên");
System.out.println("2. Hiển thị thông tin sinh viên");
System.out.println("3.Hiển thị max và min theo điểm trung bình");
System.out.println("4.Tìm kiếm theo mã sinh viên");
System.out.println("5.Sort A-Z theo tên sinh viên và hiển thị");
System.out.println("6. Hiển thị sinh viên được học bổng & sắp xếp điểm cao xuống thấp");
}
}
hoangkhiem [C1907L]2020-04-15 11:34:39
/*
- To change this license header, choose License Headers in Project Properties.
- To change this template file, choose Tools | Templates
- and open the template in the editor.
/
package BaiTap273;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
/*
*
- @author Admin
/
public class StudentFrame extends javax.swing.JFrame {
private ArrayList<Student1> list;
DefaultTableModel model;
/*
- Creates new form StudentFrame
/
public StudentFrame() {
initComponents();
model = (DefaultTableModel) tblStudent.getModel();
list = new ArrayList<>();
}
/*
- 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() {
jScrollPane1 = new javax.swing.JScrollPane();
tblStudent = new javax.swing.JTable();
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
txtRollNo = new javax.swing.JTextField();
txtfullname = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jLabel3 = new javax.swing.JLabel();
cbgender = new javax.swing.JComboBox<>();
jButton3 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
tblStudent.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"No", "Roll No", "Full Name", "Gender"
}
));
jScrollPane1.setViewportView(tblStudent);
jPanel1.setBackground(new java.awt.Color(51, 255, 153));
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Khiêm Đẹp Trai"));
jLabel1.setText("RollNo : ");
jLabel2.setText("Full Name : ");
txtfullname.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtfullnameActionPerformed(evt);
}
});
jButton1.setText("Save");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("Reset");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jLabel3.setText("Gender : ");
cbgender.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Nam", "Nữ" }));
jButton3.setText("Search");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel3)
.addComponent(jLabel1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(txtRollNo, javax.swing.GroupLayout.DEFAULT_SIZE, 218, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(txtfullname)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(cbgender, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 147, Short.MAX_VALUE))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(26, 26, 26)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel1)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txtRollNo, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1)))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel2)
.addComponent(jButton2)
.addComponent(txtfullname))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(cbgender, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton3))
.addContainerGap(66, Short.MAX_VALUE))
);
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)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
pack();
}// </editor-fold>
private void txtfullnameActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
System.out.println("Save");
String rollno = txtRollNo.getText();
String fullname = txtfullname.getText();
String gender = cbgender.getSelectedItem().toString();
if (rollno.isEmpty() || fullname.isEmpty()) {
JOptionPane.showMessageDialog(rootPane, "Mời nhập thông tin");
return;
}
for (Student1 student1 : list) {
if (student1.getRollno().equalsIgnoreCase(rollno)) {
update();
return;
}
}
Student1 s = new Student1();
s.setRollno(txtRollNo.getText());
s.setFullname(txtfullname.getText());
s.setGender(cbgender.getSelectedItem().toString());
list.add(s);
showReSult();
}
int i = 1;
private void update() {
for (int row = 0; row < tblStudent.getRowCount(); row++) {
if (tblStudent.getValueAt(row, 1).toString().equalsIgnoreCase(txtRollNo.getText())) {
tblStudent.setValueAt(txtfullname.getText(), row, 2);
tblStudent.setValueAt(cbgender.getSelectedItem().toString(), row, 3);
return;
}
}
}
private void showReSult() {
Student1 s = list.get(list.size() - 1);
model.addRow(new Object[]{
i++, s.getRollno(), s.getFullname(), s.getGender()
});
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
System.out.println("Reset");
txtRollNo.setText("");
txtfullname.setText("");
cbgender.setSelectedIndex(0);
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
System.out.println("Search");
String seach = JOptionPane.showInputDialog("Mời nhập Rollno cần tìm");
for (int row = 0; row < tblStudent.getRowCount(); row++) {
if(tblStudent.getValueAt(row, 1).toString().equalsIgnoreCase(seach)){
txtRollNo.setText(tblStudent.getValueAt(row, 1).toString());
txtfullname.setText(tblStudent.getValueAt(row, 2).toString());
int gend =(tblStudent.getValueAt(row, 3).toString().equals("Nam") )? 0 :1;
cbgender.setSelectedIndex(gend);
}
}
}
/*
- @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(StudentFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(StudentFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(StudentFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(StudentFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/ Create and display the form */
java.awt.EventQueue.invokeLater(() -> {
new StudentFrame().setVisible(true);
});
}
// Variables declaration - do not modify
private javax.swing.JComboBox<String> cbgender;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable tblStudent;
private javax.swing.JTextField txtRollNo;
private javax.swing.JTextField txtfullname;
// End of variables declaration
}
/*
- To change this license header, choose License Headers in Project Properties.
- To change this template file, choose Tools | Templates
- and open the template in the editor.
/
package BaiTap273;
/*
*
- @author Admin
*/
public class Student1 {
String rollno , fullname,gender;
public Student1() {
}
public Student1(String rollno, String fullname, String gender) {
this.rollno = rollno;
this.fullname = fullname;
this.gender = gender;
}
public String getRollno() {
return rollno;
}
public void setRollno(String rollno) {
this.rollno = rollno;
}
public String getFullname() {
return fullname;
}
public void setFullname(String fullname) {
this.fullname = fullname;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
Nguyễn Hoàng Anh [C1907L]2020-04-14 12:09:31
/*
- To change this license header, choose License Headers in Project Properties.
- To change this template file, choose Tools | Templates
- and open the template in the editor.
/
package MyFistJavaSwing;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
/*
*
- @author Redmibook 14
/
public class FistJVSwing extends javax.swing.JFrame {
/*
- Creates new form FistJVSwing
/
DefaultTableModel TableModel;
public FistJVSwing() {
initComponents();
TableModel = (DefaultTableModel) tblStudent.getModel();
}
List<Student> ArrStudents = new ArrayList<>();
/*
- 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() {
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
javax.swing.JPanel jPanel1 = new javax.swing.JPanel();
boxGender = new javax.swing.JComboBox<>();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
TextRollNo = new javax.swing.JTextField();
txtFullName = new javax.swing.JTextField();
btnSave = new javax.swing.JButton();
btnReset = new javax.swing.JButton();
jLabel4 = new javax.swing.JLabel();
btnSearch = new javax.swing.JButton();
jScrollPane2 = new javax.swing.JScrollPane();
tblStudent = new javax.swing.JTable();
jTable1.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"
}
));
jScrollPane1.setViewportView(jTable1);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Input Student's Detail Infomation"));
boxGender.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Male", "Female" }));
jLabel1.setText("RollNo :");
jLabel2.setText("Full Name :");
btnSave.setText("Save");
btnSave.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSaveActionPerformed(evt);
}
});
btnReset.setText("Reset");
btnReset.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnResetActionPerformed(evt);
}
});
jLabel4.setText("Gender :");
btnSearch.setText("Search");
btnSearch.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSearchActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel2)
.addComponent(jLabel4))
.addGap(32, 32, 32)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(TextRollNo, javax.swing.GroupLayout.PREFERRED_SIZE, 336, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtFullName))
.addGap(43, 43, 43))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(boxGender, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btnSearch, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnReset, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(btnSave, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE)))
.addGap(21, 21, 21))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(TextRollNo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1)
.addComponent(btnSave))
.addGap(26, 26, 26)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(txtFullName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnReset))
.addGap(25, 25, 25)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(boxGender, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnSearch))
.addContainerGap(30, Short.MAX_VALUE))
);
tblStudent.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"No", "RollNo", "Full Name", "Gender"
}
) {
boolean[] canEdit = new boolean [] {
false, false, true, false
};
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
jScrollPane2.setViewportView(tblStudent);
if (tblStudent.getColumnModel().getColumnCount() > 0) {
tblStudent.getColumnModel().getColumn(0).setResizable(false);
tblStudent.getColumnModel().getColumn(1).setResizable(false);
tblStudent.getColumnModel().getColumn(2).setResizable(false);
tblStudent.getColumnModel().getColumn(3).setResizable(false);
}
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)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jScrollPane2))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
pack();
}// </editor-fold>
private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String rollNo = TextRollNo.getText();
String fullName = txtFullName.getText();
String gender = (String) boxGender.getSelectedItem();
Student student = new Student(rollNo, fullName, gender);
if(TextRollNo.getText().isEmpty()){
JOptionPane.showMessageDialog(null, "RollNo are not supposed to be empty.");
return;
}
int flag = 0;
for (int i = 0; i < ArrStudents.size(); i++) {
if (ArrStudents.get(i).RollNo.equals(rollNo)) {
ArrStudents.get(i).FullName = fullName;
ArrStudents.get(i).Gender = gender;
updateTable();
flag++;
break;
}
}
if (flag == 0) {
ArrStudents.add(student);
updateTable();
}
}
private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
TextRollNo.setText("");
txtFullName.setText("");
boxGender.setSelectedIndex(0);
}
private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String rollNo = TextRollNo.getText();
for (int i = 0; i < ArrStudents.size(); i++) {
if (TableModel.getValueAt(i, 1).equals(rollNo)) {
txtFullName.setText(TableModel.getValueAt(i, 2).toString());
if (TableModel.getValueAt(i, 3).toString().equals("Male")) {
boxGender.setSelectedIndex(0);
}
if (TableModel.getValueAt(i, 3).toString().equals("Female")) {
boxGender.setSelectedIndex(1);
}
}
}
}
private void updateTable() {
TableModel.setRowCount(0);
for (int i = 0; i < ArrStudents.size(); i++) {
TableModel.addRow(new Object[]{TableModel.getRowCount() + 1, ArrStudents.get(i).RollNo, ArrStudents.get(i).FullName, ArrStudents.get(i).Gender});
}
}
/*
- @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(FistJVSwing.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(FistJVSwing.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(FistJVSwing.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(FistJVSwing.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 FistJVSwing().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JTextField TextRollNo;
private javax.swing.JComboBox<String> boxGender;
private javax.swing.JButton btnReset;
private javax.swing.JButton btnSave;
private javax.swing.JButton btnSearch;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel4;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTable jTable1;
private javax.swing.JTable tblStudent;
private javax.swing.JTextField txtFullName;
// End of variables declaration
}
/*
- To change this license header, choose License Headers in Project Properties.
- To change this template file, choose Tools | Templates
- and open the template in the editor.
/
package MyFistJavaSwing;
/*
*
- @author Redmibook 14
*/
public class Student {
String RollNo,FullName,Gender;
public Student() {
}
public Student(String RollNo, String FullName, String Gender) {
this.RollNo = RollNo;
this.FullName = FullName;
this.Gender = Gender;
}
public String getRollNo() {
return RollNo;
}
public String getFullName() {
return FullName;
}
public String getGender() {
return Gender;
}
public void setRollNo(String RollNo) {
this.RollNo = RollNo;
}
public void setFullName(String FullName) {
this.FullName = FullName;
}
public void setGender(String Gender) {
this.Gender = Gender;
}
}
Ngô Quang Huy [C1907L]2020-04-13 14:24:32
/*
- To change this license header, choose License Headers in Project Properties.
- To change this template file, choose Tools | Templates
- and open the template in the editor.
/
package April13JavaSwing;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
/*
*
- @author Administrator
/
public class MainFrame extends javax.swing.JFrame {
DefaultTableModel tableModel;
/*
- Creates new form MainFrame
/
public MainFrame() {
initComponents();
tableModel = (DefaultTableModel) Table.getModel();
}
/*
- 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() {
jMenu1 = new javax.swing.JMenu();
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
Roll = new javax.swing.JTextField();
FullName = new javax.swing.JTextField();
Drop = new javax.swing.JComboBox<>();
Save = new javax.swing.JButton();
Reset = new javax.swing.JButton();
Search = new javax.swing.JButton();
jLabel4 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
Table = new javax.swing.JTable();
jMenu1.setText("jMenu1");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBackground(new java.awt.Color(204, 255, 204));
jPanel1.setToolTipText("");
jPanel1.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
jPanel1.setName(""); // NOI18N
jLabel1.setText("Roll No:");
jLabel2.setText("Full Name:");
jLabel3.setText("Gender:");
Drop.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Male", "Female" }));
Save.setText("Save");
Save.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SaveActionPerformed(evt);
}
});
Reset.setText("Reset");
Reset.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ResetActionPerformed(evt);
}
});
Search.setText("Search");
Search.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SearchActionPerformed(evt);
}
});
jLabel4.setText("Input Student's Detail Information");
jLabel4.setToolTipText("");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 239, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(FullName, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Roll, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Drop, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(19, 19, 19)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(28, 28, 28)
.addComponent(Reset, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Search, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Save, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)))))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel4)
.addGap(52, 52, 52)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Roll, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Save))
.addGap(27, 27, 27)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(FullName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Reset))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Drop, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Search))
.addContainerGap(29, Short.MAX_VALUE))
);
Table.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"No", "Roll No", "Full Name", "Gender"
}
));
jScrollPane1.setViewportView(Table);
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(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(0, 4, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void ResetActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Roll.setText("");
FullName.setText("");
Drop.setSelectedIndex(0);
}
private void SaveActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
// System.out.println("Ok");
String rollNumber = Roll.getText();
String fullname = FullName.getText();
if(rollNumber.isEmpty() || fullname.isEmpty()){
System.out.println("Enter fullname & rollno");
JOptionPane.showMessageDialog(rootPane, "Enter fullname & rollno");
return;
}
String gender = Drop.getSelectedItem().toString();
tableModel.addRow(new Object[] {tableModel.getRowCount() + 1, rollNumber, fullname, gender});
}
private void SearchActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String searchStr = JOptionPane.showInputDialog("Please input roll number");
for (int row = 0; row < Table.getRowCount();row++) {
if (Table.getValueAt(row, 1).toString().equalsIgnoreCase(searchStr)) {
Roll.setText(Table.getValueAt(row, 1).toString());
FullName.setText(Table.getValueAt(row, 2).toString());
int gend = (Table.getValueAt(row, 3).toString().equals("Male")) ? 0 : 1;
Drop.setSelectedIndex(gend);
return;
}
}
JOptionPane.showMessageDialog(rootPane, "Not found");
}
/*
- @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(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MainFrame.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 MainFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JComboBox<String> Drop;
private javax.swing.JTextField FullName;
private javax.swing.JButton Reset;
private javax.swing.JTextField Roll;
private javax.swing.JButton Save;
private javax.swing.JButton Search;
private javax.swing.JTable Table;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JMenu jMenu1;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration
}
`
![](https://www.gravatar.com/avatar/67c23432e4710f33dd14e580d41b0379.jpg?s=80&d=mm&r=g)
##### trung \[C1907L\]
2020-04-13 14:15:24
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
/* - To change this license header, choose License Headers in Project Properties.
- To change this template file, choose Tools | Templates
- and open the template in the editor.
/
/*
*
- @author prdox
/
public class lession13 extends javax.swing.JFrame {
DefaultTableModel tableModel;
List<Student> studentList = new ArrayList<>();
/*
undefined
/* - To change this license header, choose License Headers in Project Properties.
- To change this template file, choose Tools | Templates
- and open the template in the editor.
/
/*
*
- @author prdox
*/
public class Student {
String rollno, fullname, gender;
public Student() {
}
public Student(String rollno, String fullname, String gender) {
this.rollno = rollno;
this.fullname = fullname;
this.gender = gender;
}
public String getRollno() {
return rollno;
}
public void setRollno(String rollno) {
this.rollno = rollno;
}
public String getFullname() {
return fullname;
}
public void setFullname(String fullname) {
this.fullname = fullname;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
`
|