I Can't switch my CardLayout @.@

There is a card 3 but i havn't add ,so the "Enterbtn" is to trigger card 3.

So what is wrong with my code that it can't switch? I changed the cardPanel from 2 to 1 and 1 to 2 but it only appear card 2.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.*;

public class HPhoneInv extends JFrame {

private int currentCard = 1;

private JPanel cardPanel;

private CardLayout cl;

public HPhoneInv() {

setTitle("Handphone Inventory System");

setSize(500, 160);

cardPanel = new JPanel();

cl = new CardLayout();

cardPanel.setLayout(cl);

JPanel jp1 = new JPanel();

jp1.setLayout(new GridLayout(1,1) );

JLabel jl1 = new JLabel("Select Option:");

jp1.add(jl1);

cardPanel.add(jp1, "1");

JPanel buttonPanel = new JPanel();

buttonPanel.setLayout(new GridLayout(1,2) );

JButton AcqBtn=new JButton("Used Phone Acquisition");

JButton TranBtn=new JButton("Sale Transaction");

buttonPanel.add(AcqBtn);

buttonPanel.add(TranBtn);

getContentPane().add(jp1, BorderLayout.NORTH);

getContentPane().add(buttonPanel, BorderLayout.SOUTH);

JPanel jp2 = new JPanel();

jp2.setLayout(new GridLayout(5,2) );

JLabel lbl1 = new JLabel("Cust IC No:");

JLabel lbl2= new JLabel("Brand:");

JLabel lbl3 =new JLabel("Model:");

JLabel lbl4=new JLabel("Serial No:");

JLabel lbl5=new JLabel("Cost:");

JTextField tf1=new JTextField("");

JTextField tf2=new JTextField("");

JTextField tf3=new JTextField("");

JTextField tf4=new JTextField("");

JTextField tf5=new JTextField("");

jp2.add(lbl1);

jp2.add(tf1);

jp2.add(lbl2);

jp2.add(tf2);

jp2.add(lbl3);

jp2.add(tf3);

jp2.add(lbl4);

jp2.add(tf4);

jp2.add(lbl5);

jp2.add(tf5);

cardPanel.add(jp2, "2");

JPanel buttonPanel2 = new JPanel();

buttonPanel2.setLayout(new GridLayout(1,2) );

JButton EnterBtn=new JButton("Enter");

JButton CancelBtn=new JButton("Cancel");

buttonPanel2.add(EnterBtn);

buttonPanel2.add(CancelBtn);

getContentPane().add(jp2, BorderLayout.NORTH);

getContentPane().add(buttonPanel2, BorderLayout.SOUTH);

AcqBtn.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

if (currentCard < 2) {

currentCard +=1;

cl.show(cardPanel, "" + (currentCard));

}

}

});

TranBtn.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

if (currentCard < 2) {

currentCard += 1;

cl.show(cardPanel, "" + (currentCard));

}

}

});

EnterBtn.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

if (currentCard > 1) {

currentCard = 1;

}

}

});

CancelBtn.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

if (currentCard > 1) {

currentCard = 1;

cl.show(cardPanel, "" + (currentCard));

}

}

});

}

public static void main(String[] args) {

HPhoneInv cl = new HPhoneInv();

cl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

cl.setVisible(true);

}

}

Message was edited by:

RockmanEXE

Message was edited by:

RockmanEXE

[3501 byte] By [RockmanEXEa] at [2007-11-27 10:24:45]
# 1

"How to Use Card Layout"

http://java.sun.com/docs/books/tutorial/uiswing/layout/card.html

Don't forget to use the "Code Formatting Tags",

see http://forum.java.sun.com/help.jspa?sec=formatting,

so the posted code retains its original formatting.

camickra at 2007-7-28 17:29:44 > top of Java-index,Desktop,Core GUI APIs...
# 2

The code i get from java tips is more simple than of sun.Refer to the link below ,still can't switch from layer 2 to one.

http://www.java-tips.org/java-se-tips/java.awt/how-to-use-awt-cardlayout.html

Message was edited by:

RockmanEXE

RockmanEXEa at 2007-7-28 17:29:44 > top of Java-index,Desktop,Core GUI APIs...
# 3

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.*;

public class HPhoneInv extends JFrame {

private int currentCard = 1;

private JPanel cardPanel;

private CardLayout cl;

public HPhoneInv() {

setTitle("Handphone Inventory System");

setSize(500, 160);

cardPanel = new JPanel();

cl = new CardLayout();

cardPanel.setLayout(cl);

JPanel jp1 = new JPanel();

jp1.setLayout(new GridLayout(1,1) );

JLabel jl1 = new JLabel("Select Option:");

jp1.add(jl1);

cardPanel.add(jp1, "1");

JPanel buttonPanel = new JPanel();

buttonPanel.setLayout(new GridLayout(1,2) );

JButton AcqBtn=new JButton("Used Phone Acquisition");

JButton TranBtn=new JButton("Sale Transaction");

buttonPanel.add(AcqBtn);

buttonPanel.add(TranBtn);

getContentPane().add(jp1, BorderLayout.NORTH);

getContentPane().add(buttonPanel, BorderLayout.SOUTH);

JPanel jp2 = new JPanel();

jp2.setLayout(new GridLayout(5,2) );

JLabel lbl1 = new JLabel("Cust IC No:");

JLabel lbl2= new JLabel("Brand:");

JLabel lbl3 =new JLabel("Model:");

JLabel lbl4=new JLabel("Serial No:");

JLabel lbl5=new JLabel("Cost:");

JTextField tf1=new JTextField("");

JTextField tf2=new JTextField("");

JTextField tf3=new JTextField("");

JTextField tf4=new JTextField("");

JTextField tf5=new JTextField("");

jp2.add(lbl1);

jp2.add(tf1);

jp2.add(lbl2);

jp2.add(tf2);

jp2.add(lbl3);

jp2.add(tf3);

jp2.add(lbl4);

jp2.add(tf4);

jp2.add(lbl5);

jp2.add(tf5);

cardPanel.add(jp2, "2");

JPanel buttonPanel2 = new JPanel();

buttonPanel2.setLayout(new GridLayout(1,2) );

JButton EnterBtn=new JButton("Enter");

JButton CancelBtn=new JButton("Cancel");

buttonPanel2.add(EnterBtn);

buttonPanel2.add(CancelBtn);

getContentPane().add(jp2, BorderLayout.NORTH);

getContentPane().add(buttonPanel2, BorderLayout.SOUTH);

AcqBtn.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

if (currentCard < 2) {

currentCard +=1;

cl.show(cardPanel, "" + (currentCard));

}

}

});

TranBtn.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

if (currentCard < 2) {

currentCard += 1;

cl.show(cardPanel, "" + (currentCard));

}

}

});

EnterBtn.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

if (currentCard > 1) {

currentCard = 1;

}

}

});

CancelBtn.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

if (currentCard > 1) {

currentCard = 1;

cl.show(cardPanel, "" + (currentCard));

}

}

});

}

public static void main(String[] args) {

HPhoneInv cl = new HPhoneInv();

cl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

cl.setVisible(true);

}

}

RockmanEXEa at 2007-7-28 17:29:44 > top of Java-index,Desktop,Core GUI APIs...
# 4

The order of the components, added into the cards and into the JFrame are wrong, so i just rearranged the order and i added one more JPanel.

so based on that, u code accordinglyimport java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.*;

public class HPhoneInv extends JFrame {

private int currentCard = 1;

private JPanel cardPanel;

private CardLayout cl;

JPanel card1 = null;

JPanel card2 = null;

public HPhoneInv() {

setTitle("Handphone Inventory System");

setSize(500, 160);

cardPanel = new JPanel(new CardLayout());

JPanel jp1 = new JPanel();

jp1.setLayout(new GridLayout(1,1) );

JLabel jl1 = new JLabel("Select Option:");

jp1.add(jl1);

//cardPanel.add(jp1, "1");

JPanel buttonPanel = new JPanel();

buttonPanel.setLayout(new GridLayout(1,2) );

JButton AcqBtn=new JButton("Used Phone Acquisition");

JButton TranBtn=new JButton("Sale Transaction");

buttonPanel.add(AcqBtn);

buttonPanel.add(TranBtn);

card1 = new JPanel(new BorderLayout());

//getContentPane().add(jp1, BorderLayout.NORTH);

//getContentPane().add(buttonPanel, BorderLayout.SOUTH);

card1.add(jp1, BorderLayout.NORTH);

card1.add(buttonPanel, BorderLayout.SOUTH);

card2 = new JPanel(new BorderLayout());

JPanel jp2 = new JPanel();

jp2.setLayout(new GridLayout(5,2) );

JLabel lbl1 = new JLabel("Cust IC No:");

JLabel lbl2= new JLabel("Brand:");

JLabel lbl3 =new JLabel("Model:");

JLabel lbl4=new JLabel("Serial No:");

JLabel lbl5=new JLabel("Cost:");

JTextField tf1=new JTextField("");

JTextField tf2=new JTextField("");

JTextField tf3=new JTextField("");

JTextField tf4=new JTextField("");

JTextField tf5=new JTextField("");

jp2.add(lbl1);

jp2.add(tf1);

jp2.add(lbl2);

jp2.add(tf2);

jp2.add(lbl3);

jp2.add(tf3);

jp2.add(lbl4);

jp2.add(tf4);

jp2.add(lbl5);

jp2.add(tf5);

//cardPanel.add(jp2, "2");

JPanel buttonPanel2 = new JPanel();

buttonPanel2.setLayout(new GridLayout(1,2) );

JButton EnterBtn=new JButton("Enter");

JButton CancelBtn=new JButton("Cancel");

buttonPanel2.add(EnterBtn);

buttonPanel2.add(CancelBtn);

//getContentPane().add(jp2, BorderLayout.NORTH);

//getContentPane().add(buttonPanel2, BorderLayout.SOUTH);

card2.add(jp2, BorderLayout.NORTH);

card2.add(buttonPanel2, BorderLayout.SOUTH);

cardPanel.add(card2,"two");

cardPanel.add(card1,"one");

this.getContentPane().add(cardPanel);

AcqBtn.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

CardLayout cardLayout = (CardLayout)(cardPanel.getLayout());

cardLayout.show(cardPanel,"two");

/*if (currentCard < 2) {

currentCard +=1;

cl.show(cardPanel, "" + (currentCard));

}

**/

}

});

TranBtn.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

CardLayout cardLayout = (CardLayout)(cardPanel.getLayout());

cardLayout.show(cardPanel,"two");

/*if (currentCard < 2) {

currentCard += 1;

cl.show(cardPanel, "" + (currentCard));

}*/

}

});

EnterBtn.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

//System.out.println("Enter Button got clicked");

CardLayout cardLayout = (CardLayout)(cardPanel.getLayout());

cardLayout.show(cardPanel,"one");

}

});

CancelBtn.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

System.exit(0);

/*

if (currentCard > 1) {

currentCard = 1;

cl.show(cardPanel, "" + (currentCard));

}*/

}

});

}

public static void main(String[] args) {

HPhoneInv cl = new HPhoneInv();

cl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

cl.setVisible(true);

}

}

nirgun_xyza at 2007-7-28 17:29:44 > top of Java-index,Desktop,Core GUI APIs...
# 5

Thx you.Joining Sun Forum is the right thing.

RockmanEXEa at 2007-7-28 17:29:44 > top of Java-index,Desktop,Core GUI APIs...