Results 1 to 4 of 4
  1. #1
    cc11rocks is offline Member
    Join Date
    Jan 2011
    Posts
    49
    Rep Power
    0

    Default One error, Buttenlistener problems

    I have two files, "Risk.java" and "RiskApp.java". I had it working before I tried to add the ButtenListeners and things of that nature. I was wondering what I am doing wrong? It is giving me one error
    Risk.java:
    Java Code:
    import javax.swing.*;
    
    public class Risk extends JPanel {
        public JButton enter;
    public JFormattedTextField stupid, yummy;
    public JLabel smart, dumb;
    public NumberFormat yucker = NumberFormat.getNumberInstance();
    public NumberFormat nucker = NumberFormat.getNumberInstance();
        public Risk() {
    JLabel smart = new JLabel("First Number:");
    add(smart);
    JFormattedTextField stupid = new JFormattedTextField(yucker);
    stupid.setColumns(3);
    add(stupid);
    boolean isDigit;
    JLabel dumb = new JLabel("Second Number:");
    add(dumb);
    JFormattedTextField yummy = new JFormattedTextField(nucker);
    yummy.setColumns(3);
    add(yummy);
            enter = new JButton("Enter");
     add(enter);
    [COLOR="Blue"] [B][I][U]enter.addActionListener(new ActionListener());[/U][/I][/B][I][U][/U][/I][/COLOR]
    	
        }
    class ButtonListener implements ActionListener {
    ButtonListener() {
    }
    public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand().equals("enter")) {
    System.out.println("Enter Button clicked");
    }
    }
    }
    }
    RiskApp.java:
    Java Code:
    import javax.swing.*;
    import java.text.NumberFormat;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.Toolkit;
    
    public class RiskApp {
    
        private static void createAndShowGUI() {
            JFrame frame = new JFrame("Risk");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Risk riskPanel = new Risk();
    class ButtonListener implements ActionListener {
    ButtonListener() {
    }
    public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand().equals("enter")) {
    Toolkit.getDefaultToolkit().beep();
    }
    }
    }
            frame.getContentPane().add(riskPanel);
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }
    The bolded, italicized, blued, and underlined line is the line that is giving me trouble. Here is the error code that the java compiler spits out at me. I use cmd prompt to compile and run.
    Java Code:
    Risk.java:31: java.awt.event.ActionListener is abstract; cannot be instantiated enter.addActionListener(new ActionListener());
    the carrot is pointing to the "n" in "new".
    If someone could help me out that would be great.
    Thanks,
    cc11rocks
    Last edited by cc11rocks; 02-06-2011 at 08:56 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    How about changing
    Java Code:
    enter.addActionListener(new ActionListener());

    to

    Java Code:
    enter.addActionListener(new ButtonListener());

    new ActionListener(); won't work since you can't instantiate an interface. But ButtonListener is a concrete class that implements ActionListener and was created specifically as your button's listener, right?

  3. #3
    cc11rocks is offline Member
    Join Date
    Jan 2011
    Posts
    49
    Rep Power
    0

    Default Thank you!!!

    Thank you very much. I was away from my computer all day so that is why I didn't respond.
    Thanks,
    cc11rocks

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. InputStream/Jar Problems/File IO Problems
    By rdjava in forum Advanced Java
    Replies: 31
    Last Post: 01-17-2011, 12:12 PM
  2. Error Page in JSP Having Problems.
    By goldest in forum JavaServer Pages (JSP) and JSTL
    Replies: 6
    Last Post: 07-19-2010, 09:35 AM
  3. Replies: 1
    Last Post: 12-05-2009, 08:28 AM
  4. java.lang.Error: Unresolved compilation problems
    By jon80 in forum New To Java
    Replies: 0
    Last Post: 06-07-2009, 11:04 PM
  5. runtime error problems. please help!
    By RoadRunner in forum Forum Lobby
    Replies: 0
    Last Post: 04-21-2009, 06:03 PM

Posting Permissions

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