I need help with an applet that I am making for my coursework.
I am using card layout and I want the content of each card to be taken from different classes. Then in each card I want buttons to switch to other cards. I've seen examples that work in a similar way but they are based on JFrame rather than applet.
Please do not send me any external links, I've been researching card layout for several days already. I am a Java beginner and I am a slow learner. I would like someone to show me a basic example and explain how it is done. Thank you!
public class MainClass extends JApplet implements ActionListener {
boolean inAnApplet = true;
JButton btn1, btn2;
JPanel cards;
final static String PANEL1 = "";
final static String PANEL2 = "";
public MainClass() {
Container contentPane = getContentPane();
JPanel cbp = new JPanel();
cbp.add(new JLabel ("Hello world!"));
contentPane.add(cbp, BorderLayout.NORTH);
cards = new JPanel();
cards.setLayout(new CardLayout());
JPanel p1 = new JPanel();
p1.add(new JLabel("First panel!"));
btn1 = new JButton("Text panel");
btn1.addActionListener(this);
p1.add(btn1);
JPanel p2 = new JPanel();
p2.add(new JLabel("Second panel!"));
btn2 = new JButton("Button panel");
btn2.addActionListener(this);
p2.add(btn2);
cards.add(p1, PANEL1);
cards.add(p2, PANEL2);
contentPane.add(cards, BorderLayout.CENTER);
}
public void init(){}
public void actionPerformed(ActionEvent evt) {
CardLayout cl = (CardLayout)(cards.getLayout());
if(evt.getSource() == btn1)
{
cl.show(cards, PANEL1);
}
else if(evt.getSource() == btn2)
{
cl.show(cards, PANEL2);
}
}
}