여러 개의 카드가 쌓여 있는 형태로 컴포넌트를 배치하는 경우에 사용한다. CardLayout 클래스를 사용하였을 경우 항상 맨 위의 컴포넌트만이 보인다. 사용자는 화면에 보이는 카드를 여러 가지 CardLayout 클래스의 메소드를 사용하여 선택할 수 있다. 즉 맨 처음이나 마지막 카드, 다음 카드, 이전 카드, 특정한 이름을 갖는 카드를 지정할 수 있다.


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MyFrame4 extends JFrame
{
    public MyFrame4()
    {
        this.setTitle("CardLayoutTest");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(400,200);
       
        JPanel panel = new JPanel(new GridLayout(1,5));
        CreateButton("<<",panel);
        CreateButton("<",panel);
        CreateButton(">",panel);
        CreateButton(">>",panel);
        CreateButton("종료",panel);
       
        this.add(panel);
        this.setVisible(true);
    }
   
    private void CreateButton(String bt, JPanel panel)
    {
        JButton b = new JButton(bt);
        panel.add(b);
    }
}










import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
 * 여기?? MyFrame4 ???래스 설명??? 작성하십시오.
 *
 * @author (작성?? ???름)
 * @version (버전번호나 날짜)
 */
public class MyFrame4 extends JFrame implements ActionListener
{
    Cards cards;
    JPanel panel;
    public MyFrame4()
    {
        this.setTitle("CardLayoutTest");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(400,200);
       
        panel = new JPanel(new GridLayout(1,5));
        CreateButton("<<",panel);
        CreateButton("<",panel);
        CreateButton(">",panel);
        CreateButton(">>",panel);
        CreateButton("종료",panel);
       
        this.add(panel,BorderLayout.NORTH);
       
        cards = new Cards();
        this.add(cards, BorderLayout.CENTER);
        this.setVisible(true);
    }
   
    private void CreateButton(String bt, JPanel panel)
    {
        JButton b = new JButton(bt);
        b.addActionListener();
        panel.add(b);
    }
    class Cards extends JPanel
    {
        CardLayout cl;
        public Cards()
        {
            cl = new CardsLayout();
            setLayout(cl);
            for(int i=1;i<=10;i++)
            {
                add(new JButton("현재 카드의 번호는"+i+"입니다."),"Center");
            }
        }
    }
    public void actionPerformed(ActionEvent e)
    {
        String s = e.getActionCommand();
        if(s=="종료")
        {
            System.exit(0);
        }
        else if(s=="<<")
        {
            cards.first(cl);
        }
        else if(s=="<")
        {
            cards.previous(cl);
        }
        else if(s==">")
        {
            cards.next(cl);
        }
        else if(s==">>")
        {
            cards.last(cl);
        }
       
       
    }
}

'컴퓨터공학부 > 고급객체지향프로그래밍(Java)' 카테고리의 다른 글

과제2  (0) 2010.10.29
FlowLayout  (0) 2010.10.29
BoxLayout 클래스  (0) 2010.10.29
GridLayout 클래스  (0) 2010.10.29
BorderLayout 클래스  (0) 2010.10.29

+ Recent posts