Results 1 to 3 of 3
  1. #1
    imagiro1 is offline Member
    Join Date
    May 2010
    Posts
    2
    Rep Power
    0

    Default 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);
       }
    }

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

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

  3. #3
    imagiro1 is offline Member
    Join Date
    May 2010
    Posts
    2
    Rep Power
    0

    Default

    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

  1. generating 26 buttons
    By yasmin k in forum AWT / Swing
    Replies: 4
    Last Post: 11-08-2009, 02:11 PM
  2. Where To Get Pop Up Buttons
    By hitmen in forum AWT / Swing
    Replies: 7
    Last Post: 03-26-2009, 04:05 AM
  3. Demonstration of Buttons
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-11-2008, 04:44 PM
  4. How to use SWT Buttons
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-11-2008, 04:44 PM
  5. Next and Previous Buttons
    By JavaNewb in forum New To Java
    Replies: 1
    Last Post: 05-09-2008, 01:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •