-
Card Layout, what is wrong?
I've complied my applet file sucessfully but when I hit any button, a long list of errors appear. Can anyone point me in the right direction?
(Sorry, don't know how to do a code box on this board)
//Input Card
JLabel labelInput = new JLabel ("Input", SwingConstants.CENTER);
JPanel inputCard = new JPanel();
inputCard.add (labelInput);
deck.add (inputCard, labelInput.getText());
...other cards declared like this too...
//add listeners
input.addActionListener (this);
admit.addActionListener (this);
display.addActionListener (this);
public void actionPerformed (ActionEvent event)
{
//show cards
if (event.getSource() == input)
cardManager.first(deck);
else if (event.getSource ()== admit)
cardManager.next(deck);
else if (event.getSource()==display);
cardManager.last(deck);
-
Whatever the errors are they are not in the code you have posted here.
eschew obfuscation
-
OK, I wanted to avoid posting the entire code but the error must be here somewhere.
entire code follows.
==
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ApplicationCentre extends JApplet implements ActionListener
{
private JPanel main, deck;
private JLabel labelN, labelS, labelE, labelW;
private JButton input, admit, display;
private Container container;
private CardLayout cardManager;
// End of variables declaration
public void init ()
{
initComponents();
setSize(400, 100);
}//end init
public void initComponents()
{
container = getContentPane();
container.setLayout( new BorderLayout() );
// Creating instances of each item
main = new JPanel();
input = new JButton("Input");
admit = new JButton("Admit");
display = new JButton("Display");
labelE = new JLabel("East");
// End Creation
//set up main menu
main.setLayout (new GridLayout (3,0));
main.add(input);
main.add (admit);
main.add (display);
//set up deck
CardLayout cardManager = new CardLayout();
deck = new JPanel();
//deck.setLayout (cardManager);
//Input Card
JLabel labelInput = new JLabel ("Input", SwingConstants.CENTER);
JPanel inputCard = new JPanel();
inputCard.add (labelInput);
deck.add (inputCard, labelInput.getText());
//Admit Card
JLabel labelAdmit = new JLabel ("Admit");
JPanel admitCard = new JPanel();
admitCard.add (labelAdmit);
deck.add (admitCard);
//Display Card
JLabel labelDisplay = new JLabel ("Display");
JPanel displayCard = new JPanel();
displayCard.add (labelAdmit);
deck.add (displayCard);
//add listeners
input.addActionListener (this);
admit.addActionListener (this);
display.addActionListener (this);
container.add( main, BorderLayout.WEST);
container.add(deck, BorderLayout.EAST);
}//end initcomponents
//buttons!!
public void actionPerformed (ActionEvent event)
{
//show cards
if (event.getSource() == input)
cardManager.first(deck);
else if (event.getSource ()== admit)
cardManager.next(deck);
else if (event.getSource()==display);
cardManager.last(deck);
}//end button handler
}
-
See my comments
There was some problems with your use of a cardlayout 
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ApplicationCentre
extends JApplet
implements ActionListener {
private JPanel main, deck;
private JLabel labelN, labelS, labelE, labelW;
private JButton input, admit, display;
private Container container;
private CardLayout cardManager;
// End of variables declaration
public void init() {
initComponents();
setSize(400, 100);
} //end init
public void initComponents() {
container = getContentPane();
container.setLayout(new BorderLayout());
// Creating instances of each item
main = new JPanel();
input = new JButton("Input");
admit = new JButton("Admit");
display = new JButton("Display");
labelE = new JLabel("East");
// End Creation
//set up main menu
main.setLayout(new GridLayout(3, 0));
main.add(input);
main.add(admit);
main.add(display);
//set up deck
//CardLayout cardManager = new CardLayout();
// ERROR ******************
// the above is a redeclaration, the cardManager here is local to the
// initComponents method, the cardManager declared at class level
// is still null
cardManager = new CardLayout();
deck = new JPanel(cardManager);
//deck.setLayout (cardManager);
//Input Card
JLabel labelInput = new JLabel("Input", SwingConstants.CENTER);
JPanel inputCard = new JPanel();
inputCard.add(labelInput);
deck.add(inputCard, "Input");
//Admit Card
JLabel labelAdmit = new JLabel("Admit");
JPanel admitCard = new JPanel();
admitCard.add(labelAdmit);
deck.add(admitCard, "Admit");
//Display Card
JLabel labelDisplay = new JLabel("Display");
JPanel displayCard = new JPanel();
//displayCard.add(labelAdmit); // <--- ooops
displayCard.add(labelDisplay);
deck.add(displayCard,"Display");
//add listeners
input.addActionListener(this);
admit.addActionListener(this);
display.addActionListener(this);
container.add(main, BorderLayout.WEST);
container.add(deck, BorderLayout.EAST);
} //end initcomponents
//buttons!!
public void actionPerformed(ActionEvent event) {
//show cards
if (event.getSource() == input)
cardManager.show(deck,"Input");
else if (event.getSource() == admit)
cardManager.show(deck,"Admit");
else if (event.getSource() == display)
cardManager.show(deck,"Display");
} //end button handler
public static void main(String[] args) {
JFrame f = new JFrame("Applet tester");
f.getContentPane().setLayout(new GridLayout());
ApplicationCentre applet = new ApplicationCentre();
f.getContentPane().add(applet);
applet.init();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.setBounds(100, 100, 300, 300);
f.setVisible(true);
}
}
eschew obfuscation
-
OK, I fixed my code as indicated above but pushing the buttons on the West still does nothing to flip the cards.
-
Then you have missed out on a detail or two cause this code flips cards like mad
on my computer.
Check it again.
eschew obfuscation
-
Similar Threads
-
By thomastl in forum Java
Replies: 1
Last Post: 03-29-2009, 11:49 AM
-
By thomastl in forum Java
Replies: 4
Last Post: 01-22-2007, 02:45 AM
-
Replies: 0
Last Post: 04-25-2001, 09:29 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|