The next function of my cardlayout is working properly, but previous isnt. As far as I'm concerned, just having "layout.previous(_);" in the actionPerformed method body in my makePanel() method should work, but when I run my program and click the prev button, nothing happens. What am I doing wrong? –
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
public class Temp
{
public static void main (String[] args)
{
new MakeAQuiz();
}
static class MakeAQuiz
{
private JPanel start, base, fields, buttonz, question;
private String [] labels = {"Enter your question: ", "Answer 1: ", "Answer 2: ", "Answer 3: ", "Answer 4: "};
private JButton [] buttons = {new JButton("<<Go back"), new JButton("I'm done"), new JButton("Next>>")};
private JFrame makeFrame;
public MakeAQuiz()
{
start = new JPanel(new CardLayout());
start.add(makePanel(),"1");
makeFrame = new JFrame();
makeFrame.setSize(500,600);
makeFrame.add(start);
makeFrame.setVisible(true);
makeFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public JFrame getJFrame()
{
return makeFrame;
}
public JPanel makePanel()
{
question = new JPanel(new FlowLayout());
fields = new JPanel(new GridBagLayout());
buttonz = new JPanel(new FlowLayout());
base = new JPanel(new BorderLayout());
GridBagConstraints c = new GridBagConstraints();
c.weighty=0.5; //adds padding between the fields vertically
for (int i = 1; i<5; i++)
{
c.gridy++; //puts each field in a seperate line/row
JLabel label = new JLabel(labels[i]);
// c.fill = GridBagConstraints.HORIZONTAL;
fields.add(label,c);
JTextField textField = new JTextField(20);
fields.add(textField,c);
}
final CardLayout layout = (CardLayout)start.getLayout();
buttons[1].addActionListener(new ActionListener() {
// @Override
public void actionPerformed(ActionEvent e) {
buttons[0].setEnabled(false);
buttons[2].setEnabled(false);
// for(Component comp : cardPanel.getComponents()) {
// if(comp instanceof Page) {
// Page page = (Page)comp;
// page.printData();
// }
// }
}
});
buttons[2].addActionListener(new ActionListener()
{
// @Override
public void actionPerformed(ActionEvent e)
{
start.add(makePanel(), String.valueOf(start.getComponentCount() + 1));
layout.next(start);
}
});
buttons[0].addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
layout.previous(start);
}
});
buttonz.add(buttons[0]);
buttonz.add(buttons[1]);
buttonz.add(buttons[2]);
JLabel l = new JLabel(labels[0]);
JTextField t = new JTextField(30);
question.add(l);
question.add(t);
base.add(question,BorderLayout.NORTH);
base.add(buttonz,BorderLayout.SOUTH);
base.add(fields,BorderLayout.CENTER);
return base;
}
}
}