Write an application in which the user enters the student's name and score from two text boxes, and the program displays the students ' names and scores in a text area, sorted by their score.
Import Java.awt.FlowLayout;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import java.util.ArrayList;
Import java.util.Collections;
Import Java.util.Iterator;
Import java.util.List;
Import Javax.swing.Box;
Import Javax.swing.JButton;
Import Javax.swing.JFrame;
Import Javax.swing.JLabel;
Import Javax.swing.JTextArea;
Import Javax.swing.JTextField;
public class Myjframe extends JFrame implements ActionListener {
JFrame Mjframe;
JTextField MJTextField1, MJTextField2;
JTextArea Mjtextarea;
Myjframe () {
Mjframe = new JFrame ("score sort");
Mjframe.setbounds (200, 100, 900, 600);//Setting the window position and window width height
Mjframe.setlayout (New FlowLayout ());//Set the layout of the window
MJTextField1 = new JTextField (10);//explicitly create two text boxes
MJTextField2 = new JTextField (10);
JButton Mjbutton = new JButton ("OK");//Create button
Box MBox = Box.createhorizontalbox ();//Create a horizontal box container
Mbox.add (New JLabel ("name"));//Add an implicitly created label because you do not need to click an event so you can create it implicitly
Mbox.add (mJTextField1);//All add is added to the parent container
Mbox.add (Box.createhorizontalstrut (50));//Set horizontal intervals
Mbox.add (New JLabel ("score"));
Mbox.add (MJTEXTFIELD2);
Mbox.add (Box.createhorizontalstrut (50));
Mbox.add (Mjbutton);
Mjframe.add (MBox);
Mjtextarea = new JTextArea (40,80);//Create a text area to display the output, so you must explicitly create
Mjtextarea.settext ("Write your data on top, click" \ n ");
Mjframe.add (Mjtextarea);
Mjframe.setvisible (TRUE);//Make the window explicit
Mjbutton.addactionlistener (this);
}
list<student> list = new arraylist<student> ();
@Override
public void actionperformed (ActionEvent e) {
TODO auto-generated Method Stub
/*
* Because only button has a click event, so you can directly without judgment, if there are multiple controls bound to the event will need to determine
* This directly determines whether the text box is empty
*/
Mjtextarea.settext ("");
String name = Mjtextfield1.gettext (). Trim ();
String grade = Mjtextfield2.gettext (). Trim ();
if (! "". Equals (name) &&! "". Equals (grade)) {
try {
List.add (New Student (name, float.parsefloat (grade)));
Mjtextarea.append ("Currently inserted information: Name:" +name+ "score:" +grade+ "\ n");
} catch (NumberFormatException E1) {
TODO auto-generated Catch block
E1.printstacktrace ();
Mjtextarea.append ("Do not know the result is the number format, re-enter!") \ n ");
}
}
Collections.sort (list);//Sort list
iterator<student> Iterator = List.iterator ();
Student Student;
while (Iterator.hasnext ()) {
Student = Iterator.next ();
Mjtextarea.append ("Name:" +student.name+ "Grade:" +student.grade+ "\ n");
}
}
}
Class Student implements comparable<student>{
String name;
float grade;
Public Student (String name, float grade) {
Super ();
THIS.name = name;
This.grade = grade;
}
@Override
public int compareTo (Student o) {
TODO auto-generated Method Stub
return (int) (o.grade*10-this.grade*10);//guarantee the accuracy of a decimal case comparison
}
}
public class Test1 {
public static void Main (string[] args) {
TODO auto-generated method stubs
Myjframe myjframe = new Myjframe ();//Display window
}
}
Java Collection Framework exercises: The user enters the student's name and score from two text boxes, respectively ....