Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: What is wrong with my code? Handling button events in eclipse standard 4.4? Java?

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post What is wrong with my code? Handling button events in eclipse standard 4.4? Java?

    Code:
    package button;
    import javax.swing.*;
    import java.awt.event.*;
    public class Actions extends JFrame implements ActionListener
    {
    JPanel pnl = new JPanel();
    public static void main (String[] args)
    {
    Actions gui = new Actions();
    }
    JButton btn1 = new JButton("Submit");
    JButton btn2 = new JButton("Cancel");
    JTextField txt1 = new JTextField("Message to:",38);
    JTextField tx1 = new JTextField("Subject:",38);
    JTextArea txt = new JTextArea("Essay:",5,37);
    JScrollPane pane = new JScrollPane(txt);
    public Actions()
    {
    super ("Button");
    setSize(500,250);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    add(pnl);
    setVisible(true);
    pnl.add(txt1);
    pnl.add(tx1);
    pnl.add(pane);
    pnl.add(btn1);
    pnl.add(btn2);
    btn2.setEnabled(false);
    btn1.addActionListener(this);
    btn2.addActionListener(this);
    if (event.getSource() == btn1)
    {btn2.setEnabled(true); btn1.setEnabled(false);}
    if (event.getSource() == btn2)
    {btn2.setEnabled(false); btn1.setEnabled(true);}
    }
    }



    Errors:
    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    The type Actions must implement the inherited abstract method ActionListener.actionPerformed(ActionEvent)
    event cannot be resolved
    event cannot be resolved

    at button.Actions.<init>(Actions.java:4)
    at button.Actions.main(Actions.java:9)


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,618
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: What is wrong with my code? Handling button events in eclipse standard 4.4? Java?

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.

    It looks like you left out some code for this program. See the tutorial for how to write listeners:
    Lesson: Writing Event Listeners (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,315
    My Mood
    Mellow
    Thanks
    194
    Thanked 669 Times in 656 Posts

    Default Re: What is wrong with my code? Handling button events in eclipse standard 4.4? Java?

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly per the above link and do not post multiple threads on the same topic. Your other thread was deleted.

Similar Threads

  1. Replies: 3
    Last Post: September 26th, 2013, 12:29 PM
  2. Problem netbeans gui button event handling
    By hiepa in forum AWT / Java Swing
    Replies: 7
    Last Post: December 3rd, 2012, 04:24 PM
  3. What is wrong with my code? JAVA-Eclipse
    By bespinoz in forum Loops & Control Statements
    Replies: 9
    Last Post: November 17th, 2012, 03:06 PM
  4. Replies: 2
    Last Post: August 12th, 2011, 05:59 PM
  5. Handling two button events?
    By BelowTheHeavens in forum AWT / Java Swing
    Replies: 2
    Last Post: May 15th, 2010, 01:35 AM