FileDocCategorySizeDatePackage
Score.javaAPI DocExample2569Tue Jan 02 14:44:18 GMT 2001None

Score

public class Score extends Object
Class to display and update a Scoreboard for a Quiz game.

Fields Summary
protected JFrame
f
JTextField
lst
The left text field
JTextField
rst
The right-hand text field
DecimalFormat
df
A formatter for all the numbers
Constructors Summary
Score()
Construct the GUI


	    
	 
		JButton b;
		f = new JFrame("Score");

		JMenuBar mb = new JMenuBar();
		JMenuItem mi;

		JMenu fm = new JMenu("File");
		fm.add(mi = new JMenuItem("Quit"));
		mi.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				System.exit(0);
			}
		});
		mb.add(fm);

		JMenu em = new JMenu("Edit");
		em.add(mi = new JMenuItem("Reset"));
		mi.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				lst.setText(df.format(0));
				rst.setText(df.format(0));
			}
		});
		mb.add(em);
		f.setJMenuBar(mb);

		Container cp = f.getContentPane();
		cp.setLayout(new FlowLayout());
		Font bigF = new Font("helvetica", Font.BOLD, 128);
		cp.add(lst = new JTextField(df.format(0)));
		lst.setFont(bigF);

		cp.add(b = new JButton("+"));
		b.addActionListener(new Adder(lst, 1));

		cp.add(rst = new JTextField(df.format(0)));
		rst.setFont(bigF);

		cp.add(b = new JButton("+"));
		b.addActionListener(new Adder(rst, 1));

		f.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				//System.out.println(e);
				switch(e.getKeyChar()) {
					case 'l": case 'L": incr(lst); break;
					case 'r": case 'R": incr(rst); break;
					// XXX "Q" should require a JOptionPane confirmation...
					case 'Q": System.exit(0); break;
				} 
			}
		});

		f.pack();
	
Methods Summary
protected voidincr(javax.swing.JTextField j)

		incr(j, 1);
	
protected voidincr(javax.swing.JTextField j, int incr)

		String t = j.getText().trim();
		int i = 0;
		try {
			i = Integer.parseInt(t);
		} catch (NumberFormatException e) {
			System.out.println("BLATZ");
		}
		j.setText(df.format(i + incr));
	
public static voidmain(java.lang.String[] args)

		new Score().setVisible(true);
	
public voidsetVisible(boolean b)
Little convenience routine, so we can be used like JFrame

		f.setVisible(b);