Jump to content

how to use actionlistener?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
hayschooler

hayschooler

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
I'm working on some java code that displays a phone keypad. When keypad buttons are clicked the number is suppose to appear in a display window. The errors I'm getting are saying I can't add an action listener to my keypad. How can I add action listener to recognize when the keys are clicked on?

KeyPadPanel.java:55: addActionListener(java.awt.event.ActionListener) in javax.swing.AbstractButton cannot be applied to (KeyPadPanel)
ÏÏ§Ï b1.addActionListener(this);
ÏÏ§Ï ^


This is the code I have:

[FONT=Courier New][COLOR=#0000ff]
[FONT=Courier New][COLOR=#0000ff]1 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]import[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] java.awt.*;[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]2 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]import[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] java.awt.event.*;[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]3 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]import[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] javax.swing.*;[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]4 [/COLOR][/FONT]
[FONT=Courier New][COLOR=#0000ff]5 [/COLOR][/FONT][/COLOR][/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]6 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]public[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]class[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] KeyPadPanel [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]extends[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JPanel[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]7 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]{[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]8 [/COLOR][/FONT]
[FONT=Courier New][COLOR=#0000ff]9 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]private[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JTextField textfield;[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]10 [/COLOR][/FONT][/COLOR][/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]11 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]public[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] KeyPadPanel()[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]12 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]{[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]13 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]JPanel keys = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JPanel();[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]14 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]keys.setLayout([/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] GridLayout(4, 3));[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]15 [/COLOR][/FONT][/COLOR][/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]16 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]JButton b1 = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JButton ([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"1"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]17 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]JButton b2 = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JButton ([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"2"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]18 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]JButton b3 = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JButton ([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"3"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]19 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]JButton b4 = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JButton ([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"4"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]20 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]JButton b5 = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JButton ([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"5"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]21 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]JButton b6 = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JButton ([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"6"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]22 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]JButton b7 = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JButton ([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"7"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]23 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]JButton b8 = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JButton ([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"8"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]24 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]JButton b9 = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JButton ([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"9"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]25 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]JButton bPound = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JButton ([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"*"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]26 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]JButton b0 = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JButton ([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"0"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]27 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]JButton bStar = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JButton ([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"#"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]28 [/COLOR][/FONT][/COLOR][/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]29 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]keys.add (b1);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]30 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]keys.add (b2);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]31 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]keys.add (b3);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]32 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]keys.add (b4);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]33 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]keys.add (b5);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]34 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]keys.add (b6);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]35 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]keys.add (b7);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]36 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]keys.add (b8);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]37 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]keys.add (b9);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]38 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]keys.add (bStar);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]39 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]keys.add (b0);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]40 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]keys.add (bPound);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]41 [/COLOR][/FONT][/COLOR][/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]42 [/COLOR][/FONT][/COLOR][/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]43 [/COLOR][/FONT][/COLOR][/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]44 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]JPanel base = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JPanel();[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]45 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]base.setLayout([/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] BorderLayout());[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]46 [/COLOR][/FONT][/COLOR][/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]47 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]JButton bclear = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JButton([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"clear"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]48 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]textfield = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JTextField(10);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]49 [/COLOR][/FONT][/COLOR][/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]50 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]add(keys, BorderLayout.CENTER); [/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]51 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]add(bclear, BorderLayout.EAST);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]52 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]add(textfield, BorderLayout.NORTH);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]53 [/COLOR][/FONT]
[FONT=Courier New][COLOR=#0000ff]54 [/COLOR][/FONT][/COLOR][/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]55 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]b1.addActionListener([/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]this[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]56 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]b2.addActionListener([/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]this[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]57 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]b3.addActionListener([/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]this[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]58 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]b4.addActionListener([/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]this[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]59 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]b5.addActionListener([/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]this[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]60 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]b6.addActionListener([/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]this[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]61 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]b7.addActionListener([/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]this[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]62 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]b8.addActionListener([/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]this[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]63 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]b9.addActionListerner([/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]this[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]64 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]bPound.addActionListener([/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]this[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]65 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]b0.addActionListener([/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]this[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]66 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]bStar.addActionListner([/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]this[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]67 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]bclear.addActionListner([/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]this[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]68 [/COLOR][/FONT][/COLOR][/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]69 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]}[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]70[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]} [/FONT]
 

The driver program:

[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]
1 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]import[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] javax.swing.*;
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]2 
3 
4 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]public[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]class[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] KeyPadDemo
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]5 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]{
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]6 
7 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]public[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]static[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]void[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] main (String[] args)
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]8 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]{
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]9 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]JFrame frame = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JFrame ([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"Key Pad Demo"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]10 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]11 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]12 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]KeyPadPanel panel = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] KeyPadPanel();
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]13 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]14 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]frame.getContentPane().add(panel);
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]15 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]frame.pack();
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]16 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]frame.setVisible([/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]true[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]); 
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]17 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]}
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]18 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]19 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]}

[/FONT]


#2
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
It is because you aren't having KeyPadPanel implement the ActionListener interface. You'll also need to add a void actionPerformed(ActionEvent e) method to your KeyPadPanel object.

However, a more preferable way to do this may be to use a private scoped class within your KeyPadPanel that implements the ActionListener interface. This way you could make many separate ActionListener objects that, for example, take in an int in their constructor. This way, you can tell which button is actually being pressed!
Wow I changed my sig!

#3
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts

Quote

However, a more preferable way to do this may be to use a private scoped class within your KeyPadPanel that implements the ActionListener interface. This way you could make many separate ActionListener objects that, for example, take in an int in their constructor. This way, you can tell which button is actually being pressed!
In this case it's not to hard to get to know which button is pressed as the buttons have a nice text on them with the number, which you can get from the ActionEvent e
JButton buttonPressed = (JButton) e.getSource();
String number = buttonPressed.getText();


#4
hayschooler

hayschooler

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
Did you guys mean something like this? Sorry, I got confused.
I get this type of error setting up a privated class:

ÏKeyPadPanel.java:83: illegal start of expression
ÏÏ§Ï private class ButtonListener implements ActionListener



[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]
9 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]import[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] java.awt.*;
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]10 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]import[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] java.awt.event.*;
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]11 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]import[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] javax.swing.*;
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]12 
13 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]14 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]public[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]class[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] KeyPadPanel [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]extends[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JPanel
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]15 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]{
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]16 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]String number=[/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]""[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New];
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]17 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]18 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]public[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] KeyPadPanel()
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]19 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]{
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]20 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]//Create all the objects to be displayed.
[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]21 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]22 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]JButton b1 = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JButton ([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"1"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]23 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]JButton b2 = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JButton ([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"2"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]24 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]JButton b3 = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JButton ([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"3"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]25 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]JButton b4 = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JButton ([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"4"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]26 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]JButton b5 = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JButton ([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"5"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]27 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]JButton b6 = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JButton ([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"6"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]28 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]JButton b7 = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JButton ([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"7"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]29 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]JButton b8 = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JButton ([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"8"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]30 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]JButton b9 = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JButton ([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"9"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]31 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]JButton bPound = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JButton ([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"*"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]32 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]JButton b0 = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JButton ([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"0"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]33 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]JButton bStar = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JButton ([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"#"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]34 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]35 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]JButton bclear = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JButton([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"clear"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]36 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]JTextField textfield = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JTextField(10);
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]37 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]38 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]b1.addActionListener([/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] ButtonListener());
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]39 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]b2.addActionListener([/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] ButtonListener());
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]40 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]b3.addActionListener([/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] ButtonListener());
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]41 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]b4.addActionListener([/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] ButtonListener());
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]42 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]b5.addActionListener([/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] ButtonListener());
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]43 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]b6.addActionListener([/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] ButtonListener());
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]44 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]b7.addActionListener([/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] ButtonListener());
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]45 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]b8.addActionListener([/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] ButtonListener());
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]46 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]b9.addActionListener([/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] ButtonListener());
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]47 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]bPound.addActionListener([/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] ButtonListener());
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]48 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]b0.addActionListener([/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] ButtonListener());
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]49 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]bStar.addActionListener([/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] ButtonListener());
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]50 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]bclear.addActionListener([/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] ButtonListener());
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]51 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]52 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]//Create a keys panel to add the keys
[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]53 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]JPanel keys = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JPanel();
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]54 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]keys.setLayout([/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] GridLayout(4,3));
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]55 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]56 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]//Add individual keys to keys panel
[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]57 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]keys.add (b1);
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]58 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]keys.add (b2);
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]59 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]keys.add (b3);
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]60 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]keys.add (b4);
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]61 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]keys.add (b5);
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]62 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]keys.add (b6);
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]63 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]keys.add (b7);
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]64 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]keys.add (b8);
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]65 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]keys.add (b9);
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]66 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]keys.add (bStar);
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]67 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]keys.add (b0);
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]68 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]keys.add (bPound);
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]69 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]70 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]71 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]//Create a display panel to add keys, clear button, textfield
[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]72 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]JPanel display = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JPanel();
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]73 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]display.setLayout ([/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] BorderLayout());
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]74 
75 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]display.add(textfield, BorderLayout.NORTH); 
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]76 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]display.add(keys, BorderLayout.CENTER);
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]77 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]display.add(bclear, BorderLayout.EAST);
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]78 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]79 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]//Add entire display to KeyPadPanel 
[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]80 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]add(display);
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]81 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]82 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]//Create a private scoped class within keypanel
[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]83 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]private[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]class[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] ButtonListener [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]implements[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] ActionListener
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]84 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]{
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]85 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]86 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]public[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]void[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] actionPerformed (ActionEvent e)
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]87 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]{
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]88 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]if[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] (e.getActionEvent().equals([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"b1"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New])){
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]89 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]number=[/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"1"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] + number;
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]90 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]}[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]else[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]if[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New](e.getActionEvent().equals([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"b2"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New])){
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]91 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]number=[/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"2"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] + number;
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]92 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]}[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]else[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]if[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New](e.getActionEvent().equals([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"b3"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New])){
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]93 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]number=[/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"3"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] + number;
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]94 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]}[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]else[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]if[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New](e.getActionEvent().equals([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"b4"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New])){
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]95 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]number=[/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"4"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] + number;
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]96 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]}[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]else[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]if[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New](e.getActionEvent().equals([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"b5"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New])){
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]97 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]number=[/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"5"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] + number;
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]98 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]}[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]else[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]if[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New](e.getActionEvent().equals([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"b6"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New])){
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]99 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]number=[/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"6"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] + number;
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]100 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]}[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]else[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]if[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New](e.getActionEvent().equals([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"b7"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New])){
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]101 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]number=[/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"7"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] + number;
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]102 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]}[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]else[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]if[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New](e.getActionEvent().equals([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"b8"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New])){
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]103 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]number=[/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"8"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] + number;
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]104 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]}[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]else[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]if[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New](e.getActionEvent().equals([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"b9"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New])){
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]105 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]number=[/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"9"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] + number;
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]106 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]}[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]else[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]if[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New](e.getActionEvent().equals([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"b*"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New])){
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]107 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]number=[/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"*"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] + number;
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]108 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]}[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]else[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]if[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New](e.getActionEvent().equals([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"b0"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New])){
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]109 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]number=[/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"0"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] + number;
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]110 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]}[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]else[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]if[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New](e.getActionEvent().equals([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"b#"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New])){
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]111 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]number=[/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"#"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] + number;
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]112 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]}
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]113 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]} 
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]114 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]}
[/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]115 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]}

[/FONT]


#5
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
I am not very into inner classes but i think you just type class without a public/private word. Notice that on line 86 public void actionperformed, you forgot the space between "public" and "void".

If you decide to use the inner class actionlistener, it's indeed better to give the value of the button as parameter.

9 import java.awt.*;

10 import java.awt.event.*;

11 import javax.swing.*;

12 

13 

14 publicclass KeyPadPanel extends JPanel

15 {

16 String number="";

17 

18 public KeyPadPanel()

19 {

20 //Create all the objects to be displayed.

21 

22 JButton b1 = new JButton ("1");

23 JButton b2 = new JButton ("2");

.........

.........

 

35 JButton bclear = new JButton("clear");

36 JTextField textfield = new JTextField(10);

37 

38 b1.addActionListener(new ButtonListener("1"));

39 b2.addActionListener(new ButtonListener("2"));


........

......


57 keys.add (b1);

58 keys.add (b2);


........

.......

82 //Create a private scoped class within keypanel

83 class ButtonListener implements ActionListener

84 {

      private String value;

     //constructor

       public ButtonListener(String value){

         this.value = value

      }

 

86 publicvoid actionPerformed (ActionEvent e)

87 {

       number = value + number;

112 }


115 }

Tip: It might be wise to put all the buttons in an array(list)
Then you can easily loop trough it to add the actionlistener. Something like
for(int i=0 ; i<array.length ; i++){

    array[i].addActionListener(new ButtonListener(array[i].getText() ))

}

How did you get the code so colorful? It's so nice to read :love:

#6
hayschooler

hayschooler

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
oxano, I don't know why some of the key words are colored. It's how they appear in the IDE though. I've only used JGrasp so I don't know what the code looks like in the others.

I don't know why 'publicvoid' didn't have a space when I pasted the code. It really did have the space but it just didn't transfer over for some reason.

I haven't gotten to arrays yet. That's the next chapter. I'll try to use what you've shown me so far.

I've never seen a class declared without public or private in front of it but I tried it?

I've been trying anything to get this to work, so I'm not sure of a clearer way to do it.

I got some more errors, unless I didn't follow what your code corrrectly?

ÏKeyPadPanel.java:38: cannot find symbol
ÏϧÏsymbol : class ButtonListener
ÏϧÏlocation: class KeyPadPanel
ÏÏ§Ï b1.addActionListener(new ButtonListener("1"));
ÏÏ§Ï ^
ϼ§ÏKeyPadPanel.java:39: cannot find symbol
ÏϧÏsymbol : class ButtonListener
ÏϧÏlocation: class KeyPadPanel
ÏÏ§Ï b2.addActionListener(new ButtonListener("2"));
ÏÏ§Ï ^
ϼ§ÏKeyPadPanel.java:40: cannot find symbol
ÏϧÏsymbol : class ButtonListener
ÏϧÏlocation: class KeyPadPanel
ÏÏ§Ï b3.addActionListener(new ButtonListener("3"));
ÏÏ§Ï ^
ϼ§ÏKeyPadPanel.java:41: cannot find symbol
ÏϧÏsymbol : class ButtonListener
ÏϧÏlocation: class KeyPadPanel
ÏÏ§Ï b4.addActionListener(new ButtonListener("4"));
ÏÏ§Ï ^
ϼ§ÏKeyPadPanel.java:42: cannot find symbol
ÏϧÏsymbol : class ButtonListener
ÏϧÏlocation: class KeyPadPanel
ÏÏ§Ï b5.addActionListener(new ButtonListener("5"));
ÏÏ§Ï ^
ϼ§ÏKeyPadPanel.java:43: cannot find symbol
ÏϧÏsymbol : class ButtonListener
ÏϧÏlocation: class KeyPadPanel
ÏÏ§Ï b6.addActionListener(new ButtonListener("6"));
ÏÏ§Ï ^
ϼ§ÏKeyPadPanel.java:44: cannot find symbol
ÏϧÏsymbol : class ButtonListener
ÏϧÏlocation: class KeyPadPanel
ÏÏ§Ï b7.addActionListener(new ButtonListener("7"));
ÏÏ§Ï ^
ϼ§ÏKeyPadPanel.java:45: cannot find symbol
ÏϧÏsymbol : class ButtonListener
ÏϧÏlocation: class KeyPadPanel
ÏÏ§Ï b8.addActionListener(new ButtonListener("8"));
ÏÏ§Ï ^
ϼ§ÏKeyPadPanel.java:46: cannot find symbol
ÏϧÏsymbol : class ButtonListener
ÏϧÏlocation: class KeyPadPanel
ÏÏ§Ï b9.addActionListener(new ButtonListener("9"));
ÏÏ§Ï ^
ϼ§ÏKeyPadPanel.java:47: cannot find symbol
ÏϧÏsymbol : class ButtonListener
ÏϧÏlocation: class KeyPadPanel
ÏÏ§Ï bPound.addActionListener(new ButtonListener("#"));
ÏÏ§Ï ^
ϼ§ÏKeyPadPanel.java:48: cannot find symbol
ÏϧÏsymbol : class ButtonListener
ÏϧÏlocation: class KeyPadPanel
ÏÏ§Ï b0.addActionListener(new ButtonListener("0"));
ÏÏ§Ï ^
ϼ§ÏKeyPadPanel.java:49: cannot find symbol
ÏϧÏsymbol : class ButtonListener
ÏϧÏlocation: class KeyPadPanel
ÏÏ§Ï bStar.addActionListener(new ButtonListener("*"));
ÏÏ§Ï ^
ϼ§ÏKeyPadPanel.java:50: cannot find symbol
ÏϧÏsymbol : class ButtonListener
ÏϧÏlocation: class KeyPadPanel
ÏÏ§Ï bclear.addActionListener(new ButtonListener());
ÏÏ§Ï ^
ÏϧÏ13 errors


So, the value from the ActionListener object can be sent to the ButtonListener method and we don't need all those if statements? That would eliminate a lot of code. It's much easier to use this code if I used it correctly.

[FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]this[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New].value = value;[/FONT]




[FONT=Courier New][COLOR=#0000ff]
[FONT=Courier New][COLOR=#0000ff]9 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]import[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] java.awt.*;[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]10 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]import[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] java.awt.event.*;[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]11 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]import[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] javax.swing.*;[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]12 [/COLOR][/FONT]
[FONT=Courier New][COLOR=#0000ff]13 [/COLOR][/FONT][/COLOR][/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]14 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]public[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]class[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] KeyPadPanel [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]extends[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JPanel[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]15 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]{[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]16 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]String number=[/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]""[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New];[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]17 [/COLOR][/FONT][/COLOR][/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]18 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]public[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] KeyPadPanel()[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]19 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]{[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]20 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]//Create all the objects to be displayed.[/COLOR][/FONT]
[/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]21 [/COLOR][/FONT][/COLOR][/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]22 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]JButton b1 = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JButton ([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"1"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]23 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]JButton b2 = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JButton ([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"2"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]24 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]JButton b3 = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JButton ([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"3"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]25 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]JButton b4 = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JButton ([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"4"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]26 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]JButton b5 = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JButton ([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"5"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]27 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]JButton b6 = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JButton ([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"6"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]28 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]JButton b7 = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JButton ([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"7"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]29 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]JButton b8 = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JButton ([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"8"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]30 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]JButton b9 = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JButton ([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"9"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]31 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]JButton bPound = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JButton ([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"*"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]32 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]JButton b0 = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JButton ([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"0"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]33 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]JButton bStar = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JButton ([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"#"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]34 [/COLOR][/FONT][/COLOR][/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]35 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]JButton bclear = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JButton([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"clear"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]36 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]JTextField textfield = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JTextField(10);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]37 [/COLOR][/FONT][/COLOR][/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]38 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]b1.addActionListener([/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] ButtonListener([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"1"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]));[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]39 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]b2.addActionListener([/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] ButtonListener([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"2"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]));[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]40 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]b3.addActionListener([/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] ButtonListener([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"3"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]));[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]41 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]b4.addActionListener([/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] ButtonListener([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"4"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]));[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]42 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]b5.addActionListener([/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] ButtonListener([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"5"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]));[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]43 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]b6.addActionListener([/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] ButtonListener([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"6"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]));[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]44 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]b7.addActionListener([/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] ButtonListener([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"7"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]));[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]45 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]b8.addActionListener([/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] ButtonListener([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"8"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]));[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]46 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]b9.addActionListener([/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] ButtonListener([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"9"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]));[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]47 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]bPound.addActionListener([/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] ButtonListener([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"#"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]));[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]48 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]b0.addActionListener([/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] ButtonListener([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"0"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]));[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]49 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]bStar.addActionListener([/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] ButtonListener([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"*"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]));[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]50 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]bclear.addActionListener([/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] ButtonListener());[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]51 [/COLOR][/FONT][/COLOR][/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]52 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]//Create a keys panel to add the keys[/COLOR][/FONT]
[/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]53 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]JPanel keys = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JPanel();[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]54 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]keys.setLayout([/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] GridLayout(4,3));[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]55 [/COLOR][/FONT][/COLOR][/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]56 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]//Add individual keys to keys panel[/COLOR][/FONT]
[/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]57 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]keys.add (b1);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]58 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]keys.add (b2);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]59 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]keys.add (b3);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]60 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]keys.add (b4);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]61 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]keys.add (b5);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]62 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]keys.add (b6);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]63 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]keys.add (b7);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]64 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]keys.add (b8);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]65 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]keys.add (b9);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]66 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]keys.add (bStar);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]67 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]keys.add (b0);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]68 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]keys.add (bPound);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]69 [/COLOR][/FONT][/COLOR][/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]70 [/COLOR][/FONT][/COLOR][/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]71 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]//Create a display panel to add keys, clear button, textfield[/COLOR][/FONT]
[/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]72 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]JPanel display = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] JPanel();[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]73 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]display.setLayout ([/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] BorderLayout());[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]74 [/COLOR][/FONT]
[FONT=Courier New][COLOR=#0000ff]75 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]display.add(textfield, BorderLayout.NORTH); [/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]76 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]display.add(keys, BorderLayout.CENTER);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]77 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]display.add(bclear, BorderLayout.EAST);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]78 [/COLOR][/FONT][/COLOR][/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]79 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]//Add entire display to KeyPadPanel [/COLOR][/FONT]
[/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]80 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]add(display);[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]81 [/COLOR][/FONT][/COLOR][/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]82 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]//Create a private scoped class within keypanel[/COLOR][/FONT]
[/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]83 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]class[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] ButtonListener [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]implements[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] ActionListener[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]84 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]{ [/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]85 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]private[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] String value;[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]86 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]//constructor[/COLOR][/FONT]
[/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]87 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]public[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] ButtonListener(String value){[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]88 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]this[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New].value = value;[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]89 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]}[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]90 [/COLOR][/FONT]
[FONT=Courier New][COLOR=#0000ff]91 [/COLOR][/FONT][/COLOR][/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]92 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]public[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]void[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] actionPerformed (ActionEvent e)[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]93 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]{[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]94 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]number = value + number;[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]95 [/COLOR][/FONT]
[FONT=Courier New][COLOR=#0000ff]96 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]// if (e.getActionEvent().equals("b1")){[/COLOR][/FONT]
[/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]97 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]// number="1" + number;[/COLOR][/FONT]
[/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]98 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]// }else if(e.getActionEvent().equals("b2")){[/COLOR][/FONT]
[/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]99 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]// number="2" + number;[/COLOR][/FONT]
[/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]100 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]// }else if(e.getActionEvent().equals("b3")){[/COLOR][/FONT]
[/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]101 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]// number="3" + number;[/COLOR][/FONT]
[/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]102 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]// }else if(e.getActionEvent().equals("b4")){[/COLOR][/FONT]
[/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]103 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]// number="4" + number;[/COLOR][/FONT]
[/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]104 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]// }else if(e.getActionEvent().equals("b5")){[/COLOR][/FONT]
[/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]105 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]// number="5" + number;[/COLOR][/FONT]
[/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]106 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]// }else if(e.getActionEvent().equals("b6")){[/COLOR][/FONT]
[/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]107 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]// number="6" + number;[/COLOR][/FONT]
[/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]108 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]// }else if(e.getActionEvent().equals("b7")){[/COLOR][/FONT]
[/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]109 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]// number="7" + number;[/COLOR][/FONT]
[/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]110 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]// }else if(e.getActionEvent().equals("b8")){[/COLOR][/FONT]
[/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]111 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]// number="8" + number;[/COLOR][/FONT]
[/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]112 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]// }else if(e.getActionEvent().equals("b9")){[/COLOR][/FONT]
[/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]113 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]// number="9" + number;[/COLOR][/FONT]
[/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]114 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]// }else if(e.getActionEvent().equals("b*")){[/COLOR][/FONT]
[/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]115 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]// number="*" + number;[/COLOR][/FONT]
[/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]116 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]// }else if(e.getActionEvent().equals("b0")){[/COLOR][/FONT]
[/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]117 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]// number="0" + number;[/COLOR][/FONT]
[/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]118 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]// }else if(e.getActionEvent().equals("b#")){[/COLOR][/FONT]
[/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]119 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]// number="#" + number;[/COLOR][/FONT]
[/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]120 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]}[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]121 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]} [/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]122 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]}[/FONT]
[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]123 [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]} [/FONT]


#7
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
Okay, looked a bit up on inner classes now ;)
Here's what i came up with:
import java.awt.event.ActionListener;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class KeyPadPanel extends JPanel {
    String number = "";
    JTextField textfield;

    public KeyPadPanel() {
        //Create all the objects to be displayed.

        JButton b1 = new JButton("1");
        JButton b2 = new JButton("2");
        JButton b3 = new JButton("3");
        JButton b4 = new JButton("4");
        JButton b5 = new JButton("5");
        JButton b6 = new JButton("6");
        JButton b7 = new JButton("7");
        JButton b8 = new JButton("8");
        JButton b9 = new JButton("9");
        JButton bPound = new JButton("*");
        JButton b0 = new JButton("0");
        JButton bStar = new JButton("#");

        JButton bclear = new JButton("clear");
        textfield = new JTextField(10);
        textfield.setHorizontalAlignment(JTextField.RIGHT);

        b1.addActionListener(new ButtonListener("1"));
        b2.addActionListener(new ButtonListener("2"));
        b3.addActionListener(new ButtonListener("3"));
        b4.addActionListener(new ButtonListener("4"));
        b5.addActionListener(new ButtonListener("5"));
        b6.addActionListener(new ButtonListener("6"));
        b7.addActionListener(new ButtonListener("7"));
        b8.addActionListener(new ButtonListener("8"));
        b9.addActionListener(new ButtonListener("9"));
        bPound.addActionListener(new ButtonListener("*"));
        b0.addActionListener(new ButtonListener("0"));
        bStar.addActionListener(new ButtonListener("#"));
        bclear.addActionListener(new ButtonListener("clear"));

//Create a keys panel to add the keys
        JPanel keys = new JPanel();
        keys.setLayout(new GridLayout(4, 3));

//Add individual keys to keys panel
        keys.add(b1);
        keys.add(b2);
        keys.add(b3);
        keys.add(b4);
        keys.add(b5);
        keys.add(b6);
        keys.add(b7);
        keys.add(b8);
        keys.add(b9);
        keys.add(bStar);
        keys.add(b0);
        keys.add(bPound);


//Create a display panel to add keys, clear button, textfield
        JPanel display = new JPanel();
        display.setLayout(new BorderLayout());

        display.add(textfield, BorderLayout.NORTH);
        display.add(keys, BorderLayout.CENTER);
        display.add(bclear, BorderLayout.EAST);

//Add entire display to KeyPadPanel
        add(display);

//Create a private scoped class within keypanel
    }


    private class ButtonListener implements ActionListener {
        private String value;

        //constructor
        public ButtonListener(String value) {
            this.value = value;
        }


        public void actionPerformed(ActionEvent e) {
            if (value.equals("clear"))
                textfield.setText("");
            else
                textfield.setText(value + textfield.getText());
        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.add(new KeyPadPanel());
        frame.pack();
        frame.setVisible(true);
    }
}

I find it a bit weird to add numbers to the left tho.

#8
hayschooler

hayschooler

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
oxano, Thank you. This was probably an easy problem but it's been the most difficult one that I've seen yet, which isn't saying much. I wouldn't have gotten this without the help. I see I forgot some simple things like declaring my textfield as a variable and sending a parameter for the clear button.

I'm not sure what you meant about adding numbers on the left but it's great to see this thing working.

#9
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
Well, in the current version if you press "1" followed by "2", it will display "21" on the screen, while i find it more logical if it would be "12"