public void actionPerformed(ActionEvent e) { CardLayout cl = (CardLayout)(mainPanel.getLayout()); if (e.getActionCommand().equals("next")) { if (index <= Integer.valueOf(LABELPANEL)) { index ++; cl.show(mainPanel, Integer.toString(index)); if (index > Integer.valueOf(LABELPANEL)) index = Integer.valueOf(LABELPANEL); } }
if (e.getActionCommand().equals("back")) { if (index >= Integer.valueOf(TEXTPANEL)) { index --; cl.show(mainPanel, Integer.toString(index)); if (index < Integer.valueOf(TEXTPANEL)) index = Integer.valueOf(TEXTPANEL); } } System.out.println(index); }
public static void main(String[] args) { WizardTest at = new WizardTest(); } }
import java.awt.event.ActionListener;
import java.awt.*;
import javax.swing.*;public class WizardTest extends JFrame implements ActionListener{
final static String TEXTPANEL = "0";
final static String BUTTONPANEL = "1";
final static String LABELPANEL = "2";
private JPanel mainPanel = new JPanel();
private JPanel panel1 = new JPanel();
private JPanel panel2 = new JPanel();
private JPanel panel3 = new JPanel();
private JPanel btnPanel = new JPanel();
private JButton nextBtn = new JButton("next");
private JButton backBtn = new JButton("back");
private int index = 0;
public WizardTest() {
this.setSize(500, 400);
this.setLayout(new BorderLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
backBtn.addActionListener(this);
nextBtn.addActionListener(this);
btnPanel.add(backBtn);
btnPanel.add(nextBtn);
panel1.add(new TextField("text Panel"));
panel2.add(new JButton("button Panel"));
panel3.add(new JLabel("label Panel"));
mainPanel.setLayout(new CardLayout());
mainPanel.add(panel1, TEXTPANEL);
mainPanel.add(panel2, BUTTONPANEL);
mainPanel.add(panel3, LABELPANEL);
this.add(mainPanel, BorderLayout.CENTER);
this.add(btnPanel, BorderLayout.SOUTH);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
CardLayout cl = (CardLayout)(mainPanel.getLayout());
if (e.getActionCommand().equals("next")) {
if (index <= Integer.valueOf(LABELPANEL)) {
index ++;
cl.show(mainPanel, Integer.toString(index));
if (index > Integer.valueOf(LABELPANEL))
index = Integer.valueOf(LABELPANEL);
}
}
if (e.getActionCommand().equals("back")) {
if (index >= Integer.valueOf(TEXTPANEL)) {
index --;
cl.show(mainPanel, Integer.toString(index));
if (index < Integer.valueOf(TEXTPANEL))
index = Integer.valueOf(TEXTPANEL);
}
}
System.out.println(index);
}
public static void main(String[] args) {
WizardTest at = new WizardTest();
}
}