01/10/2018, 08:17

Tạo giao diện cho Java

Dưới đây là đoạn code đã hoàn chỉnh của mình và mình muốn tao giao diện cho sản phẩm này. Nhưng mình không biết cách gán đoạn code này vào JFrame vậy nên mình mong các bạn giúp mình giải quyết cái này!!!

/*
 * 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 javaapplication42;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Objects;
import java.util.Scanner;

class Information
{
	private String stdID;
	private String stdName;
	private Date stdDob;
	private String stdEmail;
	private String stdPhone;
	private String stdAddress;
	private String stdBatch;
	
	// set&get ID
	void setstdID(String ID)
	{
		stdID = ID;
	}
	
	String getstdID()
	{
		return stdID;
	}
	
	// set&get Name
	void setstdName(String name){
		stdName = name;
	}
	
	String getstdName(){
		return stdName;
	}
	
	// set&get Day of birth
	void setstdDob(Date date)
	{
		stdDob = date;
	}
	
	Date getstdDob()
	{
		return stdDob;
	}
	
	// set&get Email
	void setstdEmail(String email)
	{
		stdEmail = email;
	}
	
	String getstdEmail()
	{
		return stdEmail;
	}
	
	// set&get Phone
	void setstdPhone(String phone)
	{
		stdPhone = phone;
	}
	
	String getstdPhone()
	{
		return stdPhone;
	}
	
	// set&get Address
	void setstdAddress(String address)
	{
		stdAddress = address;
	}
	
	String getstdAddress()
	{
		return stdAddress;
	}
	
	// set&get Batch
	void setstdBatch(String batch)
	{
		stdBatch = batch;
	}
	
	String getstdBatch()
	{
		return stdBatch;
	}
	//
	void viewAllStudents()
	{
		SimpleDateFormat dt1 = new SimpleDateFormat("dd/MM/yyyy");
		System.out.println("1. Student ID:" + stdID);
		System.out.println("2. Student Name:" + stdName);
		System.out.println("3. Student Dob:" + dt1.format(stdDob));
		System.out.println("4. Student Email:" + stdEmail);
		System.out.println("5. Student Phone:" + stdPhone);
		System.out.println("6. Student Address:" + stdAddress);
		System.out.println("7. Student Batch:" + stdBatch);
	}
}
class students
{
	Scanner scan = new Scanner(System.in);
	public ArrayList<Information> allStudent = new ArrayList<Information>();
	SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
	checkinformation checking;
	
	
	void addnewstudents(Information infor){
	checking = new checkinformation();
	// input**
	System.out.println("Fill in all the infortion.");
	
	
	// input and check ID
	while(true){
	System.out.println("1. Student ID(Inform: GTxxxxx or GCxxxxx(x: is a digit)): ");
	infor.setstdID(scan.nextLine());
	checking.checkID(infor.getstdID());
	if (checking.checkID(infor.getstdID()) == true)
	{
		boolean checktrung = false;
		for (int n = 0; n < allStudent.size(); n++)
			{
				checktrung = Objects.equals(infor.getstdID(), allStudent.get(n).getstdID());
				if (checktrung == true)
				{
					break;
				}
			}
		if (checktrung == true)
		{
			System.out.println("The ID already existed.");
		}
		else
			break;
	}
	else 
		System.out.println("Unvalid ID");
	}
	
	
	//input and check name
	while(true){
	System.out.println("2. Student Name: ");
	infor.setstdName(scan.nextLine());
	checking.checkName(infor.getstdName());
	if (checking.checkName(infor.getstdName()) == false)
		break;
	else
		System.out.println("Invalid Name.");
	}
	
	
	//Check date**
	while(true){
	System.out.println("3. Student Dob: ");
	String date = scan.nextLine();
	try{
		infor.setstdDob(dateFormat.parse(date));
		break;
	}catch(ParseException e){System.out.println("The date isn't valid
Input again");}
	}
	// Using Loop to ask user input until it's true**
	
	
	//input and check Email
	while(true){
	System.out.println("4. Student Email: ");
	infor.setstdEmail(scan.nextLine());
	checking.checkEmail(infor.getstdEmail());
	if (checking.checkEmail(infor.getstdEmail()) == true)
		break;
	else
		System.out.println("Invalid Email.");
	}
	
	
	//input and check phone number
	while(true){
	System.out.println("5. Student Phone: ");
	infor.setstdPhone(scan.nextLine());
	checking.checkPhone(infor.getstdPhone());
	if (checking.checkPhone(infor.getstdPhone()) == true)
		break;
	else
		System.out.println("Invalid Phone Number");
	}
	
	
	System.out.println("6. Student Address: ");
	infor.setstdAddress(scan.nextLine());
	System.out.println("7. Student Batch: ");
	infor.setstdBatch(scan.nextLine());
	
	// Save information of student into the array list** 
	allStudent.add(infor);
	}
	
	void seeallstudents()
	{
		int i = 1;
		System.out.println("=============================");
		if(allStudent.size() == 0){
			System.out.println("The student list is empty.");
		}
		else{
			for (Information infor : allStudent)
			{
				System.out.println("STT: " + i);
				infor.viewAllStudents();
				if (allStudent.size() > 1)
					System.out.println("------------------------");
				i++;
			}
		}
	}
	void searchInformation()
	{
		//The place where user input information
		System.out.println("Student Name: ");
		int find = 0;
		String searchName = scan.nextLine();
		for (int i = 0; i < allStudent.size(); i++)
		{
			// Checking
			if (allStudent.get(i).getstdName().indexOf(searchName) != -1)
			{
				if (find > 0)
					System.out.println("------------------------");
				System.out.println("STT: " + find+1);
				allStudent.get(i).viewAllStudents();
				find++;
			}
		}
		// If can not find out the name
		if (find == 0)
		System.out.println("There's no such name");
	}
	void deleteStudent()
	{
		System.out.println("Student ID: ");
		String inputID = scan.nextLine();
		boolean confirm = false;
		// the condition means looping for each object of allStudent
		for (Information infor : allStudent)
		{
			if (infor.getstdID().equals(inputID))
			{
				allStudent.remove(infor);
				System.out.println("Delete Successfully");
				confirm = true;
				break;
			}
		}
		if (confirm == false)
		{
			System.out.println("Unavailable ID");
		}
	}
	void updateStudent()
	{
		System.out.println("Student ID: ");
		String inputID = scan.nextLine();
		boolean confirm = false;
		// the condition means looping for each object of allStudent
		for (Information infor : allStudent)
		{
			if (infor.getstdID().equals(inputID))
			{
				System.out.println("The current data
---------------------------");
				infor.viewAllStudents();
				System.out.println("---------------------");
				System.out.println("Choose one of these concepts  ");
				System.out.println("1. Name");
				System.out.println("2. DOB");
				System.out.println("3. Email");
				System.out.println("4. Phone");
				System.out.println("5. Address");
				System.out.println("6. Batch");
				System.out.println("---------------------");
				System.out.println("Choose: ");
				int choosen;
				choosen = scan.nextInt();
				System.out.println("---------------------");
				Scanner input = new Scanner(System.in);
				switch(choosen)
				{
				case 1:
					String name;
					System.out.println("Name: ");
					name = input.nextLine();
					infor.setstdName(null);
					infor.setstdName(name);
					System.out.println("Update successfully");
					break;
				case 2:
					System.out.println("Day of birth: ");
					String date = input.nextLine();
					infor.setstdDob(null);
					try{
						infor.setstdDob(dateFormat.parse(date));
						System.out.println("Update successfully");
						break;
					}catch(ParseException e){System.out.println("The date isn't valid");}
				case 3:
					String email;
					System.out.println("Email: ");
					email = input.nextLine();
					infor.setstdEmail(null);
					infor.setstdEmail(email);
					System.out.println("Update successfully");
					break;
				case 4:
					String phone;
					System.out.println("Phone: ");
					phone = input.nextLine();
					infor.setstdPhone(null);
					infor.setstdPhone(phone);
					System.out.println("Update successfully");
					break;
				case 5:
					String Address;
					System.out.println("Address: ");
					Address = input.nextLine();
					infor.setstdAddress(null);
					infor.setstdAddress(Address);
					System.out.println("Update successfully");
					break;
				case 6:
					String batch;
					System.out.println("Batch: ");
					batch = input.nextLine();
					infor.setstdBatch(null);
					infor.setstdBatch(batch);
					System.out.println("Update successfully");
					break;
				}
				confirm = true;
			}
		}
		if (confirm == false)
		{
			System.out.println("Unavailable ID");
		}
	}
}
class checkinformation
{
		
	public static boolean isNumber(String str)  // check in ID and String
	{  
	  try  
	  {  
	    double d = Double.parseDouble(str);  
	  }  
	  catch(NumberFormatException nfe)  
	  {  
	    return false;  
	  }  
	  return true;  
	}
	
	
	boolean checkID(String id) // return true or false
	{
		int validateGT = id.indexOf("GT");
		int validateGC = id.indexOf("GC");
		int leng = id.length();
		String subcheck = id.substring(2, id.length());
		boolean checknumber= isNumber(subcheck);
		boolean checkfinal;
		if ((validateGT == -1 && validateGC == -1) || leng != 7 || checknumber == false)
			{
				checkfinal = false;
			}
		else
			{
				checkfinal = true;
			}
		return checkfinal;
	}
	
	
	boolean checkName(String name) // 'false' means the string is characters
	{
		boolean validatechecking;
		validatechecking = isNumber(name);
		return validatechecking;
	}
	
	
	boolean checkEmail(String email) // if the email doesn't contain '@' or '.'. 
	//The function will return false and opposite.
	{
            return email.indexOf("@") != -1 && email.indexOf(".") != -1;
	}
	
	
	boolean checkPhone(String phone)
	{
		boolean checkfinal = isNumber(phone);
		if (checkfinal == true && phone.length() > 8)
			return true;
		else
			return false;
	}
	
	
	boolean checklecID(String ID)
	{
		boolean checkfinal = isNumber(ID);
		int check1 = ID.length();
            return checkfinal == true && check1 == 8;
	}
}
/**
 *
 * @author Administrator
 */
public class JavaApplication42 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        while(true)
	{
		
		students student = new students();
		
		Scanner scan = new Scanner(System.in);
		
		char choosen = scan.next().charAt(0);
		switch(choosen)
		{
		case '1':
			boolean out = true;
			while(out == true)
			{
				
				System.out.println("Please choose : ");
                                System.out.println("Press 1 to add information: ");
                                System.out.println("Press 2 to view s tudent list: ");
                                System.out.println("Press 3 to search student: ");
                                System.out.println("Press 4 to delete student information: ");
                                System.out.println("Press 5 to update student information: ");    
				char choosen1 = scan.next().charAt(0);
				switch(choosen1)
				{
				case '1':
					student.addnewstudents(new Information());
					break;
				case '2':
					student.seeallstudents();
					break;
				case '3': 
					student.searchInformation();
					break;
				case '4':
					student.deleteStudent();
					break;
				case '5':
					student.updateStudent();
					break;
				case '6':
					out = false;
					break;
				default:
					System.out.println("==Invalid number==");
				}
			}
			break;
		case '2':
			boolean out1 = true;
			while(out1 == true)
			{
				
				System.out.println("Please choose: ");
                                System.out.println("Press 1 to add information: ");
                                System.out.println("Press 2 to view s tudent list: ");
                                System.out.println("Press 3 to search student: ");
                                System.out.println("Press 4 to delete student information: ");
                                System.out.println("Press 5 to update student information: ");  
				
			}
			break;
		case '3': 
			System.exit(0);
		default:
			System.out.println("==Invalid number==");
		}
	}
        
    }
    
}
Hidan viết 10:32 ngày 01/10/2018

mình nghĩ ko ai làm thế này

public ArrayList<Information> allStudent = new ArrayList<Information>();

information nên đổi tên thành human, rồi cho class student kế thừa

Bài liên quan
0