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

package test;

import java.awt.Color;
import java.awt.GridLayout;
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.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class Test2 extends JFrame {
    JButton btn;
    HashMap<String, Integer> map = new HashMap<>();

    public Test2() {
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        JPanel contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        this.setContentPane(contentPane);
        contentPane.setLayout(new GridLayout(5, 1, 5, 5));
        JPanel pane1 = new JPanel();
        // pane1.setBackground(Color.black);
        contentPane.add(pane1);
        JPanel pane2 = new JPanel();
        contentPane.add(pane2);
        JPanel pane3 = new JPanel();
        contentPane.add(pane3);
        JLabel label1 = new JLabel("姓名:");

        JTextField textField1 = new JTextField();
        textField1.setColumns(10);
        pane1.add(label1);
        pane1.add(textField1);
        JLabel label2 = new JLabel("分数:");
        JTextField textField2 = new JTextField();
        textField2.setColumns(10);
        pane2.add(label2);
        pane2.add(textField2);
        btn = new JButton("递交");
        pane3.add(btn);
        JScrollPane j = new JScrollPane();

        JTextArea t = new JTextArea(10, 10);
        j.setViewportView(t);
        contentPane.add(j);
        setSize(300, 500);
        setVisible(true);

        btn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                String name = textField1.getText();
                String g = textField2.getText();
                // System.out.println(name+Integer.valueOf(g));
                map.put(name, Integer.valueOf(g));
                sort(map, t);
//              System.out.println(map.size());
                // for(String s:map.keySet()){
                // t.setText(s+":"+map.get(s));
                // }

            }
        });

    }

    public void sort(HashMap<String, Integer> map, JTextArea t) {
        // TreeMap<String, Integer> treeMap =new TreeMap<>();
        // Set<String> key = map.keySet();

        List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
        // 然后通过比较器来实现排序
        Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {

            @Override
            public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
                // TODO Auto-generated method stub
                return o2.getValue().compareTo(o1.getValue());
            }
            // 升序排序

        });
        String txt = "";

        for (Map.Entry<String, Integer> mapping : list) {
            String s = mapping.getKey() + ":" + mapping.getValue() + "\n";
            txt += s;
        }

        t.setText(txt);
        // map.entrySet().stream().sorted(Map.Entry.comparingByKey())

    }

    public static void main(String[] args) {
        new Test2();

    }

}

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值