English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Hello,

I want to create a program using JCreator, that uses radio buttons, to ask the user questions. For example: Who was the 1st president of the US?
Button 1- Washington
Button 2- Jackson
Button 3- Bush
Button 4- Johnson

Unfortunatly anything I search for online just dosen't help me. Could someone please help me and either write a code for that example or reccomend me to a place that has pretty clear and simple directions (starting from the beggining of the program).

Thank you so much in advance!

2007-03-02 10:10:00 · 2 answers · asked by Anonymous in Computers & Internet Programming & Design

2 answers

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
public class Jra extends JFrame
{
private JPanel p1;
private JLabel l1,l2;
private JButton b1;
private JRadioButton jb1,jb2,jb3,jb4;
privateButtonGroup bg1;
public static void main(String[] args)
{
Jra f= new Jra();
f.setVisible(true);

}
public Jra(){

p1=new JPanel();
p1.setLayout(new GridLayout(7,2));
l2=new JLabel("Who was the first president??");
b1=new JButton("Choose Ans");

jb1= new JRadioButton("Washingtonn");
jb2= new JRadioButton("Jackson");
jb3= new JRadioButton("Bush");
jb4= new JRadioButton("Johnson");
bg1= new ButtonGroup();
l1= new JLabel("Correct/Wrong");

bg1.add(jb1);
bg1.add(jb2);
bg1.add(jb3);
bg1.add(jb4);
p1.add(l2);
p1.add(jb1);
p1.add(jb2);
p1.add(jb3);
p1.add(jb4);
p1.add(b1);
p1.add(l1);

getContentPane().add(p1);
setDefaultCloseOperation(JFra
me.EXIT_ON_CLOSE);
setSize(200,200);
b1.addActionListener(new Choose());
}
public class Choose implements ActionListener{
public void actionPerformed(ActionEvent e){
if(jb1.isSelected()){
l1.setText("Correct Answer");
}
else{
l1.setText("Wrong Answer");
}
}
}

}

2007-03-02 19:05:19 · answer #1 · answered by Geinius 3 · 0 0

Never used JCreator so I have no clue what that provides you.

But if you want to know about Radio buttons, Suns tutorial hasnt decent material.

http://java.sun.com/docs/books/tutorial/uiswing/components/button.html

2007-03-02 23:15:16 · answer #2 · answered by rsmith985 3 · 0 0

fedest.com, questions and answers