Forum Stats

  • 3,875,295 Users
  • 2,266,907 Discussions
  • 7,912,141 Comments

Discussions

Question on Applets

843789
843789 Member Posts: 46,655 Green Ribbon
I just started looking at java Applets today and i was wondering how to get the event listener to work right, i'm not sure if i'm in the right forum, but i am a student in highschool learning this stuff. I have noone to teach me except my own mind. this is what i have so far, you dont have all the classes so it won't work to compile.
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;

import javax.swing.*;


public class TestFrame extends JPanel
implements ActionListener {
static TestReader a = new TestReader();
static String[] sc;
Problem p1 = new Problem(sc,0);
JLabel p;
JRadioButton bx;
char answer;
static int numRight=0;
static int numWrong=0;
static double rawScore =0;
public static void read() throws IOException
{
sc = a.readIn();
}
public TestFrame()
{
super(new BorderLayout());


JLabel l1 = new JLabel(p1.getQuestion());
JRadioButton a = new JRadioButton(p1.getA());
a.setMnemonic(KeyEvent.VK_A);
a.setActionCommand("a");
a.setSelected(true);
JRadioButton b = new JRadioButton(p1.getB());
b.setMnemonic(KeyEvent.VK_B);
b.setActionCommand("b");
b.setSelected(true);
JRadioButton c = new JRadioButton(p1.getC());
c.setMnemonic(KeyEvent.VK_C);
c.setActionCommand("c");
c.setSelected(true);
JRadioButton d = new JRadioButton(p1.getD());
c.setMnemonic(KeyEvent.VK_D);
c.setActionCommand("d");
c.setSelected(true);
JRadioButton e = new JRadioButton(p1.getE());
c.setMnemonic(KeyEvent.VK_E);
c.setActionCommand("e");
c.setSelected(true);

ButtonGroup group = new ButtonGroup();
group.add(a);
group.add(b);
group.add(c);
group.add(d);
group.add(e);

a.addActionListener(this);
b.addActionListener(this);
c.addActionListener(this);
d.addActionListener(this);
e.addActionListener(this);

JPanel radioPanel = new JPanel(new GridLayout(0, 1));
radioPanel.add(l1);
radioPanel.add(a);
radioPanel.add(b);
radioPanel.add(c);
radioPanel.add(d);
radioPanel.add(e);

add(radioPanel, BorderLayout.LINE_START);
setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
}


public void actionPerformed(ActionEvent e)
{
char answer = p1.getAnswer();
if (e.equals(answer))
numRight++;
else
numWrong++;
}

private static void createAndShowGUI()
{

JFrame frame = new JFrame("Practice Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


JComponent newContentPane = new TestFrame();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);

frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) throws IOException
{
read();
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run()
{
createAndShowGUI();
}
});
}
}

public void actionPerformed(ActionEvent e) is the problem i think. Also how do i add a label that gets the results of the score

Any help will be wonderfull. Thank you.

Comments

This discussion has been closed.