黑马程序员技术交流社区
标题:
一个GUI的小实验,欢迎大家交流
[打印本页]
作者:
马也keyboard
时间:
2015-6-23 22:53
标题:
一个GUI的小实验,欢迎大家交流
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
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 Test {
private static JFrame f_all;
private JFrame f_insert;
private JFrame f_check;
//all组件
private static JButton btn_insert;
private static JButton btn_check;
private static JButton btn_sort;
private static JTextArea txtArea;
//insert组件
private static JButton btn_insert_ensure;
private static JTextField txt_id;
private static JTextField txt_num;
//check组件
private static JButton btn_check_ensure;
private static JTextField txt_id_in;
private static JLabel lab_num;
private static ArrayList<Student> array;
public void myEvent()
{
f_all.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
btn_insert.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
init_insert();
}
});
btn_insert_ensure.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
String str_id = txt_id.getText().toString();
String str_num = txt_num.getText().toString();
if(str_id != "")
{
if(str_num != "")
{
int num = Integer.parseInt(str_num);
array.add(new Student(str_id,num));
txt_id.setText("");
txt_num.setText("");
}
}
}
});
btn_check.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
init_check();
}
});
btn_check_ensure.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
String str_id = txt_id_in.getText().toString();
Iterator<Student> it = array.iterator();
//判断数据是否有效
boolean flag = false;
while(it.hasNext())
{
Student stu = it.next();
String id = stu.getId();
int num = stu.getNum();
if(id.equals(str_id))
{
lab_num.setText(num+"");
flag = true;
}
}
if(!flag)
{
lab_num.setText("不存在查询学号");
}
}
});
btn_sort.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
Collections.sort(array, new myCompare());
Iterator<Student> it = array.iterator();
StringBuffer str = new StringBuffer();
while(it.hasNext())
{
Student stu = it.next();
String id = stu.getId();
int num = stu.getNum();
str.append(id+"::"+num+System.getProperty("line.separator"));
}
String string = str.toString();
txtArea.setText(string);
}
});
}
//整体布局
public void init_all()
{
btn_insert = new JButton("输入");
btn_check = new JButton("查询");
btn_sort = new JButton("排序");
txtArea = new JTextArea(6,20);
//布局
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
//组件加入布局
jp1.add(btn_insert);
jp1.add(btn_check);
jp1.add(btn_sort);
jp2.add(txtArea);
f_all = new JFrame();
f_all.setBounds(300, 200, 300, 200);
f_all.setLayout(new BorderLayout());
f_all.add(jp1,BorderLayout.NORTH);
f_all.add(jp2,BorderLayout.CENTER);
jp1.setVisible(true);
jp2.setVisible(true);
f_all.setResizable(false);
f_all.setVisible(true);
}
//输入布局
public void init_insert()
{
txt_id = new JTextField(5);
txt_num = new JTextField(5);
JLabel jl1 = new JLabel("学号:");
JLabel jl2 = new JLabel("成绩:");
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
f_insert = new JFrame();
p1.add(jl1);
p1.add(txt_id);
p2.add(jl2);
p2.add(txt_num);
p3.add(btn_insert_ensure);
f_insert.setBounds(300, 200, 200, 150);
f_insert.add(p1,BorderLayout.NORTH);
f_insert.add(p2,BorderLayout.CENTER);
f_insert.add(p3,BorderLayout.SOUTH);
f_insert.setResizable(true);
f_insert.setVisible(true);
}
//查询布局
public void init_check()
{
txt_id_in = new JTextField(5);
lab_num = new JLabel();
JLabel jl1 = new JLabel("学号:");
JPanel p1 = new JPanel();
p1.add(jl1);
p1.add(txt_id_in);
f_check = new JFrame();
f_check.setBounds(300, 200, 200, 150);
f_check.add(p1,BorderLayout.NORTH);
f_check.add(btn_check_ensure,BorderLayout.CENTER);
f_check.add(lab_num,BorderLayout.SOUTH);
f_check.setResizable(false);
f_check.setVisible(true);
}
public static void main(String[] args)
{
Test t = new Test();
t.btn_insert_ensure = new JButton("输入");
t.btn_check_ensure = new JButton("查询");
array = new ArrayList<Student>();
t.init_all();
t.myEvent();
System.out.println(array);
}
}
class Student
{
private String id;
private int num;
public Student(String id, int num) {
super();
this.id = id;
this.num = num;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
class myCompare implements Comparator<Student>
{
public int compare(Student stu1, Student stu2) {
int num1 = stu1.getNum();
int num2 = stu2.getNum();
if(num1 == num2)
{
return stu1.getId().compareTo(stu2.getId());
}
return num1-num2;
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2