I'm doing a project for my class and I'm working on the GUI right now. I don't have much because, well, it's not showing up and it's infuriating. Here's my code.
public class BookQuizGUI extends JFrame implements ActionListener
{
private Container c;
private JPanel pnlButtons;
private JButton addQs;
private JButton takeQuiz;
private JButton quit;
private Container c2;
private JPanel pnlButtons2;
private JComboBox qType;
private JComboBox ans;
private JTextField q;
private JTextField cA;
private JTextField cB;
private JTextField cC;
private JTextField cD;
private JButton add;
private JButton writeAll;
private JButton done;
/**
*
*/
public BookQuizGUI()
{
//The main screen for when the program starts
c = getContentPane();
pnlButtons = new JPanel();
pnlButtons.setLayout(new GridLayout(1, 3));
addQs = new JButton("Add Questions");
takeQuiz = new JButton("Take Quiz");
quit = new JButton("Quit");
setTitle("Book Quiz");
setSize(800, 400);
setLocation(400, 250);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pnlButtons.add(addQs);
pnlButtons.add(takeQuiz);
pnlButtons.add(quit);
addQs.addActionListener(this);
takeQuiz.addActionListener(this);
quit.addActionListener(this);
c.add(pnlButtons, BorderLayout.NORTH);
c.setVisible(true);
//The screen for when the user presses "Add questions"
c2 = getContentPane();
pnlButtons2 = new JPanel();
qType = new JComboBox();
qType.addItem("Elementary Question");
qType.addItem("Standard Question");
qType.addItem("Advanced Question");
pnlButtons2.add(qType);
}
/**
* @param args
*/
public static void main(String[] args)
{
BookQuizGUI gui = new BookQuizGUI();
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == quit)
{
System.exit(0);
}
else if(e.getSource() == addQs)
{
c2.setVisible(true);
}
}
}
Another fundamental question of mine is how to go about switching between GUI screens? This code is supposed to have like a book quiz and you can add questions. Do I just make one container not visible and another visible?
You need to pack it and set it to be visible!
frame.pack();
frame.setVisible(true);
As for answering your second question, why not just change the elements and repaint() the screen, if I understand you want it to be like a quiz with multiple questions, right?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.