• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

To many Frames

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to create a quiz program but each time I move to the new question I create a brand new frame each time. Is there any way that I can destroy the previous frame without having to start all over again.
Thanks..........code below.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Quiz extends JFrame implements ItemListener,ActionListener
{
JRadioButton radio1, radio2, radio3, radio4;
ButtonGroup group;
Button b;
JTextField text;
JLabel jl;
public void showGui()
{
Container contentPane = getContentPane();
contentPane.setLayout(new GridLayout(0,1));
jl = new JLabel("(1) Who played the guitar solo on Peg?");
contentPane.add(jl);
group = new ButtonGroup();
radio1 = new JRadioButton("Dennis Dias.");
radio2 = new JRadioButton("Walter Becker.");
radio3 = new JRadioButton("Steve Gadd.");
radio4 = new JRadioButton("Jeff Skunk Baxter");
group.add(radio1);
group.add(radio2);
group.add(radio3);
group.add(radio4);
radio1.addItemListener(this);
radio2.addItemListener(this);
radio3.addItemListener(this);
radio4.addItemListener(this);
contentPane.add(radio1);
contentPane.add(radio2);
contentPane.add(radio3);
contentPane.add(radio4);
text = new JTextField(15);
contentPane.add(text);
b = new Button("Question Two.");
b.addActionListener(this);
contentPane.add(b);
setSize(350,300);
setVisible(true);
}
public static void main(String args[])
{
Quiz f = new Quiz();
f.showGui();
f.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
f.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == b) {
Quiz2 f = new Quiz2();
f.showGui2();
}
}
public void itemStateChanged(ItemEvent e)
{
if (e.getItemSelectable() == radio1) {
text.setText("Dennis Dias.");
} else if (e.getItemSelectable() == radio2) {
text.setText("Walter Becker.");
} else if (e.getItemSelectable() == radio3) {
text.setText("Steve Gadd.");
} else if (e.getItemSelectable() == radio4) {
text.setText("Jeff Skunk Baxter.");
}
}
class Quiz2 extends JFrame implements ItemListener,ActionListener
{
public void showGui2()
{
Container contentPane = getContentPane();
contentPane.setLayout(new GridLayout(0,1));
jl = new JLabel("(2) Who was the song Chain lightning written about?");
contentPane.add(jl);
group = new ButtonGroup();
radio1 = new JRadioButton("The Christians.");
radio2 = new JRadioButton("The Klu Klux Klan.");
radio3 = new JRadioButton("The Nazi Party.");
radio4 = new JRadioButton("The Mormons.");
group.add(radio1);
group.add(radio2);
group.add(radio3);
group.add(radio4);
radio1.addItemListener(this);
radio2.addItemListener(this);
radio3.addItemListener(this);
radio4.addItemListener(this);
contentPane.add(radio1);
contentPane.add(radio2);
contentPane.add(radio3);
contentPane.add(radio4);
text = new JTextField(15);
contentPane.add(text);
b = new Button("Question Three.");
b.addActionListener(this);
contentPane.add(b);
setSize(350,300);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == b) {
Quiz3 f = new Quiz3();
f.showGui3();
}
}
public void itemStateChanged(ItemEvent e)
{
if (e.getItemSelectable() == radio1) {
text.setText("The Christians.");
} else if (e.getItemSelectable() == radio2) {
text.setText("The Klu Klux Klan.");
} else if (e.getItemSelectable() == radio3) {
text.setText("The Nazi Party.");
} else if (e.getItemSelectable() == radio4) {
text.setText("The Mormons.");
}
}
}

class Quiz3 extends JFrame implements ItemListener,ActionListener
{
public void showGui3()
{
Container contentPane = getContentPane();
contentPane.setLayout(new GridLayout(0,1));
jl = new JLabel("(3) What famous guitarist played on 'Time out of mind'?");
contentPane.add(jl);
group = new ButtonGroup();
radio1 = new JRadioButton("Eric Clapton.");
radio2 = new JRadioButton("BB King.");
radio3 = new JRadioButton("George Benson.");
radio4 = new JRadioButton("Mark Knopfler.");
group.add(radio1);
group.add(radio2);
group.add(radio3);
group.add(radio4);
radio1.addItemListener(this);
radio2.addItemListener(this);
radio3.addItemListener(this);
radio4.addItemListener(this);
contentPane.add(radio1);
contentPane.add(radio2);
contentPane.add(radio3);
contentPane.add(radio4);
text = new JTextField(15);
contentPane.add(text);
b = new Button("Question Four.");
b.addActionListener(this);
contentPane.add(b);
setSize(350,300);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == b) {
Quiz4 f = new Quiz4();
f.showGui4();
}
}
public void itemStateChanged(ItemEvent e)
{
if (e.getItemSelectable() == radio1) {
text.setText("Eric Clapton.");
} else if (e.getItemSelectable() == radio2) {
text.setText("BB King.");
} else if (e.getItemSelectable() == radio3) {
text.setText("George Benson.");
} else if (e.getItemSelectable() == radio4) {
text.setText("Mark Knopfler.");
}
}
}

class Quiz4 extends JFrame implements ItemListener//ActionListener
{
public void showGui4()
{
Container contentPane = getContentPane();
contentPane.setLayout(new GridLayout(0,1));
jl = new JLabel("(4) Who played the keyboard solo on 'Black Cow'?");
contentPane.add(jl);
group = new ButtonGroup();
radio1 = new JRadioButton("Donald Fagen.");
radio2 = new JRadioButton("Herbie Hancock.");
radio3 = new JRadioButton("Victor Feldman.");
radio4 = new JRadioButton("Tino Beggs.");
group.add(radio1);
group.add(radio2);
group.add(radio3);
group.add(radio4);
radio1.addItemListener(this);
radio2.addItemListener(this);
radio3.addItemListener(this);
radio4.addItemListener(this);
contentPane.add(radio1);
contentPane.add(radio2);
contentPane.add(radio3);
contentPane.add(radio4);
text = new JTextField(15);
contentPane.add(text);
b = new Button("Question Five.");
//b.addActionListener(this);
contentPane.add(b);
setSize(350,300);
setVisible(true);
}
//public void actionPerformed(ActionEvent e) {
//if(e.getSource() == b);
//Quiz f = new Quiz();
//f.ShowGui4();
//}
public void itemStateChanged(ItemEvent e)
{
if (e.getItemSelectable() == radio1) {
text.setText("Donald Fagen.");
} else if (e.getItemSelectable() == radio2) {
text.setText("Herbie Hancock.");
} else if (e.getItemSelectable() == radio3) {
text.setText("Victor Feldman.");
} else if (e.getItemSelectable() == radio4) {
text.setText("Tino Beggs.");
}
}
}}
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Methinks you are in the wrong forum. Your friendly neighborhood bartender will move this to the Swing forum before too long.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving this to the Swing / JFC / AWT forum...
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you really have to do this with a new class/frame for each question?
If not, see if this makes sense.
 
Douglas McAuley
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Micheal, your code works perfect and makes mine look like hard work, which for me it was, neanderthal that I am, I am now looking for a way to store the answers and output a final score but to be honest I don't know where to start....any pointers?
Regards Douglas
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You already have the user's responses at int[] selection = {,,,};
Include another array int[] answers = {0,0,0,0};
(where each 0 is the actual answer to each question)
When the user is at the last question, (JButton) b2 is disabled - instead
leave enabled and change the text of the button to "Finish". You will need
additional code in actionPerformed() to check if source is b2, and then
what text is showing. If text is "Finish", call a method getScore()
In getScore(), firstly check selection[] elements are each < 4, if any at
4 it means they've made no selection - so probably an option to return and
complete or to continue (if returning, remember to add code to change b2
text back to "Next"). Now compare answers[] to selection[] and the score
is the number of matches. Output the result in e.g. JOptionPane
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic