⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 addnewstudent.java

📁 JAVA CLONE 用于CLASS范式的CLONE
💻 JAVA
字号:
package frame;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;

import baseClass.Class;
import baseClass.Student;

public class AddNewStudent extends JFrame{
	/**
	 * the fram of add new student
	 * @param jfram
	 */
	public AddNewStudent(final ManagerSystem jfram)
	{
	    Container container = getContentPane();  
	    container.setLayout(new GridBagLayout());
	    final JLabel lblmessage=new JLabel();
	    JLabel lblId=new JLabel("sutdent id:");
	    JLabel lblname=new JLabel("name:");
	    JLabel lblclass=new JLabel("select class:");
	    final JTextArea txtid=new JTextArea();
	    final JTextArea txtname=new JTextArea();
	    final JComboBox classlist=new JComboBox(getclassdate());
	    classlist.setSelectedIndex(0);
	    JButton btnadd=new JButton("add");
	    btnadd.addActionListener(new ActionListener() {
	     	     public void actionPerformed(ActionEvent e) {
	     	    	 //if(txtid.getText())
	     	    	 if(classlist.getSelectedIndex()==0)//not select a class
	     	    	 {
	     	    		lblmessage.setText("please select a class");
	     	    	 }
	     	    	 else
	     	    	 {
	     	    	 		String selectname=classlist.getSelectedItem().toString();
	     	    			String cid=getclassid(selectname);
	     	    			Student tempclass=new Student(txtid.getText(),txtname.getText(),cid);
	     	    			
	     	    			int result=tempclass.addnewStudent(tempclass);
	     	    			switch(result)
	     	    			{
	     	    			case 3:
	     	    				//MessageDialog box=new MessageDialog();
	     	    				lblmessage.setText("add sucess!");	     	    				
	     	    				jfram.myupdate(tempclass.getStudentId(),1);
	     	    				break;
	     	    			case 2:
	     	    				lblmessage.setText("id exsit!");
	     	    				break;
	     	    			case 1:
	     	    				lblmessage.setText("classid and student id must be number!");
	     	    				break;
	     	    			default:
	     	    				break;
	     	    			}
	     	    	 }
	   	         }});
	    
	    JButton btncancle=new JButton("cancle");
	    btncancle.addActionListener(new ActionListener() {
    	     public void actionPerformed(ActionEvent e) {
    	    	 setVisible(false);
  	         }});
	    container.add(lblId,new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 5, 0, 0), 0, 0));
	    container.add(txtid,new GridBagConstraints(1, 0, 5, 1, 0.0, 0.0,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 5, 0, 0), 0, 0));
	    container.add(lblname,new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 0), 0, 0));
	    container.add(txtname,new GridBagConstraints(1, 1, 5, 1, 0.0, 0.0,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 0), 0, 0));
	    container.add(lblclass,new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 0), 0, 0));
	    container.add(classlist,new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 0), 0, 0));
	    container.add(btnadd,new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 0), 0, 0));
	    container.add(btncancle,new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 0), 0, 0));
	    container.add(lblmessage,new GridBagConstraints(0, 4, 5, 1, 0.0, 0.0,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 5, 0, 0), 0, 0));    
	    this.setVisible(true);
	    this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	    initSizeAndLocation(this,200,200);
	    //setFrameAlwaysFocus(this);
	}
	public static void main(String[] args)		
	{
		//new AddNewClass();
	}
    public static void initSizeAndLocation(Window window, int w, int h) {
        window.setSize(w, h);
        Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
        window.setLocation(size.width / 2 - w / 2, size.height / 2 - h / 2);
    }
    /**
     * Let window has been front
     * @param frame
     */
	public static void setFrameAlwaysFocus(final JFrame frame) {
        new javax.swing.Timer(100, new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                frame.toFront();
            }
        }).start();
    }
	/**
	 * load date
	 * @return
	 */
	public String[] getclassdate()
	{
		ArrayList ALLCLASS=ManagerSystem.ALLCLASS;
		int num=ALLCLASS.size();
	    String[] temp=new String[num+1];
	    temp[0]="please select a class";
	    for (int i=0; i <num ; i++) {
	    	Class tempclass=((Class)ALLCLASS.get(i));
	    	temp[i+1]=tempclass.getClassName();
	    }	    
	    return temp;
	}
	public String getclassid(String name)
	{
		ArrayList ALLCLASS=ManagerSystem.ALLCLASS;
		int num=ALLCLASS.size();
	    for (int i=0; i <num ; i++) 
	    {
	    	Class tempclass=((Class)ALLCLASS.get(i));
	    	String tempclassname=tempclass.getClassName();
	    	if(tempclassname.equals(name))
	    	{
	    		return tempclass.getClassId();
	    	}
	    }	    
	    return "0";
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -