Need help with button handlers
Hello I am taking a java class and I hit a problem with JButtons, button handlers, and action listeners.
I have to do this:
Step 0
// declare & initialize symbolic constants
This step has been implemented for you as we've declared symbolic int values to determine the size of the
JFrame and we've declared a symbolic String value for each of the JButtons that will appear on our form.
Step 1
// declare & initialize an action listener object
This step is similar to that which was performed in the previous exercise, except that you should create only a
single instance of ButtonHandler to support each JButton.
Steps 2-3
// declare & initialize 2 labels
// declare & initialize 6 button objects
These are the last of the initializations that should appear above the constructor TravlChk(). Be sure to use
the symbolic String constants from Step 0 to identify your JButton objects.
Step 11
// register the listener class for each JButton
The single instance of class ButtonHandler that was created at Step 1 should be registered with each JButton
that was instantiated at Step 3.
Code java:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TravlChk extends JFrame
{
private static final int WIDTH = 400, Height = 300;
private static final String
PESO_S = "Mexican peso",
FRANC_S = "Swiss franc",
EURO_S = "EURO dollar",
US_S = 'U.S. dollar",
CHECK_S = "Check Balance",
EXIT_S = "Exit";
PESO_SHandler pbHandler = new PESO_SHandler();
FRANC_SHandler fbHandler = new FRANC_SHandler();
EURO_SHandler obHandler = new EURO_SHandler();
US_SHandler ubHandler = new US_SHandler();
CHECK_SHandler cbHandler = new CHECK_SHandler();
EXIT_SHandler ebHandler = new EXIT_SHandler();
JLabel cashL = new JLabel("Cash Your Traveler's Check"' SwingConstants.RIGHT);
JLabel clickL = new JLabel("Click the button that matches your currency type", SwingConstants.RIGHT);
JButton PESO_S = new JButton("Mexican peso");
JButton*FRANC_S = new Jbutton("Swiss franc");
JButton EURO_S = new JButton("Euro dollar");
JButton US_S = new JButton("U.S. dollar");
JButton CHECK_S = new JButton("Check Balance");
JButton EXIT_S = new JButton("Exit");
Public TravlChk()
{
Container pane = getContentPane();
pane.setLayout(new GridLayout(8,1));
pane.add(cashL);
pane.add(clickL);
pane.add(PESO_S);
pane.add(FRANC_S);
pane.add(EURO_S);
pane.add(US_S);
pane.add(CHECK_S);
pane.add(EXIT_S);
setTItle("Traveler'sCheck Machine);
setSize(400,300);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
PESO_S.addActionListener(pbHandler);
FRANC_S.addActionListener(fbHandler);
EURO_S.addActionListener(obHandler);
US_S.addActionListener(ubHandler)
CHECK_S.addActionListener(cbHandler);
EXIT_S.addActionListener(ebHandler);
}
if (e.getActionCommand().equals(EXIT_S))
System.exit(0);
public static void main(String[] args)
{
TravlChk tc = new TravlChk();
}
}
//My brackets for the public class (the first and last) wont match up but when I put a bracket before the public static void main it ill match up with the open bracket for the class and close it. any idea why it is doing that? And are my buttons, button handlers, and action listeners correctly coded?
Re: Need help with button handlers
You may want to show us more code and describe more context. You are assuming that we know what the rest of your code looks like and what it's doing, and that prevents us from being able to give intelligent advice.
Re: Need help with button handlers
Try to edit the code to be properly indented, and do something about the extra commas near the top. They do not belong there.
Once the indentation is corrected it will be easier to fix the brackets.
Re: Need help with button handlers
Thanks for posting your code.
I agree with jps: properly formatted code is much easier to debug and understand than improperly formatted code. The formatting is not for the compiler's benefit, since it doesn't care what formatting you use. It is for YOUR benefit so that you can better understand and fix your code. So put some effort into your code formatting and in particular the indentation so that it is uniform and logical, and both your mistake will become obvious, and the dividends will be enormous.
Re: Need help with button handlers
Thanks guys, the problem was that I had an extra parenthesis in the pane.add section which didn't let my brackets connect.
Re: Need help with button handlers
Glad you've got it solved and fully understand the power and importance of code formatting.