java 学生成绩排序

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

程序运行效果如图:

 java 学生成绩排序_第1张图片

import java.awt.BorderLayout;  
import java.awt.event.ActionEvent;  
import java.awt.event.ActionListener;  
import java.util.ArrayList;  
import java.util.Collections;  
import java.util.Comparator;  
import java.util.HashMap;  
import java.util.Iterator;  
import java.util.LinkedHashMap;  
import java.util.List;  
import java.util.Map;  
import java.util.Map.Entry;  
  
import javax.swing.JButton;  
import javax.swing.JFrame;  
import javax.swing.JLabel;  
import javax.swing.JPanel;  
import javax.swing.JTextArea;  
import javax.swing.JTextField;  
  

public class Student extends JFrame {  
    JLabel lName,lScore;      
    JTextField tName,tScore;    
    JTextArea taShow;    
    JButton bSubmit;  
    JPanel pan;  
    Map studentMap,resultMap;  
    
    public static void main(String[] args) {           
        new Student();  
    }  
    
    public Student() {                
        init();  
        click();  
    }  
  
    public void init() {           
        lName=new JLabel("姓名");    
        lScore=new JLabel("成绩");  
        tName=new JTextField(10);  
        tScore=new JTextField(10);  
        bSubmit=new JButton("确定");  
        pan=new JPanel();  
        taShow=new JTextArea();  
        pan.add(lName);            
        pan.add(tName);  
        pan.add(lScore);  
        pan.add(tScore);  
        pan.add(bSubmit);  
        add(pan,BorderLayout.NORTH);     
        add(taShow, BorderLayout.CENTER);  
        setTitle("学生成绩排序");          
        setSize(400, 300);  
        setVisible(true);  
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);  
        validate();  
        studentMap=new HashMap();              
    }        
    private void click() {  
        bSubmit.addActionListener(new ActionListener() {          
            public void actionPerformed(ActionEvent e) {  
                save();  
                showMap();  
            }         
        });  
    }  
    private void save() {  
        studentMap.put(tName.getText(),tScore.getText());  
        resultMap = sortMapByValue(studentMap);
        tName.setText("");          
        tScore.setText("");  
    }  

    public static Map sortMapByValue(Map map) {  
        if (map == null || map.isEmpty()) {  
            return null;  
        }  
        Map sortedMap = new LinkedHashMap();  
        List> entryList = new ArrayList>(map.entrySet());   
        Collections.sort(entryList, new MapValueComparator());  
        Iterator> iter = entryList.iterator();  
        Map.Entry tmpEntry = null;  
        while (iter.hasNext()) {  
            tmpEntry = iter.next();  
            sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue());   
        }  
        return sortedMap;  
    }  
   
    private void showMap() {  
        taShow.setText("");  
        for(Map.Entry entry:resultMap.entrySet()) {      
            taShow.append("姓名:"+entry.getKey()+"     成绩:"+entry.getValue()+"\n");  
        }         
    }     
}  
class MapValueComparator implements Comparator> {  
    public int compare(Entry s1, Entry s2) {  
        return s1.getValue().compareTo(s2.getValue());  
    }  
} 

运行结果:

java 学生成绩排序_第2张图片

 

转载于:https://www.cnblogs.com/songqinzhe/p/8037221.html

你可能感兴趣的:(java)