大家开始看的java教材都哪些?
我看的是<<java2实用教程 >> 清华大学出版社的 看的差不多 但是个人感觉不好 只有怎么做 不知道为什么这样子做 还有好多细节 没有[ 本帖最后由 诸葛修勤 于 2011-4-27 09:33 编辑 ]
public class main { public static void main(String args[]) { new yxf(); } } class StudentMsg { String m_id; //学号 String m_name; //姓名 double m_mark; //分数 StudentMsg(String id, String name, double mark) { m_id = id; m_name = name; m_mark = mark; } } class yxf extends Frame implements ActionListener { LinkedList<StudentMsg> list = new LinkedList<StudentMsg>();//定义链表句柄 PanelAdd panelAdd;//添加数据元素面板句柄 PanelShow panelShow;//显示信息面板句柄 PanelDelete panelDelete;//删除信息面板句柄 Panel panelCenter; Panel panelBase; CardLayout card = new CardLayout(); Button buttonAdd; Button buttonShow; Button buttonDelete; StudentMsg stumsg; yxf() { super("yxf"); setBounds(100, 100, 200, 250); panelCenter = new Panel(); panelCenter.setLayout(card); panelAdd = new PanelAdd(); panelDelete = new PanelDelete(); panelShow = new PanelShow(); panelCenter.add("添加", panelAdd); panelCenter.add("显示", panelShow); panelCenter.add("删除", panelDelete); add(panelCenter, BorderLayout.CENTER); panelBase = new Panel(); buttonAdd = new Button("添加"); buttonAdd.addActionListener(this); buttonShow = new Button("显示"); buttonShow.addActionListener(this); buttonDelete = new Button("删除"); buttonDelete.addActionListener(this); panelBase.add(buttonAdd); panelBase.add(buttonShow); panelBase.add(buttonDelete); add(panelBase, BorderLayout.SOUTH); addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); setVisible(true); validate(); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if (e.getSource() == buttonAdd) { card.show(panelCenter, "添加"); stumsg = panelAdd.getMsg(); if (stumsg != null) { list.add(stumsg); } } else if (e.getSource() == buttonShow) { card.show(panelCenter, "显示"); panelShow.ShowMsg(list); } else if (e.getSource() == buttonDelete) { card.show(panelCenter, "删除"); stumsg = panelDelete.getMsg(); if (stumsg != null) { list.remove(stumsg); } } } } class PanelShow extends Panel { TextArea taShow; PanelShow() { taShow = new TextArea(10, 20); add (taShow); setVisible(true); validate(); } public void ShowMsg(LinkedList<StudentMsg> list) { StudentMsg stu; taShow.setText(null); for (int i=0; i<list.size(); ++i) { stu = list.get(i); taShow.append("学号: "+stu.m_id+"\n"+ "姓名: "+stu.m_name+"\n"+ "分数: "+stu.m_mark+"\n\n" ); } } } class PanelAdd extends Panel { // LinkedList<StudentMsg> list; public TextField tfId; public TextField tfName; public TextField tfMark; PanelAdd() { // this.list = list; setComponet(); setVisible(true); validate(); } //设置panel中的组件 void setComponet() { tfId = new TextField(10); tfName = new TextField(10); tfMark = new TextField(10); Box box1, box2, box3; box1 = Box.createVerticalBox(); box1.add(new Label("学号:")); box1.add(Box.createVerticalStrut(5)); box1.add(new Label("姓名:")); box1.add(Box.createVerticalStrut(5)); box1.add(new Label("分数:")); box2 = Box.createVerticalBox(); box2.add(tfId); box2.add(Box.createVerticalStrut(5)); box2.add(tfName); box2.add(Box.createVerticalStrut(5)); box2.add(tfMark); box3 = Box.createHorizontalBox(); box3.add(box1); box3.add(Box.createHorizontalStrut(4)); box3.add(box2); add(box3, BorderLayout.CENTER); } //获取学号 public String getId () { String str = tfId.getText().toString(); tfId.setText(null); if (str.trim().length() == 0) { return null; } else { return str; } } //获取姓名 public String getName() { String str = tfName.getText().toString(); tfName.setText(null); if (str.trim().length() == 0) { return null; } else { return str; } } //获取分数 public double getMark() { String str = tfMark.getText().toString(); tfMark.setText(null); if (str.trim().length() == 0) { return 0; } else { try { double dbMark = Double.parseDouble(str); return dbMark; } catch (NumberFormatException e) { System.out.println(e.getMessage()); } } return 0; } //获得学生信息 public StudentMsg getMsg() { String strid = getId(); String strname = getName(); if (strid==null || strname==null) { return null; } StudentMsg msg = new StudentMsg(strid, strname, getMark()); return msg; } } class PanelDelete extends PanelAdd { PanelDelete() { super(); setVisible(true); validate(); } //获得学生信息 public StudentMsg getMsg() { String strid = getId(); String strname = getName(); if (strid==null && strname==null) { return null; } StudentMsg msg = new StudentMsg(strid, strname, getMark()); return msg; } }功能界面: