14-2

编写一个应用程序,用户分别从两个文本框输入学术的姓名和分数,程序按成绩排序将这些学生的姓名和分数显示在一个文本区中。

程序运行效果如图:14-2_第1张图片

public class student implements Comparable{  
	      
	    String name;  
	    double grade;  
	      
	    public student(String name,double grade) {  
	        // TODO Auto-generated constructor stub  
	        this.name = name;  
	        this.grade = grade;  
	    }  
	      
	    public String getName() {  
	        return name;  
	    }  
	    public void setName(String name) {  
	        this.name = name;  
	    }  
	    public double getGrade() {  
	        return grade;  
	    }  
	    public void setGrade(double grade) {  
	        this.grade = grade;  
	    }  
	    public int compareTo(student o) {  
	        // TODO Auto-generated method stub  
	        if(this.grade list = new ArrayList();  
    public WindowBoxLayout()  
    {  
        setLayout(new java.awt.FlowLayout());  
        init();  
        setVisible(true);  
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    }  
    void init(){  
        boxV1 = Box.createHorizontalBox();  
        boxV1.add(new Label("姓名"));  
        boxV1.add(Box.createHorizontalStrut(8));  
        text1 = new JTextField(8);  
        boxV1.add(text1);  
          
          
        boxv2 = Box.createHorizontalBox();  
        boxv2.add(new Label("成绩"));  
        boxv2.add(Box.createHorizontalStrut(8));  
        text2=new JTextField(8);  
        boxv2.add(text2);  
          
          
        boxV3 = Box.createHorizontalBox();  
        btn1 = new Button("添加");  
        boxV3.add(btn1);  
        boxV3.add(Box.createHorizontalStrut(8));  
        btn2 = new Button("排序");  
        boxV3.add(btn2);  
          
        boxV4 = Box.createHorizontalBox();  
        JTextArea text3 = new JTextArea(9,20);  
        boxV4.add(text3);  
          
        baseBox = Box.createVerticalBox();  
        baseBox.add(boxV1);  
        baseBox.add(Box.createVerticalStrut(10));  
        baseBox.add(boxv2);  
        baseBox.add(Box.createVerticalStrut(10));  
        baseBox.add(boxV3);  
        add(baseBox);  
        baseBox.add(Box.createVerticalStrut(10));  
        baseBox.add(boxV4);  
          
          
        btn1.addActionListener(new ActionListener() {  
            public void actionPerformed(ActionEvent e) {  
                // TODO Auto-generated method stub  
                String name = text1.getText();  
                double grade = Double.parseDouble(text2.getText());  
                //System.out.println(name+"  "+grade);  
                student s = new student(name, grade);  
                //System.out.println(s.name+"  "+s.grade);  
                if (list.isEmpty()==true) {  
                    list.add(0,s);  
                }  
                else  
                {  
                    int i=0;  
                    while(i it = list.iterator();  
                while(it.hasNext())  
                { student student =(student) it.next();  
                  text3.append("姓名:"+student.name+" "+"成绩:"+student.grade+"\n");  
                    //System.out.println(student.name+" "+student.grade+"\n");  
                      
                }  
            }  
        });  
    }  
      
}  
import java.util.ArrayList;  
@SuppressWarnings("unused")
public class test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub 
		        WindowBoxLayout w = new WindowBoxLayout();  
		        w.setBounds(100,100,310,350);  
		        w.setTitle("成绩排序");  	          
		    }  
}
我的实验截图:
14-2_第2张图片

你可能感兴趣的:(14-2)