当前位置: 代码迷 >> Java相关 >> CardLayout布局
  详细解决方案

CardLayout布局

热度:418   发布时间:2011-04-23 19:12:33.0
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);
        }
    }   
}

----------------解决方案--------------------------------------------------------

----------------解决方案--------------------------------------------------------
程序想实现学生的添加信息  
点击显示可以显示所有的学生信息想这样子的:

----------------解决方案--------------------------------------------------------


本来是用CardLayout布局  来达到在两个Panel panel_add 和panel_show之间进行切换的  但是这里实现不成功
程序代码:
        menu_add = new Menu("添加");
        menu_add.addActionListener(this);
      
        menu_show = new Menu("显示");
        menu_show.addActionListener(this);
添加的事件没响应
----------------解决方案--------------------------------------------------------
这是 全部的代码
程序代码:
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());
        }
    }
}


----------------解决方案--------------------------------------------------------
add(String s, Componnment b)
知道Panel是容器   

根据add(String s, Componnment b) 好像是不能像我那样子用 但是没有报异常 就忽略了
现在的主要的问题是 点击菜单 没有反应
----------------解决方案--------------------------------------------------------
菜单项用MenuItem,不是用menu类。。
windowsIE浏览器坐例子:
“File”菜单是用menu类。。
“退出”菜单项用menuItem类。。
整个是menuBar类。。

本帖最近评分记录
2011-04-23 12:10:06
诸葛修勤

等 级:版主
威 望:7
帖 子:528
专家分:1826
注 册:2010-10-28
  得分:0 
程序代码:
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);
        }
    }   
现在两个功能连不起来了
----------------解决方案--------------------------------------------------------