הנה משהו
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import java.util.EventListener;
import java.util.Random;
public class Trivia extends JFrame implements ActionListener
{
private int i=0, grade=0;
private String str;
private int delay=10000;
private Timer timer= new Timer (delay, this);
private Questions q= new Questions();
private Container c = getContentPane();
private JTextField t_question;
private JTextField t_timer;
private ButtonGroup group= new ButtonGroup();
private JRadioButton choise1, choise2, choise3, choise4;
private JButton cButt= new JButton ("Play a new Game");
private int x_op1, x_op2, x_op3, x_op4;
private Random rand_op1= new Random();
private Random rand_op2= new Random();
private Random rand_op3= new Random();
private Random rand_op4= new Random();
public Trivia() throws FileNotFoundException
{
super("Trivia");
super.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
if (i<18)
{
if (grade>100)
{
grade=100;
JOptionPane.showMessageDialog(null,"You stoped the game, your grade is: "+grade);
}
else JOptionPane.showMessageDialog(null,"You stoped the game, your grade is: "+grade);
}
}
}
);
c.setLayout( new GridLayout(7,1));
t_question = new JTextField("Trivia Game");
c.add( t_question );
choise1=new JRadioButton("");
choise1.addActionListener(this);
c.add(choise1);
group.add(choise1);
choise2=new JRadioButton("");
choise2.addActionListener(this);
c.add(choise2);
group.add(choise2);
choise3=new JRadioButton("");
choise3.addActionListener(this);
c.add(choise3);
group.add(choise3);
choise4=new JRadioButton("");
choise4.addActionListener(this);
c.add(choise4);
group.add(choise4);
t_timer = new JTextField(" You have "+delay/1000+" seconds to answer the question");
c.add( t_timer );
add(cButt);
cButt.addActionListener(this);
ShowQuestions();
setSize(550,300);
setVisible(true);
}
public void ShowQuestions() throws FileNotFoundException
{
str=q.q.getQ();
t_question.setText(str);
x_op1=rand_op1.nextInt(4);
x_op2=rand_op2.nextInt(4);
x_op3=rand_op3.nextInt(4);
x_op4=rand_op4.nextInt(4);
str=q.q.getOp(x_op1);
choise1.setText(str);
while (x_op2==x_op1)
{
x_op2=rand_op2.nextInt(4);
}
str=q.q.getOp(x_op2);
choise2.setText(str);
while (x_op3==x_op1 || x_op3==x_op2)
{
x_op3=rand_op3.nextInt(4);
}
str=q.q.getOp(x_op3);
choise3.setText(str);
while (x_op4==x_op1 || x_op4==x_op2 || x_op4==x_op3)
{
x_op4=rand_op4.nextInt(4);
}
str=q.q.getOp(x_op4);
choise4.setText(str);
timer.start();
} // ShowQuestions
public void actionPerformed(ActionEvent e)
{
timer.stop();
if (e.getActionCommand()=="Play a new Game")
{
i=0;
try {
ShowQuestions();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
System.err.println("Error: " + e1);
}
}
if (e.getSource()==timer)
{
JOptionPane.showMessageDialog(null,"Time's up!");
if (grade>=5)
grade=grade-5;
else grade=0;
}
else
{
if (e.getActionCommand()==q.q.getOp(1))
grade=grade+10;
else if (grade>=5)
grade=grade-5;
else grade=0;
}
if (i<18 && e.getActionCommand()!="Play a new Game")
{
i++;
try
{
ShowQuestions();
} catch (FileNotFoundException e1)
{
// TODO Auto-generated catch block
System.err.println("Error: " + e1);
}
}
else
{
if (grade>100)
{
grade=100;
JOptionPane.showMessageDialog(null,"Your grade is: "+grade);
}
else JOptionPane.showMessageDialog(null,"Your grade is: "+grade);
}
} //actionPerformed
public static void main(String[] args) throws FileNotFoundException
{
Trivia T= new Trivia();
T.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
כל מקום שיש JField לדוגמא אני רוצה שיהיה כתוב בעברית. אם זה נמשך מקובץ כמו במקרה הזה הוא "מיישר" לשמאל.