A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 陌花╮有意、 中级黑马   /  2012-6-25 00:55  /  2335 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

import java.awt.*;
import java.awt.event.*;

import javax.swing.JFrame;

public class CardLayout_1 {
        public static void main(String args[]) {
                new CardLayout_1().go();
        }

        public void go() {
                final Frame f = new Frame("CardLayout演示");
                f.addWindowListener(new WindowAdapter() {
                        public void windowClosing(WindowEvent evt) {
                                f.setVisible(false);
                                f.dispose();
                                System.exit(0);
                        }
                });

                f.setSize(300, 100);
                f.setLayout(new CardLayout());

                final Frame f1 = f;
                for (int i = 1; i <= 5; i++) {
                        Button b = new Button("Button " + i);
                        b.setSize(100, 25);
                        b.addActionListener(new ActionListener() {
                                public void actionPerformed(ActionEvent ae) {
                                        CardLayout cl = (CardLayout) f1.getLayout();
                                        cl.next(f1);
                                }
                        });
                        f.add(b, "button" + i);
                }
                f.setVisible(true);
        }
}

谁能给解释下这个卡片布局到底是怎么运行的  希望有更好的例子可以借鉴

2 个回复

倒序浏览
public class CardLayout_1 {
        public static void main(String args[]) {
                new CardLayout_1().go();
        }

        public void go() {
                final Frame f = new Frame("CardLayout演示");
                f.addWindowListener(new WindowAdapter() {
                        public void windowClosing(WindowEvent evt) {
                                f.setVisible(false);
                                f.dispose();
                                System.exit(0);
                        }
                });

                f.setSize(300, 100);
                f.setLayout(new FlowLayout());
                //f.setLayout(new CardLayout());buttom按钮自动充满窗体,改为f.setLayout(new FlowLayout())就可以看到全部布局。

                final Frame f1 = f;
                for (int i = 1; i <= 5; i++) {
                        Button b = new Button("Button " + i);
                        b.setSize(100, 25);
                        b.addActionListener(new ActionListener() {
                                public void actionPerformed(ActionEvent ae) {
                                        CardLayout cl = (CardLayout) f1.getLayout();
                                        cl.next(f1);
                                }
                        });
                        f.add(b, "button" + i);
                }
                f.setVisible(true);
        }
}
看完毕老师视频写的例子:

import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class MyMenuTest
{
                private Frame f;
                private MenuBar bar;
                private TextArea ta;
                private Menu fileMenu;
                private MenuItem openItem,saveItem,closeItem;
               
                private FileDialog openDia,saveDia;
               
                private File file;
                MyMenuTest()
                {
                        init();
                       
                }
                public void init()
                {
                                f = new Frame("my window");
                                f.setBounds(300,100,650,600);
                                //f.setLayout(new FlowLayout());//边界布局
                               
                               
                                bar = new MenuBar();
                               
                                ta = new TextArea();
                               
                                fileMenu = new Menu("文件");
                               
                                openItem = new MenuItem("文件");
                                saveItem = new MenuItem("保存");
               closeItem = new MenuItem("退出");
              
               fileMenu.add(openItem);
               fileMenu.add(saveItem);
               fileMenu.add(closeItem);
               bar.add(fileMenu);
              
               f.setMenuBar(bar);
               openDia = new FileDialog(f,"我要打开",FileDialog.LOAD);
               saveDia = new FileDialog(f,"我要保存",FileDialog.SAVE);
              
               f.add(ta);
               myEvent();
              
               f.setVisible(true);
              
              
                       
                }
                private void myEvent()
                {
                        saveItem.addActionListener(new ActionListener()
                        {
                                public void actionPerformed(ActionEvent e)
                                {
                                        if(file ==null)
                                        {
                                        saveDia.setVisible(true);
                                       
                                        String dirPath =saveDia.getDirectory();
                                        String fileName = saveDia.getFile();
                                        if(dirPath==null || fileName== null)
                                         return;
                                         
                                         file = new File(dirPath,fileName);
                                        }
                                        try
                                        {
                                                BufferedWriter bufw = new BufferedWriter(new FileWriter(file));
                                               
                                                String text = ta.getText();
                                               
                                                bufw.write(text);
                                                //bufw.flush();
                                                bufw.close();
                                               
                                               
                                        }
                                  catch(IOException ex)
                                  {
                                          throw new RuntimeException();
                                  }
                               
                                       
                                }
                               
                        });
                        openItem.addActionListener(new ActionListener()
                        {
                                 public void actionPerformed(ActionEvent e)
                                 {
                                           openDia.setVisible(true);
                                           String dirPath = openDia.getDirectory();//路径
                                           String fileName = openDia.getFile();//文件名
                                           System.out.println(dirPath+"......"+fileName);
                                           if (dirPath == null || fileName==null)
                                           return;
                                           
                                           ta.setText("");//清空
                                            file = new File(dirPath,fileName);//封装文件
                                           
                                           try
                                           {
                                                  
                                                   BufferedReader bufr = new BufferedReader(new FileReader(file));
                                                   String line =null ;
                                                  
                                                   while((line=bufr.readLine())!=null)
                                                   {
                                                           ta.append(line+"\r\n");
                                                   }
                                                   bufr.close();
                                                  
                                                  
                                           }
                                         catch(IOException ex)
                                  {
                                          throw new RuntimeException("读取失败");
                                  }
                                        
                                        
                                }
                        });
                        closeItem.addActionListener(new ActionListener()
                        {
                                //closeItem.addPerformed(ActionEvent e)
                       
                                        public void actionPerformed(ActionEvent e)
                                        {
                                                System.exit(0);
                                        }
                               
                               
                        });
                        f.addWindowListener(new WindowAdapter()
                        {
                                public void windowClosing(WindowEvent e)
                                {
                                        System.exit(0);
                                }
                        });
                       
                }
               
               
                public static void main(String[] args)
                {
                        new MyMenuTest();
                       
                }
       
}
回复 使用道具 举报
/*
卡片布局是一种共享容器面板的技术。当用鼠标单击控件的选项后,将显示不同的功能界面
卡片布局把容器面板视作一盒卡片,当控件添加到面板后,将自动放入一个卡片中,单击后进入下一个卡片
用到的类:
java.awt.CardLayout;
javax.swing.JButton;
因为有事件产生用到事件类
java.awt.event.ActionListener;
java.awt.enent.ActionEvent;

可以看一下下面的例子,当单击jButton1时,就会出现jButton2,单击jButton2时,出现
jButton3
*/
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
class UsedCardLayout extends JFrame implements ActionListener
{

      //创建卡片布局管理器
      CardLayout cardLayout1 = new CardLayout(20,30);
      JButton jButton1 = new JButton("按钮1");
      JButton jButton2 = new JButton("按钮2");
      JButton jButton3 = new JButton("按钮3");

      UsedCardLayout()
      {
           setTitle("卡片布局示例");
           setSize(300,200);
           setDefaultCloseOperation(EXIT_ON_CLOSE);
  
           this.getContentPane().setLayout(cardLayout1);
           this.getContentPane().add("myCard1",jButton1);
           this.getContentPane().add("myCard2",jButton2);
           this.getContentPane().add("myCard3",jButton3);
          //添加监听
           jButton1.addActionListener(this);
           jButton2.addActionListener(this);
           jButton3.addActionListener(this);
          setVisible(true);

           }

//进行卡片翻阅 next 显示下一个卡片 first 显示第一个卡片
      public void actionPerformed(ActionEvent e)
      {
           Object source = e.getSource();
           if(source == jButton1 || source == jButton2 )
           {
                 cardLayout1.next(this.getContentPane());
   
            }
            else if(e.getSource() == jButton3)
           {
                    cardLayout1.first(this.getContentPane());
   
            }
  
       }
   public static void main(String[] args)
{
          UsedCardLayout usedCardLayout = new UsedCardLayout();
}

}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马