import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
public class VMachine extends Applet implements ActionListener {
public static String firstPush, secondPush;
public static GridBagLayout gbl;
public static TextField textF;
public VMachine(){
gbl = new GridBagLayout();
this.setLayout(gbl);
//sets the constraints for all 12 buttons and text field then creates and adds the button
GridBagConstraints text = new GridBagConstraints();
text.gridx = 0;
text.gridy = 0;
text.gridwidth = 3;
text.gridheight = 1;
text.fill = GridBagConstraints.HORIZONTAL;
TextField textF = new TextField();
gbl.setConstraints(textF, text);
this.add(textF);
GridBagConstraints aC = new GridBagConstraints();
aC.gridx = 0;
aC.gridy = 1;
aC.gridwidth = 1;
aC.gridheight = 1;
aC.fill = GridBagConstraints.BOTH;
aC.insets = new Insets(3, 3, 3, 3);
//create buttons
Button a = new Button("A");
gbl.setConstraints(a, aC);
a.addActionListener(this);
this.add(a);
GridBagConstraints dC = new GridBagConstraints();
dC.gridx = 0;
dC.gridy = 2;
dC.gridwidth = 1;
dC.gridheight = 1;
dC.fill = GridBagConstraints.BOTH;
dC.insets = new Insets(3, 3, 3, 3);
Button d = new Button("D");
gbl.setConstraints(d, dC);
d.addActionListener(this);
this.add(d);
GridBagConstraints oneC = new GridBagConstraints();
oneC.gridx = 0;
oneC.gridy = 3;
oneC.gridwidth = 1;
oneC.gridheight = 1;
oneC.fill = GridBagConstraints.BOTH;
oneC.insets = new Insets(3, 3, 3, 3);
Button one = new Button("1");
gbl.setConstraints(one, oneC);
one.addActionListener(this);
this.add(one);
GridBagConstraints fourC = new GridBagConstraints();
fourC.gridx = 0;
fourC.gridy = 4;
fourC.gridwidth = 1;
fourC.gridheight = 1;
fourC.fill = GridBagConstraints.BOTH;
fourC.insets = new Insets(3, 3, 3, 3);
Button four = new Button("4");
gbl.setConstraints(four, fourC);
four.addActionListener(this);
this.add(four);
GridBagConstraints bC = new GridBagConstraints();
bC.gridx = 1;
bC.gridy = 1;
bC.gridwidth = 1;
bC.gridheight = 1;
bC.fill = GridBagConstraints.BOTH;
bC.insets = new Insets(3, 3, 3, 3);
Button b = new Button("B");
gbl.setConstraints(b, bC);
b.addActionListener(this);
this.add(B)/>;
GridBagConstraints eC = new GridBagConstraints();
eC.gridx = 1;
eC.gridy = 2;
eC.gridwidth = 1;
eC.gridheight = 1;
eC.fill = GridBagConstraints.BOTH;
eC.insets = new Insets(3, 3, 3, 3);
Button e = new Button("E");
gbl.setConstraints(e, eC);
e.addActionListener(this);
this.add(e);
GridBagConstraints twoC = new GridBagConstraints();
twoC.gridx = 1;
twoC.gridy = 3;
twoC.gridwidth = 1;
twoC.gridheight = 1;
twoC.fill = GridBagConstraints.BOTH;
twoC.insets = new Insets(3, 3, 3, 3);
Button two = new Button("2");
gbl.setConstraints(two, twoC);
one.addActionListener(this);
this.add(two);
GridBagConstraints fiveC = new GridBagConstraints();
fiveC.gridx = 1;
fiveC.gridy = 4;
fiveC.gridwidth = 1;
fiveC.gridheight = 1;
fiveC.fill = GridBagConstraints.BOTH;
fiveC.insets = new Insets(3, 3, 3, 3);
Button five = new Button("5");
gbl.setConstraints(five, fiveC);
five.addActionListener(this);
this.add(five);
GridBagConstraints cC = new GridBagConstraints();
cC.gridx = 2;
cC.gridy = 1;
cC.gridwidth = 1;
cC.gridheight = 1;
cC.fill = GridBagConstraints.BOTH;
cC.insets = new Insets(3, 3, 3, 3);
Button c = new Button("C");
gbl.setConstraints(c, cC);
c.addActionListener(this);
this.add(c);
GridBagConstraints fC = new GridBagConstraints();
fC.gridx = 2;
fC.gridy = 2;
fC.gridwidth = 1;
fC.gridheight = 1;
fC.fill = GridBagConstraints.BOTH;
fC.insets = new Insets(3, 3, 3, 3);
Button f = new Button("F");
gbl.setConstraints(f, fC);
f.addActionListener(this);
this.add(f);
GridBagConstraints threeC = new GridBagConstraints();
threeC.gridx = 2;
threeC.gridy = 3;
threeC.gridwidth = 1;
threeC.gridheight = 1;
threeC.fill = GridBagConstraints.BOTH;
threeC.insets = new Insets(3, 3, 3, 3);
Button three = new Button("3");
gbl.setConstraints(three, threeC);
three.addActionListener(this);
this.add(three);
GridBagConstraints sixC = new GridBagConstraints();
sixC.gridx = 2;
sixC.gridy = 4;
sixC.gridwidth = 1;
sixC.gridheight = 1;
sixC.fill = GridBagConstraints.BOTH;
sixC.insets = new Insets(3, 3, 3, 3);
Button six = new Button("6");
gbl.setConstraints(six, sixC);
six.addActionListener(this);
this.add(six);
}
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if ( (cmd == "A"|| cmd == "B"|| cmd == "C"|| cmd == "D"|| cmd == "E"|| cmd == "F") &&
textF.getText() == null){
textF.setText(cmd);
}
if ( (cmd == "1"|| cmd == "2"|| cmd == "3"|| cmd == "4"|| cmd == "5"|| cmd == "6")&&
textF.getText() != null){
textF.setText(textF.getText() + cmd);
}
}
}
ActionPreformed issues
Page 1 of 19 Replies - 1161 Views - Last Post: 30 March 2010 - 02:17 PM
#1
ActionPreformed issues
Posted 29 March 2010 - 02:32 PM
Hey guys, I don't need much of your time, but I'm wondering why my Action Preformed method isn't setting the text in the textField based on the button that's pushed. What am I overlooking? I went into the TextField API, but i can't see what is in disagreement with my code. Skip striaght to the bottom because the rest of the code is just creating the buttons and textField and aligning them:
Replies To: ActionPreformed issues
#2
Re: ActionPreformed issues
Posted 29 March 2010 - 03:06 PM
to check equality of String Objects you have to use the equals method of the String class (and NOT the "==" operator) as:
if(cmd.equals("C"))
#3
Re: ActionPreformed issues
Posted 29 March 2010 - 03:23 PM
ok, I replaced the Action Preformed method (Just the A for now until it works so it looks like
but it still reads as a null pointer error
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if( cmd.equals("A")){
textF.setText("A ");
}
}
but it still reads as a null pointer error
#4
Re: ActionPreformed issues
Posted 29 March 2010 - 03:34 PM
as for the reason for a nullPointerException you get, here it is:
you declare a TextFiels Object as a global variable textF :
However, in the constructor, you declare another textf Object, and initialize it:
so you create a new local variable named textF, which exists ONLY in the constructor.
that's mean, the global TextField textF is still null.
you have to update the initialization code in your constructor to look like:
you declare a TextFiels Object as a global variable textF :
public static TextField textF;
However, in the constructor, you declare another textf Object, and initialize it:
TextField textF = new TextField();
so you create a new local variable named textF, which exists ONLY in the constructor.
that's mean, the global TextField textF is still null.
you have to update the initialization code in your constructor to look like:
... textF = new TextField();
#5
Re: ActionPreformed issues
Posted 29 March 2010 - 05:36 PM
also you can just make textF.setText(e.getActionCommand()+ " "); and be done with it instead of using a if statement. (only useful if you make a unique actionlistener for that button.)
#6
Re: ActionPreformed issues
Posted 29 March 2010 - 06:53 PM
I appreciate it guys, I overlook that second initialization. Much thanks
#7
Re: ActionPreformed issues
Posted 30 March 2010 - 09:47 AM
Sorry Guys,
one more problem with the same flavor. VMachine works perfectly but then I created a VProgram to create an instance of VMachine called gui When the VProgram is run, the buttons all come up in the spot where they are supposed to, but the same exact pushes of the buttons don't work in VProgram like they do in VMachine. I get a nullPointerError which makes no sense to me because TextField textF works great in VMachine. Code:
VMachine:
VProgram:
one more problem with the same flavor. VMachine works perfectly but then I created a VProgram to create an instance of VMachine called gui When the VProgram is run, the buttons all come up in the spot where they are supposed to, but the same exact pushes of the buttons don't work in VProgram like they do in VMachine. I get a nullPointerError which makes no sense to me because TextField textF works great in VMachine. Code:
VMachine:
import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class VMachine extends Applet implements ActionListener {
public static int counter = 0;
public static GridBagLayout gbl;
public Button a, b, c, d, e, f, one, two, three, four, five, six, select;
public TextField textF;
public VMachine(){
gbl = new GridBagLayout();
this.setLayout(gbl);
//sets the constraints for all 12 buttons and text field then creates and adds the button
GridBagConstraints text = new GridBagConstraints();
text.gridx = 0;
text.gridy = 0;
text.gridwidth = 3;
text.gridheight = 1;
text.fill = GridBagConstraints.HORIZONTAL;
textF = new TextField();
textF.setEditable(false);
textF.setText("");
gbl.setConstraints(textF, text);
this.add(textF);
GridBagConstraints aC = new GridBagConstraints();
aC.gridx = 0;
aC.gridy = 1;
aC.gridwidth = 1;
aC.gridheight = 1;
aC.fill = GridBagConstraints.BOTH;
aC.insets = new Insets(3, 3, 3, 3);
//create buttons
a = new Button("A");
gbl.setConstraints(a, aC);
a.addActionListener(this);
add(a);
GridBagConstraints dC = new GridBagConstraints();
dC.gridx = 0;
dC.gridy = 2;
dC.gridwidth = 1;
dC.gridheight = 1;
dC.fill = GridBagConstraints.BOTH;
dC.insets = new Insets(3, 3, 3, 3);
d = new Button("D");
gbl.setConstraints(d, dC);
d.addActionListener(this);
this.add(d);
GridBagConstraints oneC = new GridBagConstraints();
oneC.gridx = 0;
oneC.gridy = 3;
oneC.gridwidth = 1;
oneC.gridheight = 1;
oneC.fill = GridBagConstraints.BOTH;
oneC.insets = new Insets(3, 3, 3, 3);
one = new Button("1");
gbl.setConstraints(one, oneC);
one.addActionListener(this);
this.add(one);
GridBagConstraints fourC = new GridBagConstraints();
fourC.gridx = 0;
fourC.gridy = 4;
fourC.gridwidth = 1;
fourC.gridheight = 1;
fourC.fill = GridBagConstraints.BOTH;
fourC.insets = new Insets(3, 3, 3, 3);
four = new Button("4");
gbl.setConstraints(four, fourC);
four.addActionListener(this);
this.add(four);
GridBagConstraints bC = new GridBagConstraints();
bC.gridx = 1;
bC.gridy = 1;
bC.gridwidth = 1;
bC.gridheight = 1;
bC.fill = GridBagConstraints.BOTH;
bC.insets = new Insets(3, 3, 3, 3);
b = new Button("B");
gbl.setConstraints(b, bC);
b.addActionListener(this);
this.add(B)/>;
GridBagConstraints eC = new GridBagConstraints();
eC.gridx = 1;
eC.gridy = 2;
eC.gridwidth = 1;
eC.gridheight = 1;
eC.fill = GridBagConstraints.BOTH;
eC.insets = new Insets(3, 3, 3, 3);
e = new Button("E");
gbl.setConstraints(e, eC);
e.addActionListener(this);
this.add(e);
GridBagConstraints twoC = new GridBagConstraints();
twoC.gridx = 1;
twoC.gridy = 3;
twoC.gridwidth = 1;
twoC.gridheight = 1;
twoC.fill = GridBagConstraints.BOTH;
twoC.insets = new Insets(3, 3, 3, 3);
two = new Button("2");
gbl.setConstraints(two, twoC);
two.addActionListener(this);
this.add(two);
GridBagConstraints fiveC = new GridBagConstraints();
fiveC.gridx = 1;
fiveC.gridy = 4;
fiveC.gridwidth = 1;
fiveC.gridheight = 1;
fiveC.fill = GridBagConstraints.BOTH;
fiveC.insets = new Insets(3, 3, 3, 3);
five = new Button("5");
gbl.setConstraints(five, fiveC);
five.addActionListener(this);
this.add(five);
GridBagConstraints cC = new GridBagConstraints();
cC.gridx = 2;
cC.gridy = 1;
cC.gridwidth = 1;
cC.gridheight = 1;
cC.fill = GridBagConstraints.BOTH;
cC.insets = new Insets(3, 3, 3, 3);
c = new Button("C");
gbl.setConstraints(c, cC);
c.addActionListener(this);
this.add(c);
GridBagConstraints fC = new GridBagConstraints();
fC.gridx = 2;
fC.gridy = 2;
fC.gridwidth = 1;
fC.gridheight = 1;
fC.fill = GridBagConstraints.BOTH;
fC.insets = new Insets(3, 3, 3, 3);
f = new Button("F");
gbl.setConstraints(f, fC);
f.addActionListener(this);
this.add(f);
GridBagConstraints threeC = new GridBagConstraints();
threeC.gridx = 2;
threeC.gridy = 3;
threeC.gridwidth = 1;
threeC.gridheight = 1;
threeC.fill = GridBagConstraints.BOTH;
threeC.insets = new Insets(3, 3, 3, 3);
three = new Button("3");
gbl.setConstraints(three, threeC);
three.addActionListener(this);
this.add(three);
GridBagConstraints sixC = new GridBagConstraints();
sixC.gridx = 2;
sixC.gridy = 4;
sixC.gridwidth = 1;
sixC.gridheight = 1;
sixC.fill = GridBagConstraints.BOTH;
sixC.insets = new Insets(3, 3, 3, 3);
six = new Button("6");
gbl.setConstraints(six, sixC);
six.addActionListener(this);
this.add(six);
GridBagConstraints selectC = new GridBagConstraints();
selectC.gridx = 0;
selectC.gridy = 5;
selectC.gridwidth = 3;
selectC.gridheight = 1;
selectC.fill = GridBagConstraints.BOTH;
selectC.insets = new Insets(3, 3, 3, 3);
select = new Button("Select");
gbl.setConstraints(select, selectC);
select.addActionListener(this);
this.add(select);
}
public void actionPerformed(ActionEvent evt) {
String cmd = evt.getActionCommand();
if(cmd.equals("Select")){
if((textF.getText().charAt(0)=='A'||textF.getText().charAt(0)=='B'||
textF.getText().charAt(0)=='C'||textF.getText().charAt(0)=='D'||
textF.getText().charAt(0)=='E'||textF.getText().charAt(0)=='F')&&
(textF.getText().charAt(1)=='1'||textF.getText().charAt(1)=='2'||
textF.getText().charAt(1)=='3'||textF.getText().charAt(1)=='4'||
textF.getText().charAt(1)=='5'||textF.getText().charAt(1)=='6')){
getAppletContext().showStatus( "selected. Please insert $1.00");
}
else{
getAppletContext().showStatus("Incorrect entry. One letter then one number.");
}
textF.setText("");
}
if(evt.getSource() != select){
textF.setText(textF.getText() + cmd);
}
}
public void paint(Graphics g){
super.paint(g);
g.setColor( Color.GRAY);
g.fillRect(0,0, 100, 400);
g.setColor( Color.BLACK);
g.drawRect(0, 0, 100, 400);
g.fillRect(45, 10, 10, 60);
}
}
VProgram:
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
public class VProgram extends Applet{
private int width, height;
private VMachine gui;
public VProgram(){
setLayout(new BorderLayout());
gui = new VMachine();
}
public void init(){
width = 300;
height = 300;
setSize(width, height);
add(gui, "East");
}
public void paint(Graphics g){
super.paint(g);
g.setColor( Color.LIGHT_GRAY);
g.fillRect(0, 0, width, height);
}
}
#8
Re: ActionPreformed issues
Posted 30 March 2010 - 11:25 AM
It is not the TextField that causes the NullPointerException, but the call to getAppletContext in the VMachine applet.
as it is an "inner" applet inside VProgram.
as VMachine is no longer the active applet calling to getAppletContext from this class will throw that exception.
you will have to pass an Instance of VProgram to the VMachine.
and use the VProgram's appletContext inside VMachine.
I have updated your code for this changes.
i have commented the changes.
(you will have to seperate again the two classes)
as it is an "inner" applet inside VProgram.
as VMachine is no longer the active applet calling to getAppletContext from this class will throw that exception.
you will have to pass an Instance of VProgram to the VMachine.
and use the VProgram's appletContext inside VMachine.
I have updated your code for this changes.
i have commented the changes.
(you will have to seperate again the two classes)
import java.applet.AppletContext;
import java.awt.BorderLayout;
import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class VProgram extends Applet{
private int width, height;
private VMachine gui;
public VProgram(){
setLayout(new BorderLayout());
gui = new VMachine(this);
}
public void init(){
width = 300;
height = 300;
setSize(width, height);
add(gui, "East");
}
public void paint(Graphics g){
super.paint(g);
g.setColor( Color.LIGHT_GRAY);
g.fillRect(0, 0, width, height);
}
}
class VMachine extends Applet implements ActionListener {
public static int counter = 0;
public static GridBagLayout gbl;
public Button a, b, c, d, e, f, one, two, three, four, five, six, select;
public TextField textF;
//a VMachine constructor now gets an Object of VProgram
VProgram master;
public VMachine(VProgram master){
gbl = new GridBagLayout();
this.setLayout(gbl);
//getting VProgram's Object
this.master = master;
//sets the constraints for all 12 buttons and text field then creates and adds the button
GridBagConstraints text = new GridBagConstraints();
text.gridx = 0;
text.gridy = 0;
text.gridwidth = 3;
text.gridheight = 1;
text.fill = GridBagConstraints.HORIZONTAL;
textF = new TextField();
textF.setEditable(false);
textF.setText("");
gbl.setConstraints(textF, text);
this.add(textF);
GridBagConstraints aC = new GridBagConstraints();
aC.gridx = 0;
aC.gridy = 1;
aC.gridwidth = 1;
aC.gridheight = 1;
aC.fill = GridBagConstraints.BOTH;
aC.insets = new Insets(3, 3, 3, 3);
//create buttons
a = new Button("A");
gbl.setConstraints(a, aC);
a.addActionListener(this);
add(a);
GridBagConstraints dC = new GridBagConstraints();
dC.gridx = 0;
dC.gridy = 2;
dC.gridwidth = 1;
dC.gridheight = 1;
dC.fill = GridBagConstraints.BOTH;
dC.insets = new Insets(3, 3, 3, 3);
d = new Button("D");
gbl.setConstraints(d, dC);
d.addActionListener(this);
this.add(d);
GridBagConstraints oneC = new GridBagConstraints();
oneC.gridx = 0;
oneC.gridy = 3;
oneC.gridwidth = 1;
oneC.gridheight = 1;
oneC.fill = GridBagConstraints.BOTH;
oneC.insets = new Insets(3, 3, 3, 3);
one = new Button("1");
gbl.setConstraints(one, oneC);
one.addActionListener(this);
this.add(one);
GridBagConstraints fourC = new GridBagConstraints();
fourC.gridx = 0;
fourC.gridy = 4;
fourC.gridwidth = 1;
fourC.gridheight = 1;
fourC.fill = GridBagConstraints.BOTH;
fourC.insets = new Insets(3, 3, 3, 3);
four = new Button("4");
gbl.setConstraints(four, fourC);
four.addActionListener(this);
this.add(four);
GridBagConstraints bC = new GridBagConstraints();
bC.gridx = 1;
bC.gridy = 1;
bC.gridwidth = 1;
bC.gridheight = 1;
bC.fill = GridBagConstraints.BOTH;
bC.insets = new Insets(3, 3, 3, 3);
b = new Button("B");
gbl.setConstraints(b, bC);
b.addActionListener(this);
this.add(B)/>;
GridBagConstraints eC = new GridBagConstraints();
eC.gridx = 1;
eC.gridy = 2;
eC.gridwidth = 1;
eC.gridheight = 1;
eC.fill = GridBagConstraints.BOTH;
eC.insets = new Insets(3, 3, 3, 3);
e = new Button("E");
gbl.setConstraints(e, eC);
e.addActionListener(this);
this.add(e);
GridBagConstraints twoC = new GridBagConstraints();
twoC.gridx = 1;
twoC.gridy = 3;
twoC.gridwidth = 1;
twoC.gridheight = 1;
twoC.fill = GridBagConstraints.BOTH;
twoC.insets = new Insets(3, 3, 3, 3);
two = new Button("2");
gbl.setConstraints(two, twoC);
two.addActionListener(this);
this.add(two);
GridBagConstraints fiveC = new GridBagConstraints();
fiveC.gridx = 1;
fiveC.gridy = 4;
fiveC.gridwidth = 1;
fiveC.gridheight = 1;
fiveC.fill = GridBagConstraints.BOTH;
fiveC.insets = new Insets(3, 3, 3, 3);
five = new Button("5");
gbl.setConstraints(five, fiveC);
five.addActionListener(this);
this.add(five);
GridBagConstraints cC = new GridBagConstraints();
cC.gridx = 2;
cC.gridy = 1;
cC.gridwidth = 1;
cC.gridheight = 1;
cC.fill = GridBagConstraints.BOTH;
cC.insets = new Insets(3, 3, 3, 3);
c = new Button("C");
gbl.setConstraints(c, cC);
c.addActionListener(this);
this.add(c);
GridBagConstraints fC = new GridBagConstraints();
fC.gridx = 2;
fC.gridy = 2;
fC.gridwidth = 1;
fC.gridheight = 1;
fC.fill = GridBagConstraints.BOTH;
fC.insets = new Insets(3, 3, 3, 3);
f = new Button("F");
gbl.setConstraints(f, fC);
f.addActionListener(this);
this.add(f);
GridBagConstraints threeC = new GridBagConstraints();
threeC.gridx = 2;
threeC.gridy = 3;
threeC.gridwidth = 1;
threeC.gridheight = 1;
threeC.fill = GridBagConstraints.BOTH;
threeC.insets = new Insets(3, 3, 3, 3);
three = new Button("3");
gbl.setConstraints(three, threeC);
three.addActionListener(this);
this.add(three);
GridBagConstraints sixC = new GridBagConstraints();
sixC.gridx = 2;
sixC.gridy = 4;
sixC.gridwidth = 1;
sixC.gridheight = 1;
sixC.fill = GridBagConstraints.BOTH;
sixC.insets = new Insets(3, 3, 3, 3);
six = new Button("6");
gbl.setConstraints(six, sixC);
six.addActionListener(this);
this.add(six);
GridBagConstraints selectC = new GridBagConstraints();
selectC.gridx = 0;
selectC.gridy = 5;
selectC.gridwidth = 3;
selectC.gridheight = 1;
selectC.fill = GridBagConstraints.BOTH;
selectC.insets = new Insets(3, 3, 3, 3);
select = new Button("Select");
gbl.setConstraints(select, selectC);
select.addActionListener(this);
this.add(select);
}
public void actionPerformed(ActionEvent evt) {
String cmd = evt.getActionCommand();
if(cmd.equals("Select")){
if((textF.getText().charAt(0)=='A'||textF.getText().charAt(0)=='B'||
textF.getText().charAt(0)=='C'||textF.getText().charAt(0)=='D'||
textF.getText().charAt(0)=='E'||textF.getText().charAt(0)=='F')&&
(textF.getText().charAt(1)=='1'||textF.getText().charAt(1)=='2'||
textF.getText().charAt(1)=='3'||textF.getText().charAt(1)=='4'||
textF.getText().charAt(1)=='5'||textF.getText().charAt(1)=='6')){
//using the VProgram's appletContext
master.getAppletContext().showStatus( "selected. Please insert $1.00");
}
else{
master.getAppletContext().showStatus("Incorrect entry. One letter then one number.");
}
textF.setText("");
}
else{
textF.setText(textF.getText() + cmd);
}
}
public void paint(Graphics g){
super.paint(g);
g.setColor( Color.GRAY);
g.fillRect(0,0, 100, 400);
g.setColor( Color.BLACK);
g.drawRect(0, 0, 100, 400);
g.fillRect(45, 10, 10, 60);
}
}
#9
Re: ActionPreformed issues
Posted 30 March 2010 - 12:50 PM
Wow, that's really cool, once again, I appreciate your patience and I'm still trying to get the flow down. You've been more than a big help
#10
Re: ActionPreformed issues
Posted 30 March 2010 - 02:17 PM
Follow up: I took the above example then created coin buttons so the user can "purchase" an item selected. I commented all my code so everyone can understand my thought process. It still has a few small bugs but it functions well for the most part. Thanks to you guys for all your help. Here's the code
VProgram (Main Class which you should run)
VMachine(Panel GUI)
Coins( used to create button representation of coins to "Pay")
VProgram (Main Class which you should run)
/*This program runs and creates a vending machine interface so *the user can "purchase items" from the machine
*/
import java.awt.BorderLayout;
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
public class VProgram extends Applet{
private int width, height;
//instantiation of Coins and VMachine
private VMachine gui;
private Coins buttons;
public VProgram(){
//creates and object of VMachine and Coins then sets the layout type
setLayout(new BorderLayout());
gui = new VMachine(this);
buttons = new Coins(this);
}
public void init(){
width = 300;
height = 300;
//sets applet size..
setSize(width, height);
//...then adds and aligns gui and buttons
add(gui, "East");
add(buttons, "South");
}
public void paint(Graphics g){
//paints the background of the panel and the "vending machine window"
super.paint(g);
g.setColor( Color.LIGHT_GRAY);
g.fillRect(0, 0, width, height);
}
}
VMachine(Panel GUI)
/*
* This Class creates a text field, four rows of three buttons (a-f & one-six), and a select button
* for the purposes of representing a vending machine panel
*/
import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class VMachine extends Applet implements ActionListener {
//boolean used to set the state for when the user can put coins into the machine
public static boolean inputCoins = false;
public static GridBagLayout gbl;
//the needed buttons and text field
private Button a, b, c, d, e, f, one, two, three, four, five, six, select;
private TextField textF;
//a VMachine constructor now gets an Object of VProgram
VProgram master;
public VMachine(VProgram master){
gbl = new GridBagLayout();
//uses gbl as the layout
this.setLayout(gbl);
//getting VProgram's Object
this.master = master;
//sets the constraints for all 13 buttons and text field then creates and adds the button
GridBagConstraints text = new GridBagConstraints();
text.gridx = 0;
text.gridy = 0;
text.gridwidth = 3;
text.gridheight = 1;
text.fill = GridBagConstraints.HORIZONTAL;
textF = new TextField();
textF.setEditable(false);
textF.setText("");
gbl.setConstraints(textF, text);
this.add(textF);
GridBagConstraints aC = new GridBagConstraints();
aC.gridx = 0;
aC.gridy = 1;
aC.gridwidth = 1;
aC.gridheight = 1;
aC.fill = GridBagConstraints.BOTH;
aC.insets = new Insets(3, 3, 3, 3);
a = new Button("A");
gbl.setConstraints(a, aC);
a.addActionListener(this);
add(a);
GridBagConstraints dC = new GridBagConstraints();
dC.gridx = 0;
dC.gridy = 2;
dC.gridwidth = 1;
dC.gridheight = 1;
dC.fill = GridBagConstraints.BOTH;
dC.insets = new Insets(3, 3, 3, 3);
d = new Button("D");
gbl.setConstraints(d, dC);
d.addActionListener(this);
this.add(d);
GridBagConstraints oneC = new GridBagConstraints();
oneC.gridx = 0;
oneC.gridy = 3;
oneC.gridwidth = 1;
oneC.gridheight = 1;
oneC.fill = GridBagConstraints.BOTH;
oneC.insets = new Insets(3, 3, 3, 3);
one = new Button("1");
gbl.setConstraints(one, oneC);
one.addActionListener(this);
this.add(one);
GridBagConstraints fourC = new GridBagConstraints();
fourC.gridx = 0;
fourC.gridy = 4;
fourC.gridwidth = 1;
fourC.gridheight = 1;
fourC.fill = GridBagConstraints.BOTH;
fourC.insets = new Insets(3, 3, 3, 3);
four = new Button("4");
gbl.setConstraints(four, fourC);
four.addActionListener(this);
this.add(four);
GridBagConstraints bC = new GridBagConstraints();
bC.gridx = 1;
bC.gridy = 1;
bC.gridwidth = 1;
bC.gridheight = 1;
bC.fill = GridBagConstraints.BOTH;
bC.insets = new Insets(3, 3, 3, 3);
b = new Button("B");
gbl.setConstraints(b, bC);
b.addActionListener(this);
this.add(B)/>;
GridBagConstraints eC = new GridBagConstraints();
eC.gridx = 1;
eC.gridy = 2;
eC.gridwidth = 1;
eC.gridheight = 1;
eC.fill = GridBagConstraints.BOTH;
eC.insets = new Insets(3, 3, 3, 3);
e = new Button("E");
gbl.setConstraints(e, eC);
e.addActionListener(this);
this.add(e);
GridBagConstraints twoC = new GridBagConstraints();
twoC.gridx = 1;
twoC.gridy = 3;
twoC.gridwidth = 1;
twoC.gridheight = 1;
twoC.fill = GridBagConstraints.BOTH;
twoC.insets = new Insets(3, 3, 3, 3);
two = new Button("2");
gbl.setConstraints(two, twoC);
two.addActionListener(this);
this.add(two);
GridBagConstraints fiveC = new GridBagConstraints();
fiveC.gridx = 1;
fiveC.gridy = 4;
fiveC.gridwidth = 1;
fiveC.gridheight = 1;
fiveC.fill = GridBagConstraints.BOTH;
fiveC.insets = new Insets(3, 3, 3, 3);
five = new Button("5");
gbl.setConstraints(five, fiveC);
five.addActionListener(this);
this.add(five);
GridBagConstraints cC = new GridBagConstraints();
cC.gridx = 2;
cC.gridy = 1;
cC.gridwidth = 1;
cC.gridheight = 1;
cC.fill = GridBagConstraints.BOTH;
cC.insets = new Insets(3, 3, 3, 3);
c = new Button("C");
gbl.setConstraints(c, cC);
c.addActionListener(this);
this.add(c);
GridBagConstraints fC = new GridBagConstraints();
fC.gridx = 2;
fC.gridy = 2;
fC.gridwidth = 1;
fC.gridheight = 1;
fC.fill = GridBagConstraints.BOTH;
fC.insets = new Insets(3, 3, 3, 3);
f = new Button("F");
gbl.setConstraints(f, fC);
f.addActionListener(this);
this.add(f);
GridBagConstraints threeC = new GridBagConstraints();
threeC.gridx = 2;
threeC.gridy = 3;
threeC.gridwidth = 1;
threeC.gridheight = 1;
threeC.fill = GridBagConstraints.BOTH;
threeC.insets = new Insets(3, 3, 3, 3);
three = new Button("3");
gbl.setConstraints(three, threeC);
three.addActionListener(this);
this.add(three);
GridBagConstraints sixC = new GridBagConstraints();
sixC.gridx = 2;
sixC.gridy = 4;
sixC.gridwidth = 1;
sixC.gridheight = 1;
sixC.fill = GridBagConstraints.BOTH;
sixC.insets = new Insets(3, 3, 3, 3);
six = new Button("6");
gbl.setConstraints(six, sixC);
six.addActionListener(this);
this.add(six);
GridBagConstraints selectC = new GridBagConstraints();
selectC.gridx = 0;
selectC.gridy = 5;
selectC.gridwidth = 3;
selectC.gridheight = 1;
selectC.fill = GridBagConstraints.BOTH;
selectC.insets = new Insets(3, 3, 3, 3);
select = new Button("Select");
gbl.setConstraints(select, selectC);
select.addActionListener(this);
this.add(select);
}
public void actionPerformed(ActionEvent evt) {
//sets the button's command to a string
String cmd = evt.getActionCommand();
//if cmd equals "A" up to "F" in the first position and "1" up to"6"
if(cmd.equals("Select") && !inputCoins){
if((textF.getText().charAt(0)=='A'||textF.getText().charAt(0)=='B'||
textF.getText().charAt(0)=='C'||textF.getText().charAt(0)=='D'||
textF.getText().charAt(0)=='E'||textF.getText().charAt(0)=='F')&&
(textF.getText().charAt(1)=='1'||textF.getText().charAt(1)=='2'||
textF.getText().charAt(1)=='3'||textF.getText().charAt(1)=='4'||
textF.getText().charAt(1)=='5'||textF.getText().charAt(1)=='6')){
//using the VProgram's appletContext
master.getAppletContext().showStatus( textF.getText() +
" selected. Please insert $1.00");
//sets boolean so user can input coins now
inputCoins = true;
}
else{
//default statement
master.getAppletContext().showStatus("Incorrect entry. One letter then one number.");
}
//clears the text field
textF.setText("");
}
// if the select button is unpressed, the button's action will be added to the text field
else{
textF.setText(textF.getText() + cmd);
}
}
public void paint(Graphics g){
//paints the "coin slot"
super.paint(g);
g.setColor( Color.GRAY);
g.fillRect(0,0, 100, 400);
g.setColor( Color.BLACK);
g.drawRect(0, 0, 100, 400);
g.fillRect(45, 10, 10, 60);
}
}
Coins( used to create button representation of coins to "Pay")
/* This Class creates the buttons used to represent a nickel, dime, quarter, or dollar that is used to
* "pay" the machine the proper amount for an item selected.
*/
import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Coins extends Applet implements ActionListener{
//used to keep track of the money input into the machine and how much is left to be paid
private int moneyInput, amountLeft;
//the cost of everything in the machine
private final int VALUE = 100;
//the buttons needed
private Button nickel, dime, quarter, dollar;
//the layout that Coins uses
public static GridBagLayout cL;
VProgram master;
//a Coins constructor now gets an Object of VProgram
public Coins(VProgram master){
cL = new GridBagLayout();
this.setLayout(cL);
this.master = master;
//sets the constriants and adds the four buttons in a row
GridBagConstraints niC = new GridBagConstraints();
niC.gridx = 0;
niC.gridy = 0;
niC.gridwidth = 2;
niC.gridheight = 1;
niC.fill = GridBagConstraints.HORIZONTAL;
nickel = new Button("Nickel");
cL.setConstraints(nickel, niC);
nickel.addActionListener(this);
this.add(nickel);
GridBagConstraints diC = new GridBagConstraints();
diC.gridx = 2;
diC.gridy = 0;
diC.gridwidth = 2;
diC.gridheight = 1;
diC.fill = GridBagConstraints.BOTH;
diC.insets = new Insets(3, 3, 3, 3);
dime = new Button("Dime");
cL.setConstraints(dime, diC);
dime.addActionListener(this);
this.add(dime);
GridBagConstraints quC = new GridBagConstraints();
quC.gridx = 4;
quC.gridy = 0;
quC.gridwidth = 2;
quC.gridheight = 1;
quC.fill = GridBagConstraints.BOTH;
quC.insets = new Insets(3, 3, 3, 3);
quarter = new Button("Quarter");
cL.setConstraints(quarter, quC);
quarter.addActionListener(this);
this.add(quarter);
GridBagConstraints doC = new GridBagConstraints();
doC.gridx = 6;
doC.gridy = 0;
doC.gridwidth = 2;
doC.gridheight = 1;
doC.fill = GridBagConstraints.BOTH;
doC.insets = new Insets(3, 3, 3, 3);
dollar = new Button("Dollar");
cL.setConstraints(dollar, doC);
dollar.addActionListener(this);
this.add(dollar);
}
public void actionPerformed(ActionEvent evt) {
//if VMachine's inputCoins is true, then allow the user to use the coin buttons
if(VMachine.inputCoins){
//creates a String of which button was pressed...
String cmd = evt.getActionCommand();
//...then compares to the four different buttons...
if(cmd.equals("Nickel")){
//...then adds the proper amount to the money that has been input into the machine
moneyInput += 5;
}
if(cmd.equals("Dime")){
moneyInput += 10;
}
if(cmd.equals("Quarter")){
moneyInput += 25;
}
if(cmd.equals("Dollar")){
moneyInput += 100;
}
//calculates how much is left to pay
amountLeft = VALUE - moneyInput;
//if amount is 0 then "retrieve" item and set VMachine's inputCoins to false so the
//coin buttons are "deactivated" and set the money inputed back to 0
if(amountLeft == 0){
master.getAppletContext().showStatus("Retrieving item, Thank You!");
VMachine.inputCoins = false;
moneyInput = 0;
}
//if amount is less than 0 then "retrieve" item, gives proper change, sets VMachine's
//inputCoins to false so the coin buttons are "deactivated" and sets the money inputed
//into the machine back to 0
if(amountLeft < 0){
master.getAppletContext().showStatus("Retrieving Item. Change: " + Math.abs(amountLeft));
VMachine.inputCoins = false;
moneyInput = 0;
}
//if amount is smaller than 0, tell the user how much more to input
if(amountLeft > 0){
master.getAppletContext().showStatus("Please insert 0." + amountLeft + " cents");
}
}
}
}
Page 1 of 1

New Topic/Question
Reply


MultiQuote



|