-
re: beginner java need help
I am a beginner with Java & yes this is for a class. My group has spent 2
weeks trying to get this to work. We have to create a base converter with
a GUI interface. We have tried lots of things & nothing works. Now we are
getting this error "the action performed is not defined". Any suggestions
or tips would be greatly appreciated.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.text.*;
import java.lang.String.*;
class Counter extends JPanel implements ActionListener
{
int base,value;
int currentnum=0;
char counting [] = { '0','1','2','3','4','5','6','7','8','9','A','B','C',
'D','E','F','G','H','I','J','K','L','M','N','O','P',
'Q','R','S','T','U','V','W','X','Y','Z'};
protected JTextField baseField;
protected JTextField intrValField;
protected JTextField numDisplay;
protected JButton decButton= new JButton( "Decrement" );
protected JButton incButton= new JButton( "Increment" );
protected JButton resetButton= new JButton( "Reset" );
public Counter()
{
init();
doTheLayout( );
}
public void init()
{
decButton.addActionListener (this);
decButton.setActionCommand("dec");
incButton.addActionListener (this);
incButton.setActionCommand("inc");
resetButton.addActionListener (this);
resetButton.setActionCommand("re");
}
public void actionPerfomed(ActionEvent evt)
{
if ("dec".equals(evt.getActionCommand()))
{
initializeData();
DecInterval();
}
else if ("inc".equals(evt.getActionCommand()))
{
initializeData();
IncInterval();
}
else
{
numDisplay.setText("0");
intrValField.setText("0");
baseField.setText("0");
currentnum=0;
}
}
// Make all the objects
public void initializeData()
{
base= Integer.parseInt(baseField.getText());
value=Integer.parseInt(intrValField.getText());
}
// Layout all the objects
public void doTheLayout( )
{
JTextField baseField = new JTextField( 10 );
JTextField intrValField = new JTextField( 10 );
JTextField numDisplay= new JTextField(20);
numDisplay.setEditable(false);
JPanel bottomHalf = new JPanel( );
JPanel topHalf= new JPanel( );
// Layout the top half
topHalf.setLayout( new FlowLayout( ) );
topHalf.add( new JLabel( "Base" ) );
topHalf.add( baseField );
topHalf.add( new JLabel( "Interval" ) );
intrValField=new JTextField(10);
topHalf.add( intrValField );
intrValField.addActionListener(this);
topHalf.add( new JLabel( "Current Count" ) );
numDisplay=new JTextField(20);
topHalf.add( numDisplay );
// Layout the bottom half
bottomHalf.setLayout( new FlowLayout( ) );
bottomHalf.add( decButton );
bottomHalf.add( incButton );
bottomHalf.add( resetButton );
// Now layout GUI
setLayout( new BorderLayout( ) );
add( topHalf, "North" );
add( bottomHalf, "South" );
}
public void IncInterval()
{
int tempb=base;
currentnum=currentnum+value;
int tempnum=currentnum;
String output= " ";
int remainder, beginning;
while (tempnum > tempb)
{
remainder= tempnum % tempb;
output= counting[remainder] + output;
beginning= tempnum/tempb;
tempnum= beginning;
}
output= counting[remainder] + output;
numDisplay.setText(output);
}
public void DecInterval()
{
int tempb=base;
currentnum=currentnum-value;
int tempnum=currentnum;
String output= " ";
int remainder, beginning;
while (tempnum > tempb)
{
remainder= tempnum % tempb;
output= counting[remainder] + output;
beginning= tempnum/tempb;
tempnum= beginning;
}
output= counting[remainder] + output;
numDisplay.setText(output);
}
}
class CloseableFrame extends JFrame
{
public CloseableFrame( )
{
addWindowListener( new WindowAdapter( )
{
public void windowClosing( WindowEvent event )
{ System.exit( 0 ); }
}
);
}
}
class CounterGUI extends CloseableFrame
{
public static void main( String [] args )
{
JFrame f = new CounterGUI( );
f.setTitle( "The Counter" );
Container contentPane = f.getContentPane( );
contentPane.add( new Counter() );
f.pack( );
f.show( );
}
}
-
re: beginner java need help
"M"
I am not sure about your exact requirements for creating a base converter
are but it seems to me that your main problem is getting the ActionListener
functions working. I have modified your code concentrating only on the ActionListener
object code definitions. I have also simplified your three classes in one
class. A simple comparison of your old code to the new code should be self
explanatory! Good luck with finishing your project!
E
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.text.*;
import java.lang.String.*;
class CounterGUI extends JFrame implements ActionListener
{
int base,value;
int currentnum=0;
char counting [] = { '0','1','2','3','4','5','6','7','8','9','A','B','C',
'D','E','F','G','H','I','J','K','L','M','N','O','P',
'Q','R','S','T','U','V','W','X','Y','Z'};
protected JTextField baseField;
protected JTextField intrValField;
protected JTextField numDisplay;
protected JButton decButton= new JButton( "Decrement" );
protected JButton incButton= new JButton( "Increment" );
protected JButton resetButton= new JButton( "Reset" );
public CounterGUI () {
baseField = new JTextField("0", 10 );
intrValField = new JTextField("0", 10 );
numDisplay= new JTextField("0", 20);
numDisplay.setEditable(false);
JPanel bottomHalf = new JPanel( );
JPanel topHalf= new JPanel( );
// Layout the top half
topHalf.setLayout( new FlowLayout( ) );
topHalf.add( new JLabel( "Base" ) );
topHalf.add( baseField );
topHalf.add( new JLabel( "Interval" ) );
intrValField=new JTextField(10);
topHalf.add( intrValField );
// Unsure what this was for so I created it as an inline routine
// ActionListener implementation, but it could easily be transferred
to
// the actionPerformed routine below.
intrValField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("intrValField Action");
}
});
topHalf.add( new JLabel( "Current Count" ) );
numDisplay=new JTextField(20);
topHalf.add( numDisplay );
// Layout the bottom half
bottomHalf.setLayout( new FlowLayout( ) );
bottomHalf.add( decButton );
bottomHalf.add( incButton );
bottomHalf.add( resetButton );
// The the JPanel topHalf and bottomHalf to the ContentPane of JFrame
getContentPane().setLayout(new BorderLayout());
getContentPane().add(topHalf, "North" );
getContentPane().add(bottomHalf, "South" );
// Add the ActionListeners, processed by actionPerformed event handler
// below.
decButton.addActionListener(this);
incButton.addActionListener(this);
resetButton.addActionListener(this);
}
public static void main( String [] args )
{
JFrame f = new CounterGUI();
f.setTitle( "The Counter" );
// Old way of doing it (Java 1.2 and <
//addWindowListener( new WindowAdapter( )
// {
// public void windowClosing( WindowEvent event )
// { System.exit( 0 ); }
//
// }
//);
// New way of setting closing, less text.
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack( );
f.show( );
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == decButton) {
System.out.println("Decrement Button");
} else if (source == incButton) {
System.out.println("Increment Button");
} else if (source == resetButton) {
System.out.println("Reset Button");
}
}
}
"m" <merthb@comcast.net> wrote:
>
>I am a beginner with Java & yes this is for a class. My group has spent
2
>weeks trying to get this to work. We have to create a base converter with
>a GUI interface. We have tried lots of things & nothing works. Now we
are
>getting this error "the action performed is not defined". Any suggestions
>or tips would be greatly appreciated.
>
>
>
>
>
>import java.awt.*;
>import javax.swing.*;
>import java.awt.event.*;
>import java.text.*;
>import java.lang.String.*;
>
>
>class Counter extends JPanel implements ActionListener
>{
>
>
> int base,value;
> int currentnum=0;
> char counting [] = { '0','1','2','3','4','5','6','7','8','9','A','B','C',
> 'D','E','F','G','H','I','J','K','L','M','N','O','P',
> 'Q','R','S','T','U','V','W','X','Y','Z'};
>
> protected JTextField baseField;
> protected JTextField intrValField;
> protected JTextField numDisplay;
> protected JButton decButton= new JButton( "Decrement" );
> protected JButton incButton= new JButton( "Increment" );
> protected JButton resetButton= new JButton( "Reset" );
>
> public Counter()
> {
>
> init();
> doTheLayout( );
>
>
> }
>
> public void init()
> {
>
>
> decButton.addActionListener (this);
> decButton.setActionCommand("dec");
>
>
>
> incButton.addActionListener (this);
> incButton.setActionCommand("inc");
>
>
>
> resetButton.addActionListener (this);
> resetButton.setActionCommand("re");
>
> }
>
>
> public void actionPerfomed(ActionEvent evt)
> {
>
> if ("dec".equals(evt.getActionCommand()))
> {
> initializeData();
> DecInterval();
> }
>
> else if ("inc".equals(evt.getActionCommand()))
> {
> initializeData();
> IncInterval();
> }
>
> else
> {
> numDisplay.setText("0");
> intrValField.setText("0");
> baseField.setText("0");
> currentnum=0;
> }
> }
>
> // Make all the objects
>
> public void initializeData()
> {
> base= Integer.parseInt(baseField.getText());
> value=Integer.parseInt(intrValField.getText());
> }
>
>
>
> // Layout all the objects
> public void doTheLayout( )
> {
> JTextField baseField = new JTextField( 10 );
> JTextField intrValField = new JTextField( 10 );
> JTextField numDisplay= new JTextField(20);
> numDisplay.setEditable(false);
>
> JPanel bottomHalf = new JPanel( );
> JPanel topHalf= new JPanel( );
>
>
> // Layout the top half
> topHalf.setLayout( new FlowLayout( ) );
> topHalf.add( new JLabel( "Base" ) );
> topHalf.add( baseField );
>
>
> topHalf.add( new JLabel( "Interval" ) );
> intrValField=new JTextField(10);
> topHalf.add( intrValField );
>
>
> intrValField.addActionListener(this);
> topHalf.add( new JLabel( "Current Count" ) );
> numDisplay=new JTextField(20);
> topHalf.add( numDisplay );
>
>
>
> // Layout the bottom half
> bottomHalf.setLayout( new FlowLayout( ) );
> bottomHalf.add( decButton );
> bottomHalf.add( incButton );
> bottomHalf.add( resetButton );
>
>
> // Now layout GUI
> setLayout( new BorderLayout( ) );
> add( topHalf, "North" );
> add( bottomHalf, "South" );
>
> }
>
>
> public void IncInterval()
> {
> int tempb=base;
> currentnum=currentnum+value;
> int tempnum=currentnum;
> String output= " ";
> int remainder, beginning;
>
>
>
> while (tempnum > tempb)
> {
> remainder= tempnum % tempb;
> output= counting[remainder] + output;
> beginning= tempnum/tempb;
> tempnum= beginning;
> }
>
> output= counting[remainder] + output;
>
> numDisplay.setText(output);
>
>
> }
>
> public void DecInterval()
> {
> int tempb=base;
> currentnum=currentnum-value;
> int tempnum=currentnum;
> String output= " ";
> int remainder, beginning;
>
>
>
> while (tempnum > tempb)
> {
> remainder= tempnum % tempb;
> output= counting[remainder] + output;
> beginning= tempnum/tempb;
> tempnum= beginning;
> }
>
> output= counting[remainder] + output;
>
> numDisplay.setText(output);
>
> }
>}
>
>
>
>class CloseableFrame extends JFrame
>{
> public CloseableFrame( )
> {
> addWindowListener( new WindowAdapter( )
> {
> public void windowClosing( WindowEvent event )
> { System.exit( 0 ); }
>
> }
> );
> }
>}
>
>class CounterGUI extends CloseableFrame
>{
> public static void main( String [] args )
> {
> JFrame f = new CounterGUI( );
> f.setTitle( "The Counter" );
>
> Container contentPane = f.getContentPane( );
>
> contentPane.add( new Counter() );
> f.pack( );
> f.show( );
> }
>}
>
>
>
>
-
re: beginner java need help
M -
The way you are attaching your actionlistener to your buttons doesn't look
right. You should be able to pass the actual name of the component without
quotes. In addition, I think you need an inner class that handles the listening
events. If you are still having problems please post a reply. I can put up
some code to show you what I mean.
JackT
"m" <merthb@comcast.net> wrote:
>
>I am a beginner with Java & yes this is for a class. My group has spent
2
>weeks trying to get this to work. We have to create a base converter with
>a GUI interface. We have tried lots of things & nothing works. Now we
are
>getting this error "the action performed is not defined". Any suggestions
>or tips would be greatly appreciated.
>
>
>
>
>
>import java.awt.*;
>import javax.swing.*;
>import java.awt.event.*;
>import java.text.*;
>import java.lang.String.*;
>
>
>class Counter extends JPanel implements ActionListener
>{
>
>
> int base,value;
> int currentnum=0;
> char counting [] = { '0','1','2','3','4','5','6','7','8','9','A','B','C',
> 'D','E','F','G','H','I','J','K','L','M','N','O','P',
> 'Q','R','S','T','U','V','W','X','Y','Z'};
>
> protected JTextField baseField;
> protected JTextField intrValField;
> protected JTextField numDisplay;
> protected JButton decButton= new JButton( "Decrement" );
> protected JButton incButton= new JButton( "Increment" );
> protected JButton resetButton= new JButton( "Reset" );
>
> public Counter()
> {
>
> init();
> doTheLayout( );
>
>
> }
>
> public void init()
> {
>
>
> decButton.addActionListener (this);
> decButton.setActionCommand("dec");
>
>
>
> incButton.addActionListener (this);
> incButton.setActionCommand("inc");
>
>
>
> resetButton.addActionListener (this);
> resetButton.setActionCommand("re");
>
> }
>
>
> public void actionPerfomed(ActionEvent evt)
> {
>
> if ("dec".equals(evt.getActionCommand()))
> {
> initializeData();
> DecInterval();
> }
>
> else if ("inc".equals(evt.getActionCommand()))
> {
> initializeData();
> IncInterval();
> }
>
> else
> {
> numDisplay.setText("0");
> intrValField.setText("0");
> baseField.setText("0");
> currentnum=0;
> }
> }
>
> // Make all the objects
>
> public void initializeData()
> {
> base= Integer.parseInt(baseField.getText());
> value=Integer.parseInt(intrValField.getText());
> }
>
>
>
> // Layout all the objects
> public void doTheLayout( )
> {
> JTextField baseField = new JTextField( 10 );
> JTextField intrValField = new JTextField( 10 );
> JTextField numDisplay= new JTextField(20);
> numDisplay.setEditable(false);
>
> JPanel bottomHalf = new JPanel( );
> JPanel topHalf= new JPanel( );
>
>
> // Layout the top half
> topHalf.setLayout( new FlowLayout( ) );
> topHalf.add( new JLabel( "Base" ) );
> topHalf.add( baseField );
>
>
> topHalf.add( new JLabel( "Interval" ) );
> intrValField=new JTextField(10);
> topHalf.add( intrValField );
>
>
> intrValField.addActionListener(this);
> topHalf.add( new JLabel( "Current Count" ) );
> numDisplay=new JTextField(20);
> topHalf.add( numDisplay );
>
>
>
> // Layout the bottom half
> bottomHalf.setLayout( new FlowLayout( ) );
> bottomHalf.add( decButton );
> bottomHalf.add( incButton );
> bottomHalf.add( resetButton );
>
>
> // Now layout GUI
> setLayout( new BorderLayout( ) );
> add( topHalf, "North" );
> add( bottomHalf, "South" );
>
> }
>
>
> public void IncInterval()
> {
> int tempb=base;
> currentnum=currentnum+value;
> int tempnum=currentnum;
> String output= " ";
> int remainder, beginning;
>
>
>
> while (tempnum > tempb)
> {
> remainder= tempnum % tempb;
> output= counting[remainder] + output;
> beginning= tempnum/tempb;
> tempnum= beginning;
> }
>
> output= counting[remainder] + output;
>
> numDisplay.setText(output);
>
>
> }
>
> public void DecInterval()
> {
> int tempb=base;
> currentnum=currentnum-value;
> int tempnum=currentnum;
> String output= " ";
> int remainder, beginning;
>
>
>
> while (tempnum > tempb)
> {
> remainder= tempnum % tempb;
> output= counting[remainder] + output;
> beginning= tempnum/tempb;
> tempnum= beginning;
> }
>
> output= counting[remainder] + output;
>
> numDisplay.setText(output);
>
> }
>}
>
>
>
>class CloseableFrame extends JFrame
>{
> public CloseableFrame( )
> {
> addWindowListener( new WindowAdapter( )
> {
> public void windowClosing( WindowEvent event )
> { System.exit( 0 ); }
>
> }
> );
> }
>}
>
>class CounterGUI extends CloseableFrame
>{
> public static void main( String [] args )
> {
> JFrame f = new CounterGUI( );
> f.setTitle( "The Counter" );
>
> Container contentPane = f.getContentPane( );
>
> contentPane.add( new Counter() );
> f.pack( );
> f.show( );
> }
>}
>
>
>
>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|