duta
asked on
Java Swing: getSource vs. getActionCommand
Dear experts:
Hi again!
I gave a couple of questions:
First, I have a simple code below to play a lottery game. In actionPerformed, I used getActionCommand. But someone commented that getSource may be better to use that getActionCommand. Can you kindly modify the code to use getSource, instead of getActionCommand.
Second, the someone also said that, with Java 5, content panes are no longer needed, and I need to remove the creation of the content pane and all later reference to it.
Can you kindly modify my code so that content panes may be removed?
Thanks a lot!
duta
__________________________ Here is my complete code __________________________ _____
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Lottery extends JApplet
{
private JFrame myWindow;
private final int [ ] theNumber= { 3, 7, 5, 8, 6, 2 };
private JTextField tTop1;
private JTextField tTop2;
private JTextField tTop3;
private JTextField tTop4;
private JTextField tTop5;
private JTextField tTop6;
private JTextField tCenter1;
private JTextField tCenter2;
private JTextField tCenter3;
private JTextField tCenter4;
private JTextField tCenter5;
private JTextField tCenter6;
private JTextField tBottom1;
private JTextField tBottom2;
private JTextField tBottom3;
private JTextField tBottom4;
private JTextField tBottom5;
private JTextField tBottom6;
private JTextField tYouWon;
int t1, t2, t3, t4, t5, t6 = 0;
public void init ( )
{
Container c = this.getContentPane ();
myWindow = new JFrame ();
myWindow.setSize ( 400, 500 );
c.setLayout ( new BorderLayout () );
c.add ( TopPanel (), BorderLayout.NORTH );
c.add ( CenterPanel (), BorderLayout.CENTER );
c.add ( BottomPanel (), BorderLayout.SOUTH );
}
private JPanel TopPanel ()
{
JPanel pnl = new JPanel ();
Font fnt = new Font ( null, Font.PLAIN, 20 );
JLabel lbl = new JLabel ( "$1 Million Lottery" );
lbl.setFont ( fnt );
pnl.add ( lbl );
return pnl;
}
private JPanel CenterPanel ()
{
JPanel pnl = new JPanel ();
pnl.setLayout ( new GridLayout ( 4, 1 ) );
pnl.add ( FirstPanel () );
pnl.add ( SecondPanel() );
pnl.add ( ThirdPanel () );
pnl.add ( FourthPanel() );
return pnl;
}
private JPanel FirstPanel ()
{
JPanel pnl = new JPanel ();
JLabel lblFirst = new JLabel ( "Your Drawing : " );
tTop1 = new JTextField ( 1 );
tTop2 = new JTextField ( 1 );
tTop3 = new JTextField ( 1 );
tTop4 = new JTextField ( 1 );
tTop5 = new JTextField ( 1 );
tTop6 = new JTextField ( 1 );
pnl.add ( lblFirst );
pnl.add ( tTop1 );
pnl.add ( tTop2 );
pnl.add ( tTop3 );
pnl.add ( tTop4 );
pnl.add ( tTop5 );
pnl.add ( tTop6 );
return pnl;
}
private JPanel SecondPanel ()
{
JPanel pnl = new JPanel ();
JLabel lblSecond = new JLabel ( "Right numbers : " );
tCenter1 = new JTextField ( 1 );
tCenter2 = new JTextField ( 1);
tCenter3 = new JTextField ( 1 );
tCenter4 = new JTextField ( 1 );
tCenter5 = new JTextField ( 1 );
tCenter6 = new JTextField ( 1 );
pnl.add ( lblSecond );
pnl.add ( tCenter1 );
pnl.add ( tCenter2 );
pnl.add ( tCenter3 );
pnl.add ( tCenter4 );
pnl.add ( tCenter5 );
pnl.add ( tCenter6 );
return pnl;
}
private JPanel ThirdPanel ()
{
JPanel pnl = new JPanel ();
JLabel lblThird = new JLabel ( "Matched : " );
tBottom1 = new JTextField ( 1 );
tBottom2 = new JTextField ( 1 );
tBottom3 = new JTextField ( 1 );
tBottom4 = new JTextField ( 1 );
tBottom5 = new JTextField ( 1 );
tBottom6 = new JTextField ( 1 );
pnl.add ( lblThird );
pnl.add ( tBottom1 );
pnl.add ( tBottom2 );
pnl.add ( tBottom3 );
pnl.add ( tBottom4 );
pnl.add ( tBottom5 );
pnl.add ( tBottom6 );
return pnl;
}
private JPanel FourthPanel ()
{
JPanel pnl = new JPanel ();
JLabel lblYouWon = new JLabel ( "Win / Lose : " );
tYouWon = new JTextField ( 12);
tYouWon.setBackground ( Color.white);
tYouWon.setEditable (false);
pnl.add ( lblYouWon );
pnl.add ( tYouWon );
return pnl;
}
private JPanel BottomPanel ()
{
JPanel pnl = new JPanel ();
JButton bDraw = new JButton ( "Draw" );
JButton bMatch = new JButton ( "Match" );
JButton bReset = new JButton ( "Reset" );
bDraw.setActionCommand ( "draw" );
bMatch.setActionCommand ( "match" );
bReset.setActionCommand ( "reset" );
ButtonListener bl = new ButtonListener ();
bDraw.addActionListener ( bl );
bMatch.addActionListener ( bl );
bReset.addActionListener ( bl );
pnl.add ( bDraw );
pnl.add ( bMatch );
pnl.add ( bReset );
return pnl;
}
private void Reset ()
{
tTop1.setText ( " " );
tTop2.setText ( " " );
tTop3.setText ( " " );
tTop4.setText ( " " );
tTop5.setText ( " " );
tTop6.setText ( " " );
tCenter1.setText ( " " );
tCenter2.setText ( " " );
tCenter3.setText ( " " );
tCenter4.setText ( " " );
tCenter5.setText ( " " );
tCenter6.setText ( " " );
tYouWon.setText ( " " );
tBottom1.setText (" ");
tBottom2.setText (" ");
tBottom3.setText (" ");
tBottom4.setText (" ");
tBottom5.setText (" ");
tBottom6.setText (" ");
}
private class ButtonListener implements ActionListener
{
public void actionPerformed ( ActionEvent e )
{
int num = 0;
if ( "draw".equals ( e.getActionCommand () ) )
{
try
{
t1 = (int) (Math.random () * 10 );
tTop1.setText ( "" + t1 );
t2 = (int) (Math.random () * 10 );
tTop2.setText ( "" + t2 );
t3 = (int) (Math.random () * 10 );
tTop3.setText ( "" + t3 );
t4 = (int) (Math.random () * 10 );
tTop4.setText ( "" + t4 );
t5 = (int) (Math.random () * 10 );
tTop5.setText ( "" + t5 );
t6 = (int) (Math.random () * 10 );
tTop6.setText ( "" + t6 );
}
catch (NumberFormatException ne )
{
System.out.println (ne);
}
}
else if ( "match".equals ( e.getActionCommand () ) )
{
tCenter1.setText ( "" + theNumber[0] );
tCenter2.setText ( "" + theNumber[1] );
tCenter3.setText ( "" + theNumber[2] );
tCenter4.setText ( "" + theNumber[3] );
tCenter5.setText ( "" + theNumber[4] );
tCenter6.setText ( "" + theNumber[5] );
int count = 0;
if ( t1 == theNumber[0] )
{
tBottom1.setText ("N");
count++;
}
else
tBottom1.setText (" ");
if ( t2 == theNumber[1] )
{
tBottom2.setText ("N");
count++;
}
else
tBottom2.setText (" ");
if ( t3 == theNumber[2] )
{
tBottom3.setText ("N");
count++;
}
else
tBottom3.setText (" ");
if ( t4 == theNumber[3] )
{
tBottom4.setText ("N");
count++;
}
else
tBottom4.setText (" ");
if ( t5 == theNumber[4] )
{
tBottom5.setText ("N");
count++;
}
else
tBottom5.setText (" ");
if ( t6 == theNumber[5] )
{
tBottom6.setText ("N");
count++;
}
else
tBottom6.setText (" ");
if ( count > 5 )
tYouWon.setText ( " Bingo. You won $1 million." );
else
tYouWon.setText (" " + count + " number(s) right");
}
else if ( "reset".equals ( e.getActionCommand () ) )
{
Reset ();
}
}
}
}
__________________________ ______ The end of my code __________________________ __________
Hi again!
I gave a couple of questions:
First, I have a simple code below to play a lottery game. In actionPerformed, I used getActionCommand. But someone commented that getSource may be better to use that getActionCommand. Can you kindly modify the code to use getSource, instead of getActionCommand.
Second, the someone also said that, with Java 5, content panes are no longer needed, and I need to remove the creation of the content pane and all later reference to it.
Can you kindly modify my code so that content panes may be removed?
Thanks a lot!
duta
__________________________
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Lottery extends JApplet
{
private JFrame myWindow;
private final int [ ] theNumber= { 3, 7, 5, 8, 6, 2 };
private JTextField tTop1;
private JTextField tTop2;
private JTextField tTop3;
private JTextField tTop4;
private JTextField tTop5;
private JTextField tTop6;
private JTextField tCenter1;
private JTextField tCenter2;
private JTextField tCenter3;
private JTextField tCenter4;
private JTextField tCenter5;
private JTextField tCenter6;
private JTextField tBottom1;
private JTextField tBottom2;
private JTextField tBottom3;
private JTextField tBottom4;
private JTextField tBottom5;
private JTextField tBottom6;
private JTextField tYouWon;
int t1, t2, t3, t4, t5, t6 = 0;
public void init ( )
{
Container c = this.getContentPane ();
myWindow = new JFrame ();
myWindow.setSize ( 400, 500 );
c.setLayout ( new BorderLayout () );
c.add ( TopPanel (), BorderLayout.NORTH );
c.add ( CenterPanel (), BorderLayout.CENTER );
c.add ( BottomPanel (), BorderLayout.SOUTH );
}
private JPanel TopPanel ()
{
JPanel pnl = new JPanel ();
Font fnt = new Font ( null, Font.PLAIN, 20 );
JLabel lbl = new JLabel ( "$1 Million Lottery" );
lbl.setFont ( fnt );
pnl.add ( lbl );
return pnl;
}
private JPanel CenterPanel ()
{
JPanel pnl = new JPanel ();
pnl.setLayout ( new GridLayout ( 4, 1 ) );
pnl.add ( FirstPanel () );
pnl.add ( SecondPanel() );
pnl.add ( ThirdPanel () );
pnl.add ( FourthPanel() );
return pnl;
}
private JPanel FirstPanel ()
{
JPanel pnl = new JPanel ();
JLabel lblFirst = new JLabel ( "Your Drawing : " );
tTop1 = new JTextField ( 1 );
tTop2 = new JTextField ( 1 );
tTop3 = new JTextField ( 1 );
tTop4 = new JTextField ( 1 );
tTop5 = new JTextField ( 1 );
tTop6 = new JTextField ( 1 );
pnl.add ( lblFirst );
pnl.add ( tTop1 );
pnl.add ( tTop2 );
pnl.add ( tTop3 );
pnl.add ( tTop4 );
pnl.add ( tTop5 );
pnl.add ( tTop6 );
return pnl;
}
private JPanel SecondPanel ()
{
JPanel pnl = new JPanel ();
JLabel lblSecond = new JLabel ( "Right numbers : " );
tCenter1 = new JTextField ( 1 );
tCenter2 = new JTextField ( 1);
tCenter3 = new JTextField ( 1 );
tCenter4 = new JTextField ( 1 );
tCenter5 = new JTextField ( 1 );
tCenter6 = new JTextField ( 1 );
pnl.add ( lblSecond );
pnl.add ( tCenter1 );
pnl.add ( tCenter2 );
pnl.add ( tCenter3 );
pnl.add ( tCenter4 );
pnl.add ( tCenter5 );
pnl.add ( tCenter6 );
return pnl;
}
private JPanel ThirdPanel ()
{
JPanel pnl = new JPanel ();
JLabel lblThird = new JLabel ( "Matched : " );
tBottom1 = new JTextField ( 1 );
tBottom2 = new JTextField ( 1 );
tBottom3 = new JTextField ( 1 );
tBottom4 = new JTextField ( 1 );
tBottom5 = new JTextField ( 1 );
tBottom6 = new JTextField ( 1 );
pnl.add ( lblThird );
pnl.add ( tBottom1 );
pnl.add ( tBottom2 );
pnl.add ( tBottom3 );
pnl.add ( tBottom4 );
pnl.add ( tBottom5 );
pnl.add ( tBottom6 );
return pnl;
}
private JPanel FourthPanel ()
{
JPanel pnl = new JPanel ();
JLabel lblYouWon = new JLabel ( "Win / Lose : " );
tYouWon = new JTextField ( 12);
tYouWon.setBackground ( Color.white);
tYouWon.setEditable (false);
pnl.add ( lblYouWon );
pnl.add ( tYouWon );
return pnl;
}
private JPanel BottomPanel ()
{
JPanel pnl = new JPanel ();
JButton bDraw = new JButton ( "Draw" );
JButton bMatch = new JButton ( "Match" );
JButton bReset = new JButton ( "Reset" );
bDraw.setActionCommand ( "draw" );
bMatch.setActionCommand ( "match" );
bReset.setActionCommand ( "reset" );
ButtonListener bl = new ButtonListener ();
bDraw.addActionListener ( bl );
bMatch.addActionListener ( bl );
bReset.addActionListener ( bl );
pnl.add ( bDraw );
pnl.add ( bMatch );
pnl.add ( bReset );
return pnl;
}
private void Reset ()
{
tTop1.setText ( " " );
tTop2.setText ( " " );
tTop3.setText ( " " );
tTop4.setText ( " " );
tTop5.setText ( " " );
tTop6.setText ( " " );
tCenter1.setText ( " " );
tCenter2.setText ( " " );
tCenter3.setText ( " " );
tCenter4.setText ( " " );
tCenter5.setText ( " " );
tCenter6.setText ( " " );
tYouWon.setText ( " " );
tBottom1.setText (" ");
tBottom2.setText (" ");
tBottom3.setText (" ");
tBottom4.setText (" ");
tBottom5.setText (" ");
tBottom6.setText (" ");
}
private class ButtonListener implements ActionListener
{
public void actionPerformed ( ActionEvent e )
{
int num = 0;
if ( "draw".equals ( e.getActionCommand () ) )
{
try
{
t1 = (int) (Math.random () * 10 );
tTop1.setText ( "" + t1 );
t2 = (int) (Math.random () * 10 );
tTop2.setText ( "" + t2 );
t3 = (int) (Math.random () * 10 );
tTop3.setText ( "" + t3 );
t4 = (int) (Math.random () * 10 );
tTop4.setText ( "" + t4 );
t5 = (int) (Math.random () * 10 );
tTop5.setText ( "" + t5 );
t6 = (int) (Math.random () * 10 );
tTop6.setText ( "" + t6 );
}
catch (NumberFormatException ne )
{
System.out.println (ne);
}
}
else if ( "match".equals ( e.getActionCommand () ) )
{
tCenter1.setText ( "" + theNumber[0] );
tCenter2.setText ( "" + theNumber[1] );
tCenter3.setText ( "" + theNumber[2] );
tCenter4.setText ( "" + theNumber[3] );
tCenter5.setText ( "" + theNumber[4] );
tCenter6.setText ( "" + theNumber[5] );
int count = 0;
if ( t1 == theNumber[0] )
{
tBottom1.setText ("N");
count++;
}
else
tBottom1.setText (" ");
if ( t2 == theNumber[1] )
{
tBottom2.setText ("N");
count++;
}
else
tBottom2.setText (" ");
if ( t3 == theNumber[2] )
{
tBottom3.setText ("N");
count++;
}
else
tBottom3.setText (" ");
if ( t4 == theNumber[3] )
{
tBottom4.setText ("N");
count++;
}
else
tBottom4.setText (" ");
if ( t5 == theNumber[4] )
{
tBottom5.setText ("N");
count++;
}
else
tBottom5.setText (" ");
if ( t6 == theNumber[5] )
{
tBottom6.setText ("N");
count++;
}
else
tBottom6.setText (" ");
if ( count > 5 )
tYouWon.setText ( " Bingo. You won $1 million." );
else
tYouWon.setText (" " + count + " number(s) right");
}
else if ( "reset".equals ( e.getActionCommand () ) )
{
Reset ();
}
}
}
}
__________________________
ASKER
TO: objects:
Thank you so much for your kind, prompt response.
For my code, which command (between getSource and getActionCommand) is better to use?
If I want to replace getActionCommand with getSource (if I may or if I have to), how do I need to modify the code?
I have never used getSource, I am totally dark about how to use it.
Thanks again!
duta
Thank you so much for your kind, prompt response.
For my code, which command (between getSource and getActionCommand) is better to use?
If I want to replace getActionCommand with getSource (if I may or if I have to), how do I need to modify the code?
I have never used getSource, I am totally dark about how to use it.
Thanks again!
duta
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
TO: objects:
Thanks again for your kind, prompt response.
By the way, can you kindly answer my second question?
My second question is:
Someone also said that, with Java 5, content panes are no longer needed, and I need to remove the creation of the content pane and all later reference to it.
Thanks again for your kind, prompt response.
By the way, can you kindly answer my second question?
My second question is:
Someone also said that, with Java 5, content panes are no longer needed, and I need to remove the creation of the content pane and all later reference to it.
i did above, what you already have is fine.
getActionCommand() returns the action command property
so in your case if you want to check if a specific button was pressed use getSource(),
if you want to know if a button with a certain command is pressed the use getActionCommand()
your use of content pane can stay as it is