2014-12-04 2 views
0

카드 레이아웃이 무엇인지 알아 내려고 애 쓰고 있습니다. 나는 많은 기사를 읽고이 작은 예제를 구현하여 카드 레이아웃이 어떻게 작동하는지 살펴 보았다. 그러나 나는 주석 처리 된 몇 가지 방법을 이해할 수 없다. 누군가 제발 나를 도울 수 있습니까 (저는 커맨드 라인을 사용합니다).카드 레이아웃 사용 방법?

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

class C_layout implements ActionListener 
{ 
    JButton b2; 
    JButton b1; 
    JFrame f1; 
    JPanel card1; 
    JPanel card2; 
    JPanel Jp; 
    void Example() 
    { 
    f1=new JFrame("CardLayout Exercise"); 
    f1.setLocationRelativeTo(null); 
    f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    f1.setSize(500,500); 
    f1.setVisible(true); 

    Container cont=f1.getContentPane(); 
    cont.setLayout(null); 

    Jp=new JPanel(new CardLayout()); //<-- How to implement card layout here (MAIN PANEL) 
    f1.add(Jp); 
    Jp.setLayout //<-- Not sure what means here ERROR 
    card1=new JPanel(); // First panel 
    Jp.add(card1); 
    card2=new JPanel(); // Second panel 
    Jp.add(card2); 

    JLabel lb1=new JLabel("This is the first Panel"); 
    lb1.setBounds(250,100,100,30); 
    card1.add(lb1); 

    b1=new JButton("NEXT >>"); 
    b1.setBounds(350,400,100,30); 
    b1.addActionListener(this); 
    card1.add(b1); 


    JLabel lb2=new JLabel("This is the second Panel"); 
    lb2.setBounds(250,100,100,30); 
    card2.add(lb2); 

    b2=new JButton("<< PREV"); 
    b2.setBounds(250,300,100,30); 
    b2.addActionListener(this); 
    card2.add(b2); 
    } 

    public void actionPerformed(ActionEvent e) 
    { 
    if(e.getSource()==b1) 
    { 
    CardLayout cardLayout = (CardLayout) Jp.getLayout(); 
    cardLayout.show(card2,"2"); 
    } 
    if(e.getSource()==b2) 
    { 
    // I still haven't implemented this action listener 
    } 
    } 
} 

class LayoutDemo1 
{ 
    public static void main(String[] args) 
    { 
    C_layout c=new C_layout(); 
    c.Example(); 


    } 
} 

답변

3

cont.setLayout(null); 당신은 그것을 관리하기 위해 귀하의 CardLayout에 대한 참조를 필요 해요

... 빨리 잃고, 나쁜, 나쁜 생각이다. ...

private CardLayout cardLayout; 

이제 CardLayout의 인스턴스를 생성하고 패널에 적용 ... CardLayout의 인스턴스 필드를 정의하여

cardLayout = new CardLayout(); 
Jp=new JPanel(cardLayout); 

이를 ... 시작

Jp.setLayout //<-- Not sure what means here ERROR 

은 아무 것도하지 않습니다. Java에 관한 한 유효한 진술은 아닙니다. 실제로 실제로는 LayoutManager에 대한 참조가 필요합니다. t를 사용할 수 있지만 이미 완료 했으므로 Jp의 인스턴스를 만들 때 필요하지 않습니다. ...

표시하려는 구성 요소를 식별 할 수있는 방법이 필요합니다. CardLayoutActionListener에, 지금은 ... 예를 들어, String 이름을 통해

card1=new JPanel(); // First panel 
Jp.add(card1, "card1"); 
card2=new JPanel(); // Second panel 
Jp.add(card2, "card2"); 

을이 작업을 수행, 당신은 ... show 필요한보기로 CardLayout 물어보고 싶은

public void actionPerformed(ActionEvent e) 
{ 
    if(e.getSource()==b1) 
    { 
     cardLayout.show(Jp1,"card2"); 
    } else if(e.getSource()==b2) 
    { 
     cardLayout.show(Jp1,"card1"); 
    } 
} 

CardLayout#show이 작동하려면 CardLayout이 할당 된 컨테이너의 참조와 표시 할보기의 이름을 페이싱해야합니다. D :

+0

가 대단히 감사합니다 자세한 내용은 How to Use CardLayout하세요! –

+1

호프가 도움이 되었으면 좋겠다. – MadProgrammer

+0

프레임을 보여주는 방법을 좀 더 설명해 주시겠습니까? 빈 프레임을 보여줍니다. 나는 CardLayout # show를 이해하지 못했다. –