Results 1 to 3 of 3
Thread: Buttons and Method
- 05-03-2010, 02:19 AM #1
Member
- Join Date
- May 2010
- Posts
- 2
- Rep Power
- 0
Buttons and Method
This is my first GUI attempt. It's for a servo control program I am working on. When I call the method servo.move(10), it will move the servo to 10 degrees. I have this working on a text based program, but I am getting an error when I try with the GUI. The error is commented next to the line servoA.move(10). Not sure why it doesn't work. Thanks.
Java Code:import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class guiTest { private static final int FRAME_WIDTH = 800; private static final int FRAME_HEIGHT = 600; public static void main(String[] args) throws Exception { JFrame frame = new JFrame(); // Create new servo final servoBackbone servoA = new servoBackbone(); // The button to trigger the calculation JButton increase = new JButton("Increase Angle"); JButton decrease = new JButton("Decrease Angle"); // The panel that holds the user interface components JPanel panel = new JPanel(); panel.add(increase); panel.add(decrease); frame.add(panel); class AddInterestListener implements ActionListener { public void actionPerformed(ActionEvent event) { servoA.move(10); //unreported exception java.lang.Exception; must be caught or declared to be thrown. } } ActionListener listener = new AddInterestListener(); increase.addActionListener(listener); decrease.addActionListener(listener); frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
- 05-03-2010, 03:35 AM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
The move(...) method throws an exception so you must catch it.
If you have this working in your text based program then see what you did there and do the same thing here.
I'm sure your text book has a section on "exception handling".
- 05-03-2010, 04:09 AM #3
Member
- Join Date
- May 2010
- Posts
- 2
- Rep Power
- 0
Gotcha. Thanks. I was just reading the book and saw an example using try and catch. I changed it to this and it works. Still not sure how these exceptions work. I took a programming class at school never got to learning GUI's or exceptions. So I'm trying to learn.
Java Code:try { servoA.move(10); } catch (Exception e){ System.err.println(e.toString()); }
Similar Threads
-
generating 26 buttons
By yasmin k in forum AWT / SwingReplies: 4Last Post: 11-08-2009, 02:11 PM -
Where To Get Pop Up Buttons
By hitmen in forum AWT / SwingReplies: 7Last Post: 03-26-2009, 04:05 AM -
Demonstration of Buttons
By Java Tip in forum SWTReplies: 0Last Post: 07-11-2008, 04:44 PM -
How to use SWT Buttons
By Java Tip in forum SWTReplies: 0Last Post: 07-11-2008, 04:44 PM -
Next and Previous Buttons
By JavaNewb in forum New To JavaReplies: 1Last Post: 05-09-2008, 01:23 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks