CardLayout布局
rt。。。
class student { student(String id, String name, double mark) { m_id = new String(id); m_name = new String(name); m_mark = mark; } String show_id (){ return m_id;} String show_name() {return m_name;} double show_mark() { return m_mark;} private String m_id = null;//学生的学号 private String m_name = null;//学生的姓名 private double m_mark = 0;//学生的成绩 } class yxf extends Frame implements ActionListener { LinkedList<student> list = null;//定义一个链表 Panel panel_add = null; Panel panel_show = null; MenuBar menubar = null; Menu menu_add = null; Menu menu_show = null; CardLayout card = null; yxf() { super("学生管理系统"); card = new CardLayout(); setLayout(card); panel_add = new add(this, list); add("添加",panel_add); panel_show = new show(this, list); add("显示", panel_show); menu_add = new Menu("添加"); menu_add.addActionListener(this); menu_show = new Menu("显示"); menu_show.addActionListener(this); menubar = new MenuBar(); menubar.add(menu_add); menubar.add(menu_show); setMenuBar(menubar); 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() == menu_add) { card.show(this, "添加"); System.out.println(1); } else if (e.getSource() == menu_show) { card.show(this, "显示"); System.out.println(2); } } }
import java.awt.Button; import java.awt.CardLayout; import java.awt.Container; import java.awt.Frame; import java.awt.Label; import java.awt.Menu; import java.awt.MenuBar; import java.awt.Panel; import java.awt.TextArea; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.util.Iterator; import java.util.LinkedList; import javax.swing.Box; public class main { public static void main(String args[]) { new yxf(); } } class student { student(String id, String name, double mark) { m_id = new String(id); m_name = new String(name); m_mark = mark; } String show_id (){ return m_id;} String show_name() {return m_name;} double show_mark() { return m_mark;} private String m_id = null;//学生的学号 private String m_name = null;//学生的姓名 private double m_mark = 0;//学生的成绩 } class yxf extends Frame implements ActionListener { LinkedList<student> list = null;//定义一个链表 Panel panel_add = null; Panel panel_show = null; MenuBar menubar = null; Menu menu_add = null; Menu menu_show = null; CardLayout card = null; yxf() { super("学生管理系统"); card = new CardLayout(); setLayout(card); panel_add = new add(this, list); add("添加",panel_add); panel_show = new show(this, list); add("显示", panel_show); menu_add = new Menu("添加"); menu_add.addActionListener(this); menu_show = new Menu("显示"); menu_show.addActionListener(this); menubar = new MenuBar(); menubar.add(menu_add); menubar.add(menu_show); setMenuBar(menubar); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); setBounds(100, 100, 280, 200); setVisible(true); validate(); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if (e.getSource() == menu_add) { card.show(this, "添加"); System.out.println(1); } else if (e.getSource() == menu_show) { card.show(this, "显示"); System.out.println(2); } } } //定义添加信息的类 class add extends Panel implements ActionListener { LinkedList<student> list = null; Button ack = null;//确认按钮 Button cancel = null;//取消按钮 TextField tf_id; TextField tf_name; TextField tf_mark; // TextArea ta_show = null; add(Container con, LinkedList<student> list) { this.list = list; Box box1 = Box.createVerticalBox(); Box box2 = Box.createVerticalBox(); box1.add(new Label("学号:")); box1.add(Box.createVerticalStrut(4)); box1.add(new Label("姓名:")); box1.add(Box.createVerticalStrut(4)); box1.add(new Label("成绩:")); box1.add(Box.createVerticalStrut(4)); cancel = new Button("重置"); cancel.addActionListener(this); box1.add(cancel); tf_id = new TextField(10); tf_name = new TextField(10); tf_mark = new TextField(10); box2.add(tf_id); box2.add(Box.createVerticalStrut(4)); box2.add(tf_name); box2.add(Box.createVerticalStrut(4)); box2.add(tf_mark); box2.add(Box.createVerticalStrut(4)); ack = new Button("确定"); ack.addActionListener(this); box2.add(ack); // ta_show = new TextArea(10, 20); add(box1); add(box2); // add(ta_show); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if (e.getSource() == cancel) { tf_id.setText(null); tf_name.setText(null); tf_mark.setText(null); } else if (e.getSource() == ack) { if (null == list) { list = new LinkedList<student>(); } list_add(); // show_list(); } } //向链表中添加数据 void list_add() { String str_id = new String(tf_id.getText()); String str_name = new String(tf_name.getText()); double dou = 0; try { dou = Double.parseDouble(tf_mark.getText()); list.add(new student(str_id, str_name, dou)); } catch (NumberFormatException e) { System.out.println("错误:"+e.getMessage()); } } // void show_list() // { // if (null == list) // { // ta_show.append("没有数据\n"); // // return; // } // try // { // ta_show.setText(null); // Iterator<student> iter = list.iterator(); // // while (iter.hasNext()) // { // student temp = (student)iter.next(); // ta_show.append("学号:"+temp.show_id()+" 姓名:"+temp.show_name()+" 成绩:"+temp.show_mark()+"\n"); // } // } // catch (NullPointerException e) // { // System.out.println("Iterator " +e.getMessage()); // } // } } //定义显示信息的类 class show extends Panel { LinkedList<student> list = null; TextArea ta_show = null; show(Container con, LinkedList<student> list) { this.list = list; ta_show = new TextArea(10, 20); add(ta_show); show_list(); setVisible(true); validate(); } public void show_list() { if (null == list) { ta_show.append("没有数据\n"); return; } try { Iterator<student> iter = list.iterator(); while (iter.hasNext()) { student temp = (student)iter.next(); ta_show.append("学号:"+temp.show_id()+" 姓名:"+temp.show_name()+" 成绩:"+temp.show_mark()+"\n"); } } catch (NullPointerException e) { System.out.println("Iterator " +e.getMessage()); } } }
menu_add.addActionListener(this);它有这个监听 应该就可以用
LinkedList<student> list = null;//定义一个链表 Panel panel_add = null; Panel panel_show = null; MenuBar menubar = null; Menu menu = null; MenuItem menu_add = null; MenuItem menu_show = null; CardLayout card = null; yxf() { super("学生管理系统"); card = new CardLayout(); setLayout(card); panel_add = new add(this, list); add("添加",panel_add); panel_show = new show(this, list); add("显示", panel_show); menu = new Menu("功能"); menu_add = new MenuItem("添加"); menu_add.addActionListener(this); menu_show = new MenuItem("显示"); menu_show.addActionListener(this); menu.add(menu_add); menu.add(menu_show); menubar = new MenuBar(); menubar.add(menu); setMenuBar(menubar); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); setBounds(100, 100, 280, 200); setVisible(true); validate(); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if (e.getSource() == menu_add) { card.show(this, "添加"); System.out.println(1); setSize(280, 200); } else if (e.getSource() == menu_show) { card.show(this, "显示"); System.out.println(2); setSize(280, 300); } }现在两个功能连不起来了