| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1478 人关注过本帖, 1 人收藏
标题:CardLayout布局
只看楼主 加入收藏
llooppzhang
Rank: 7Rank: 7Rank: 7
来 自:江苏
等 级:黑侠
威 望:5
帖 子:308
专家分:518
注 册:2009-10-18
收藏
得分:0 
回复 9楼 诸葛修勤
但我试了,没用。。其他的代码是对的。。。
我才在menu那出错了。。
2011-04-23 22:28
诸葛修勤
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:11
帖 子:549
专家分:1955
注 册:2010-10-28
收藏
得分:0 
程序代码:
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.MenuItem;
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 = 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);
        }
    }   
}
//定义添加信息的类
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;
   
    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);
       
        add(box1);
        add(box2);
    }

    @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();
        }
    }
    //向链表中添加数据
    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());
        }
    }
}

//定义显示信息的类
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 set_list(LinkedList<student> list)
    {
        this.list = list;
    }
    public void show_list()
    {
        if (null == list)
        {
            ta_show.append("没有数据\n");
           
            return;
        }
        try
        {
            Iterator<student> iter = list.iterator();
            ta_show.setText(null);
           
            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());
        }
    }
}
2011-04-23 22:29
诸葛修勤
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:11
帖 子:549
专家分:1955
注 册:2010-10-28
收藏
得分:0 
回复 11楼 llooppzhang
现在把两个模块联系起来 我试着增加成员函数 提供修改成员变量的方式 达到list具有相同的内容
没用 好像运行机制不一样


2011-04-23 22:33
诸葛修勤
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:11
帖 子:549
专家分:1955
注 册:2010-10-28
收藏
得分:0 
程序代码:
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.CardLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
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 import import import import import import 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 implements Serializable
{
    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
{
    add panel_add = null;
    show panel_show = null;
    Delete panel_delete = null;
   
    MenuBar menubar = null;
    Menu menu = null;
    MenuItem menu_add = null;
    MenuItem menu_show = null;
    MenuItem menu_del = null;
    CardLayout card = null;

    yxf()
    {
        super("学生管理系统");
       
        card = new CardLayout();
        setLayout(card);
        panel_add = new add();
        add("添加",panel_add);
       
        panel_show = new show();
        add("显示", panel_show);
       
        panel_delete = new Delete();
        add("删除", panel_delete);
       
        menu = new Menu("功能");
        menu_add = new MenuItem("添加");
        menu_add.addActionListener(this);
        menu_show = new MenuItem("显示");
        menu_show.addActionListener(this);
        menu_del = new MenuItem("删除");
        menu_del.addActionListener(this);
       
        menu.add(menu_add);
        menu.add(menu_show);
        menu.add(menu_del);
        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, "添加");
            setSize(280, 200);
        }
        else if (e.getSource() == menu_show)
        {
            card.show(this, "显示");
            setSize(280, 300);
        }
        else if (e.getSource() == menu_del)
        {
            card.show(this, "删除");
            setSize(280, 200);
        }
    }   
}
//定义添加信息的类
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;
   
    file_do fd = new file_do();
   
    add()
    {
        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);
       
        add(box1);
        add(box2);
    }

    @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>();
            }
           
            if (0 != tf_id.getText().toString().trim().length() &&
                0 != tf_name.getText().toString().trim().length() &&
                0 != tf_mark.getText().toString().trim().length())
            {
                list_add();
                list = fd.write_file(list);
               
                tf_id.setText(null);
                tf_name.setText(null);
                tf_mark.setText(null);
            }
        }
    }
    //向链表中添加数据
    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());
        }
    }
}

//定义显示信息的类
class show extends Panel implements ActionListener
{
    LinkedList<student> list = new LinkedList<student>();
    TextArea ta_show = null;
    Button fl = null;
   
    file_do fd = new file_do();
   
    show()
    {
        fl = new Button("刷新");
        fl.addActionListener(this);
        ta_show = new TextArea(10, 30);
        add(ta_show, BorderLayout.CENTER);
        add(fl, BorderLayout.SOUTH);
        show_list();
        setVisible(true);
        validate();
    }
    public void show_list()
    {
        if (null == list)
        {
            ta_show.append("没有数据\n");
           
            return;
        }
        try
        {
            Iterator<student> iter = list.iterator();
            ta_show.setText("");
            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());
        }
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if (e.getSource() == fl)
        {
            list = fd.read_file(list);
            show_list();
        }
    }
}

class Delete extends Panel implements ActionListener
{
    LinkedList<student> list = null;
    Button ack = null;//确认按钮
    Button cancel = null;//取消按钮
   
    TextField tf_id;
    TextField tf_name;
    TextField tf_mark;
   
    file_do fd = new file_do();
   
    Delete()
    {
        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);
       
        add(box1);
        add(box2);
    }

    @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>();
            }
           
            if (0 != tf_id.getText().toString().trim().length() &&
                0 != tf_name.getText().toString().trim().length() &&
                0 != tf_mark.getText().toString().trim().length())
            {
                list = fd.read_file(list);
                delete_list();
                list = fd.write_file(list);
               
                tf_id.setText(null);
                tf_name.setText(null);
                tf_mark.setText(null);
            }
        }
    }
    void delete_list()
    {
        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());
            student stu = new student(str_id, str_name, dou);
           
            for (int i=0; i<list.size(); ++i)
            {
                if (((student)list.get(i)).equals(stu))
                {
                    list.remove(i);
                    --i;
                }
            }
        }
        catch (NumberFormatException e)
        {
            System.out.println("错误:"+e.getMessage());
        }
    }
}

class file_do
{
    LinkedList<student> write_file(LinkedList<student> list)
    {
        try
        {
            FileOutputStream out = new FileOutputStream("yxf.txt");
            ObjectOutputStream object_out = new ObjectOutputStream(out);
           
            Iterator<student> iter = list.iterator();
           
            while(iter.hasNext())
            {
                student stu = (student)iter.next();
                object_out.writeObject(stu);
            }
            out.close();
            object_out.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
       
        return list;
    }
    LinkedList<student> read_file(LinkedList<student> list)
    {
        list.clear();
        try
        {
            FileInputStream in = new FileInputStream("yxf.txt");
            ObjectInputStream object_in = new ObjectInputStream(in);
           
            try
            {
                while (true)
                {
                    student stu = (student)object_in.readObject();
                    list.add(stu);
                }
            }
            catch(EOFException e)
            {
                System.out.println("文件已读完");
            }
            catch (ClassNotFoundException e)
            {
                e.printStackTrace();
            }
            finally
            {
                in.close();
                object_in.close();
            }
        }
        catch (IOException e)
        {
            System.out.println("文件操作:"+e.getMessage());
        }
       
        return list;
    }
}
2011-04-24 09:30
诸葛修勤
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:11
帖 子:549
专家分:1955
注 册:2010-10-28
收藏
得分:0 
for (int i=0; i<list.size(); ++i)
            {
                if (((student)list.get(i)).equals(stu))
                {
                    list.remove(i);
                    --i;
                }
            }
这段代码能不能正确地删除掉list链表中满足条件的结点

因为不知道删除了第i个结点后 后面的结点是否会被打乱    这里我假设的是不会被打乱的  不晓得到底会不会打乱
2011-04-24 09:33
w123012306
Rank: 9Rank: 9Rank: 9
来 自:湖南
等 级:蜘蛛侠
威 望:4
帖 子:307
专家分:1180
注 册:2010-4-22
收藏
得分:0 
路过

楼上,楼下的一定要幸福开心哦!
2011-04-25 12:55
llooppzhang
Rank: 7Rank: 7Rank: 7
来 自:江苏
等 级:黑侠
威 望:5
帖 子:308
专家分:518
注 册:2009-10-18
收藏
得分:0 
回复 15楼 诸葛修勤
应该是抛出异常。。。
有一个帖子也是关于这个的。
还有,会不会乱交给LINKEDLIST了,不会的。。。
2011-04-25 15:10
pywepe
Rank: 6Rank: 6
等 级:侠之大者
威 望:4
帖 子:296
专家分:483
注 册:2009-4-5
收藏
得分:0 
你想干吗 命名啊

java群
62635216
欢迎加入
2011-04-26 13:03
诸葛修勤
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:11
帖 子:549
专家分:1955
注 册:2010-10-28
收藏
得分:0 
回复 18楼 pywepe
真的不明白你想表达什么意思  


2011-04-26 20:17
快速回复:CardLayout布局
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029444 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved