编写一个应用程序,用户分别从两个文本框输入学术的姓名和分数。

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

public class student implements Comparable<student>{    
            
        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<o.grade)    
            {    
                return 0;    
            }    
            return 1;    
        }    
  
} 
import java.awt.Button;  
import java.awt.Label;  
import java.awt.event.ActionEvent;  
import java.awt.event.ActionListener;  
import java.util.ArrayList;    
import java.util.Iterator;    
  
import javax.swing.Box;  
import javax.swing.JFrame;  
import javax.swing.JTextArea;  
import javax.swing.JTextField;  
public class WindowBoxLayout extends JFrame{  
    Box baseBox,boxV1,boxv2,boxV3,boxV4;    
    JTextField text1,text2;    
    Button btn2;    
    Button btn1;    
    ArrayList<student> list = new ArrayList<student>();    
    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();    
        final 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<list.size())    
                    {    
                        student student = list.get(i);    
                        System.out.println(student.name+"  "+student.grade);    
                        if(student.compareTo(s)==0)    
                        {    
                                
                            break;    
                        }    
                        i++;    
                        System.out.println(i);    
                    }    
                    list.add(i,s);    
                        
                }    
            }    
        });           
        btn2.addActionListener(new ActionListener() {    
            public void actionPerformed(ActionEvent e) {    
                // TODO Auto-generated method stub    
                Iterator<student> 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("成绩排序");                 
            }    
}  





以下是一个简单的Python Tkinter GUI程序实现上述功能: ```python import tkinter as tk class Student: def __init__(self, name, score): self.name = name self.score = score def sort_students(students): return sorted(students, key=lambda student: student.score) def add_student(): name = name_entry.get() score = int(score_entry.get()) student = Student(name, score) students.append(student) students_sorted = sort_students(students) display_students(students_sorted) def display_students(students): text.delete('1.0', tk.END) for student in students: text.insert(tk.END, f'{student.name}: {student.score}\n') students = [] window = tk.Tk() name_label = tk.Label(window, text='姓名') name_label.grid(row=0, column=0) name_entry = tk.Entry(window) name_entry.grid(row=0, column=1) score_label = tk.Label(window, text='分数') score_label.grid(row=1, column=0) score_entry = tk.Entry(window) score_entry.grid(row=1, column=1) add_button = tk.Button(window, text='添加学生', command=add_student) add_button.grid(row=2, column=0, columnspan=2) text = tk.Text(window) text.grid(row=3, column=0, columnspan=2) window.mainloop() ``` 该程序使用了Python的Tkinter库来创建GUI界面,包含两个文本框(用于输入学生姓名分数)、一个添加学生按钮一个文本区(用于显示学生信息)。在点击添加学生按钮时,程序会从文本框中获取用户输入学生姓名分数,并将其封装为一个Student对象添加到students列表中,然后根据分数排序并在文本区中显示所有学生信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

百无聊赖Zy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值