求一个Java小程序
能在JBUILDER中运行的,小游戏也行。年终JAVA课程要求交的。。请各位编程高手帮帮忙啊!!...
能在JBUILDER中运行的,小游戏也行。年终JAVA课程要求交的。。请各位编程高手帮帮忙啊!!
展开
4个回答
展开全部
import java.awt.*;
import java.io.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
public class SMS {
public static void main(String[] args) throws Exception{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
JFrame f = new JFrame("学生信息管理");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(688,380);
f.setResizable(false);
f.setLocationRelativeTo(null);
f.setContentPane(new SMSPanel());
f.setVisible(true);
}
}
//主面板
class SMSPanel extends JPanel implements ActionListener{
private static final long serialVersionUID = 6923196760525083410L;
private String filePath="c:/students.info.txt";//文件路径
private StudentModel sm = new StudentModel(filePath);
private JTextField name,id,classname,addr;
private JComboBox sex,department;
private JButton add,query,showAllInfo;
private JScrollPane jsp;
private JRadioButton r1,r2;
private ButtonGroup group;
private JTextField querystr;
private SItemPanel spanel;
SMSPanel(){
super(null);
//第一排组件
this.addLabel("姓名:",10,10,32,20);
this.name=tf(38,10,48,20);
this.addLabel("性别:",96,10,32,20);
this.sex=new JComboBox(new String[]{"男","女"});
this.sex.setBounds(124,10,38,20);
this.add(sex);
this.addLabel("ID:",172,10,20,20);
this.id=tf(190,10,80,20);
this.addLabel("系:",280,10,20,20);
this.department=new JComboBox(
new String[]{"日语","英语","法语","德语","鸟语","花香","呵呵"});
this.department.setBounds(300,10,50,20);
this.add(department);
this.addLabel("班:",360,10,22,20);
this.classname=tf(382,10,38,20);
this.addLabel("籍贯:",430,10,32,20);
this.addr=tf(462,10,100,20);
this.add=new JButton("加入");
this.add.setBounds(575,10,58,20);
this.add(add);
//第一排组件结束
//第二排组件:
this.addLabel("查询:",10,48,32,20);
this.querystr=tf(50,48,100,20);
this.r1=new JRadioButton("根据姓名",true);
this.r1.setBounds(155,48,78,20);
this.add(r1);
this.r2=new JRadioButton("根据ID");
this.r2.setBounds(234,48,65,20);
this.add(r2);
this.query=new JButton("执行查询");
this.query.setSize(88,20);
this.query.setLocation(310,48);
this.add(query);
this.showAllInfo=new JButton("显示全部");
this.showAllInfo.setBounds(428,48,88,20);
this.add(showAllInfo);
this.spanel = new SItemPanel(sm);
this.jsp=new JScrollPane(spanel);
this.jsp.setLocation(10,90);
this.jsp.setSize(658,232);
this.add(jsp);
group = new ButtonGroup();
group.add(r1);
group.add(r2);
this.add.addActionListener(this);
this.query.addActionListener(this);
this.showAllInfo.addActionListener(this);
}
//新建LABEL对象并加入到面板
private void addLabel(String s,int x,int y,int w,int h){
JLabel l = new JLabel(s);
l.setBounds(x,y,w,h);
add(l);
}
private JTextField tf(int x,int y,int w,int h){
JTextField t = new JTextField();
t.setBounds(x,y,w,h);
this.add(t);
return t;
}
public void actionPerformed(ActionEvent e) {
//加入新学生
if(e.getSource().equals(add)){
if(check()){
JOptionPane.showMessageDialog(this,"字段未填写完整","错误",JOptionPane.WARNING_MESSAGE);
return;
}
else{
boolean st = createNew();
if(st){
reset();
spanel.showAllStudents();
}
else
JOptionPane.showMessageDialog(this,"已有同ID的学生存在","错误",JOptionPane.WARNING_MESSAGE);
}
}
//显示查找到的学生信息
else if(e.getSource().equals(query)){
String qs = querystr.getText().trim();
boolean isname = r1.isSelected();
if(isname){
ArrayList sss = sm.findByName(qs);
if(sss.isEmpty()){
JOptionPane.showMessageDialog(this,"系统找不到名为 '"+qs+"' 的学生信息");
}
else
spanel.showAllStudents(sss);
}
else{
Student s = sm.findByID(qs);
if(s==null){
JOptionPane.showMessageDialog(this,"系统找不到ID为 '"+qs+"' 的学生信息");
}
else
spanel.showOneStudent(s);
}
}
//显示所有信息
else if(e.getSource().equals(showAllInfo)){
if(sm.isEmpty()){
JOptionPane.showMessageDialog(this,"目前文件中没有任何学生信息.");
}
else
spanel.showAllStudents();
}
jsp.validate();
}
//复位字段内容
private void reset() {
name.setText("");
id.setText("");
classname.setText("");
addr.setText("");
sex.setSelectedIndex(0);
department.setSelectedIndex(0);
}
//新建学生对象并保存入库
private boolean createNew() {
Student s=new Student();
s.setName(name.getText().trim());
s.setSex(sex.getSelectedIndex()==0?"男":"女");
s.setID(id.getText().trim());
s.setDepartment(department.getSelectedItem().toString());
s.setClassName(classname.getText().trim());
s.setAddr(addr.getText().trim());
if(!sm.isExists(s)){
sm.addStudent(s);
return true;
}
return false;
}
//检查字段完整性
private boolean check() {
return
name.getText().trim().isEmpty()||
id.getText().trim().isEmpty()||
classname.getText().trim().isEmpty()||
addr.getText().trim().isEmpty();
}
}
/**学生呈现面板*/
class SItemPanel extends JPanel{
private static final long serialVersionUID = 8397146822995036515L;
private StudentModel model;
public SItemPanel(StudentModel op) {
super(null);
this.setBackground(Color.white);
this.model=op;
}
//根据数组中的学生呈现学生信息
void showAllStudents(ArrayList sss) {
StudentItem.odd=false;
this.removeAll();
this.repaint();
for(int i=0; i<sss.size(); i++){
StudentItem si = new StudentItem((Student)sss.get(i));
si.setLocation(1,i*si.getHeight()+i);
si.setSize(getWidth(),si.getHeight());
this.add(si);
si.repaint();
}
this.setPreferredSize(new Dimension(1,sss.size()*21));
}
void showAllStudents() {
showAllStudents(model.getAllStudents());
}
void showOneStudent(Student s){
ArrayList a = new ArrayList();
a.add(s);
showAllStudents(a);
}
}
//学生数据处理类
class StudentModel{
private ArrayList ss;
private File src;
StudentModel(String file){
this.src = new File(file);
this.ss=new ArrayList();
this.loadStudentInfo();
}
public boolean isEmpty() {
return ss.isEmpty();
}
//加入一个学生
public void addStudent(Student s){
this.ss.add(0,s);
this.save();
}
//返回全部学生的集合
public ArrayList getAllStudents(){return ss;}
//学生是否已存在
public boolean isExists(Student s){
return ss.indexOf(s)>-1;
}
//根据姓名找学生,姓名可以有重复
public ArrayList findByName(String name){
ArrayList s = new ArrayList();
for(int i=0; i<ss.size(); i++)
if(((Student)ss.get(i)).getName().equals(name))
s.add(ss.get(i));
return s;
}
//根据ID找学生,ID不会有重复
public Student findByID(String id){
for(int i=0; i<ss.size(); i++)
if(((Student)ss.get(i)).getID().equals(id))
return (Student)ss.get(i);
return null;
}
//保存学生信息
private void save(){
try{
BufferedWriter bw = new BufferedWriter(new FileWriter(src));
for(int i=0; i<ss.size(); i++){
bw.write(ss.get(i).toString());
bw.newLine();
}
bw.flush();
bw.close();
}catch(Exception e){}
}
//读取学生信息
private void loadStudentInfo() {
try{
BufferedReader br = new BufferedReader(new FileReader(src));
String tmp = null;
while((tmp=br.readLine())!=null){
try{
String[] sp = tmp.split(" +");
if(sp.length==6){
this.ss.add(new Student(sp));
}
}catch(Exception e){}
}
br.close();
}catch(Exception e){}
}
}
/**学生呈现类*/
class StudentItem extends JPanel{
private static final long serialVersionUID = -7110719002473825550L;
static boolean odd;
private static Color c1=new Color(0xFFFFEF);
private static Color c2=new Color(0xFFEFFF);
private static final int width=500,height=20;
private JLabel info;
StudentItem(Student s){
super(null);
this.setSize(width,height);
this.setBackground(odd?c1:c2);
this.info=new JLabel(
"<html><table border=0><tr>" +
"<td align=center width=100>"+s.getName()+"</td>" +
"<td align=center width=50>"+s.getSex()+"</td>" +
"<td align=center width=158>"+s.getID()+"</td>" +
"<td align=center width=90>"+s.getDepartment()+"系</td>" +
"<td align=center width=90>"+s.getClassName()+"班</td>" +
"<td align=center width=200>"+s.getAddr()+"人</td>" +
"</tr></table>"
);
this.info.setSize(getSize());
this.add(info);
odd=!odd;
}
}
/**学生类*/
class Student{
private String name;//姓名
private String sex;//性别
private String id;//ID
private String department;//系
private String _class;//班
private String addr;//籍贯
Student(){};
Student(String[] inf){
this(inf[0],inf[1],inf[2],inf[3],inf[4],inf[5]);
}
Student(String n,String s,String id,String dep,String c,String a){
this.setName(n);this.setSex(s);this.setID(id);this.setDepartment(dep);this.setClassName(c);this.setAddr(a);
}
void setName(String s){this.name=s;}
void setSex(String s){this.sex=s;}
void setID(String s){this.id=s;}
void setDepartment(String d){this.department = d;}
void setClassName(String s){this._class=s;}
void setAddr(String addr){this.addr=addr;}
String getName(){return name;}
String getSex(){return sex;}
String getID(){return id;}
String getDepartment(){return this.department;}
String getClassName(){return this._class;}
String getAddr(){return addr;}
public boolean isFullInfo(){
return name!=null&&sex!=null&&id!=null&&department!=null&&_class!=null&&addr!=null;
}
public String toString(){
return name+" "+sex+" "+id+" "+department+" "+_class+" "+addr+"";
}
public boolean equals(Object o){
return o!=null && o instanceof Student && ((Student)o).id.equals(id);
}
}
import java.io.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
public class SMS {
public static void main(String[] args) throws Exception{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
JFrame f = new JFrame("学生信息管理");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(688,380);
f.setResizable(false);
f.setLocationRelativeTo(null);
f.setContentPane(new SMSPanel());
f.setVisible(true);
}
}
//主面板
class SMSPanel extends JPanel implements ActionListener{
private static final long serialVersionUID = 6923196760525083410L;
private String filePath="c:/students.info.txt";//文件路径
private StudentModel sm = new StudentModel(filePath);
private JTextField name,id,classname,addr;
private JComboBox sex,department;
private JButton add,query,showAllInfo;
private JScrollPane jsp;
private JRadioButton r1,r2;
private ButtonGroup group;
private JTextField querystr;
private SItemPanel spanel;
SMSPanel(){
super(null);
//第一排组件
this.addLabel("姓名:",10,10,32,20);
this.name=tf(38,10,48,20);
this.addLabel("性别:",96,10,32,20);
this.sex=new JComboBox(new String[]{"男","女"});
this.sex.setBounds(124,10,38,20);
this.add(sex);
this.addLabel("ID:",172,10,20,20);
this.id=tf(190,10,80,20);
this.addLabel("系:",280,10,20,20);
this.department=new JComboBox(
new String[]{"日语","英语","法语","德语","鸟语","花香","呵呵"});
this.department.setBounds(300,10,50,20);
this.add(department);
this.addLabel("班:",360,10,22,20);
this.classname=tf(382,10,38,20);
this.addLabel("籍贯:",430,10,32,20);
this.addr=tf(462,10,100,20);
this.add=new JButton("加入");
this.add.setBounds(575,10,58,20);
this.add(add);
//第一排组件结束
//第二排组件:
this.addLabel("查询:",10,48,32,20);
this.querystr=tf(50,48,100,20);
this.r1=new JRadioButton("根据姓名",true);
this.r1.setBounds(155,48,78,20);
this.add(r1);
this.r2=new JRadioButton("根据ID");
this.r2.setBounds(234,48,65,20);
this.add(r2);
this.query=new JButton("执行查询");
this.query.setSize(88,20);
this.query.setLocation(310,48);
this.add(query);
this.showAllInfo=new JButton("显示全部");
this.showAllInfo.setBounds(428,48,88,20);
this.add(showAllInfo);
this.spanel = new SItemPanel(sm);
this.jsp=new JScrollPane(spanel);
this.jsp.setLocation(10,90);
this.jsp.setSize(658,232);
this.add(jsp);
group = new ButtonGroup();
group.add(r1);
group.add(r2);
this.add.addActionListener(this);
this.query.addActionListener(this);
this.showAllInfo.addActionListener(this);
}
//新建LABEL对象并加入到面板
private void addLabel(String s,int x,int y,int w,int h){
JLabel l = new JLabel(s);
l.setBounds(x,y,w,h);
add(l);
}
private JTextField tf(int x,int y,int w,int h){
JTextField t = new JTextField();
t.setBounds(x,y,w,h);
this.add(t);
return t;
}
public void actionPerformed(ActionEvent e) {
//加入新学生
if(e.getSource().equals(add)){
if(check()){
JOptionPane.showMessageDialog(this,"字段未填写完整","错误",JOptionPane.WARNING_MESSAGE);
return;
}
else{
boolean st = createNew();
if(st){
reset();
spanel.showAllStudents();
}
else
JOptionPane.showMessageDialog(this,"已有同ID的学生存在","错误",JOptionPane.WARNING_MESSAGE);
}
}
//显示查找到的学生信息
else if(e.getSource().equals(query)){
String qs = querystr.getText().trim();
boolean isname = r1.isSelected();
if(isname){
ArrayList sss = sm.findByName(qs);
if(sss.isEmpty()){
JOptionPane.showMessageDialog(this,"系统找不到名为 '"+qs+"' 的学生信息");
}
else
spanel.showAllStudents(sss);
}
else{
Student s = sm.findByID(qs);
if(s==null){
JOptionPane.showMessageDialog(this,"系统找不到ID为 '"+qs+"' 的学生信息");
}
else
spanel.showOneStudent(s);
}
}
//显示所有信息
else if(e.getSource().equals(showAllInfo)){
if(sm.isEmpty()){
JOptionPane.showMessageDialog(this,"目前文件中没有任何学生信息.");
}
else
spanel.showAllStudents();
}
jsp.validate();
}
//复位字段内容
private void reset() {
name.setText("");
id.setText("");
classname.setText("");
addr.setText("");
sex.setSelectedIndex(0);
department.setSelectedIndex(0);
}
//新建学生对象并保存入库
private boolean createNew() {
Student s=new Student();
s.setName(name.getText().trim());
s.setSex(sex.getSelectedIndex()==0?"男":"女");
s.setID(id.getText().trim());
s.setDepartment(department.getSelectedItem().toString());
s.setClassName(classname.getText().trim());
s.setAddr(addr.getText().trim());
if(!sm.isExists(s)){
sm.addStudent(s);
return true;
}
return false;
}
//检查字段完整性
private boolean check() {
return
name.getText().trim().isEmpty()||
id.getText().trim().isEmpty()||
classname.getText().trim().isEmpty()||
addr.getText().trim().isEmpty();
}
}
/**学生呈现面板*/
class SItemPanel extends JPanel{
private static final long serialVersionUID = 8397146822995036515L;
private StudentModel model;
public SItemPanel(StudentModel op) {
super(null);
this.setBackground(Color.white);
this.model=op;
}
//根据数组中的学生呈现学生信息
void showAllStudents(ArrayList sss) {
StudentItem.odd=false;
this.removeAll();
this.repaint();
for(int i=0; i<sss.size(); i++){
StudentItem si = new StudentItem((Student)sss.get(i));
si.setLocation(1,i*si.getHeight()+i);
si.setSize(getWidth(),si.getHeight());
this.add(si);
si.repaint();
}
this.setPreferredSize(new Dimension(1,sss.size()*21));
}
void showAllStudents() {
showAllStudents(model.getAllStudents());
}
void showOneStudent(Student s){
ArrayList a = new ArrayList();
a.add(s);
showAllStudents(a);
}
}
//学生数据处理类
class StudentModel{
private ArrayList ss;
private File src;
StudentModel(String file){
this.src = new File(file);
this.ss=new ArrayList();
this.loadStudentInfo();
}
public boolean isEmpty() {
return ss.isEmpty();
}
//加入一个学生
public void addStudent(Student s){
this.ss.add(0,s);
this.save();
}
//返回全部学生的集合
public ArrayList getAllStudents(){return ss;}
//学生是否已存在
public boolean isExists(Student s){
return ss.indexOf(s)>-1;
}
//根据姓名找学生,姓名可以有重复
public ArrayList findByName(String name){
ArrayList s = new ArrayList();
for(int i=0; i<ss.size(); i++)
if(((Student)ss.get(i)).getName().equals(name))
s.add(ss.get(i));
return s;
}
//根据ID找学生,ID不会有重复
public Student findByID(String id){
for(int i=0; i<ss.size(); i++)
if(((Student)ss.get(i)).getID().equals(id))
return (Student)ss.get(i);
return null;
}
//保存学生信息
private void save(){
try{
BufferedWriter bw = new BufferedWriter(new FileWriter(src));
for(int i=0; i<ss.size(); i++){
bw.write(ss.get(i).toString());
bw.newLine();
}
bw.flush();
bw.close();
}catch(Exception e){}
}
//读取学生信息
private void loadStudentInfo() {
try{
BufferedReader br = new BufferedReader(new FileReader(src));
String tmp = null;
while((tmp=br.readLine())!=null){
try{
String[] sp = tmp.split(" +");
if(sp.length==6){
this.ss.add(new Student(sp));
}
}catch(Exception e){}
}
br.close();
}catch(Exception e){}
}
}
/**学生呈现类*/
class StudentItem extends JPanel{
private static final long serialVersionUID = -7110719002473825550L;
static boolean odd;
private static Color c1=new Color(0xFFFFEF);
private static Color c2=new Color(0xFFEFFF);
private static final int width=500,height=20;
private JLabel info;
StudentItem(Student s){
super(null);
this.setSize(width,height);
this.setBackground(odd?c1:c2);
this.info=new JLabel(
"<html><table border=0><tr>" +
"<td align=center width=100>"+s.getName()+"</td>" +
"<td align=center width=50>"+s.getSex()+"</td>" +
"<td align=center width=158>"+s.getID()+"</td>" +
"<td align=center width=90>"+s.getDepartment()+"系</td>" +
"<td align=center width=90>"+s.getClassName()+"班</td>" +
"<td align=center width=200>"+s.getAddr()+"人</td>" +
"</tr></table>"
);
this.info.setSize(getSize());
this.add(info);
odd=!odd;
}
}
/**学生类*/
class Student{
private String name;//姓名
private String sex;//性别
private String id;//ID
private String department;//系
private String _class;//班
private String addr;//籍贯
Student(){};
Student(String[] inf){
this(inf[0],inf[1],inf[2],inf[3],inf[4],inf[5]);
}
Student(String n,String s,String id,String dep,String c,String a){
this.setName(n);this.setSex(s);this.setID(id);this.setDepartment(dep);this.setClassName(c);this.setAddr(a);
}
void setName(String s){this.name=s;}
void setSex(String s){this.sex=s;}
void setID(String s){this.id=s;}
void setDepartment(String d){this.department = d;}
void setClassName(String s){this._class=s;}
void setAddr(String addr){this.addr=addr;}
String getName(){return name;}
String getSex(){return sex;}
String getID(){return id;}
String getDepartment(){return this.department;}
String getClassName(){return this._class;}
String getAddr(){return addr;}
public boolean isFullInfo(){
return name!=null&&sex!=null&&id!=null&&department!=null&&_class!=null&&addr!=null;
}
public String toString(){
return name+" "+sex+" "+id+" "+department+" "+_class+" "+addr+"";
}
public boolean equals(Object o){
return o!=null && o instanceof Student && ((Student)o).id.equals(id);
}
}
参考资料: 原创
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
把你的邮箱给我,我有很多小游戏。都是我自己写的。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
网上一搜一大片 你不会那么懒吧?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询