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.
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?