error: expected with addActionListener class










0















I'm having issues with this simple block of Java code. I'm supposed to write an application that displays a JFrame containing the opening sentence from A Christmas Carol. When the user clicks the button, it will display the title of the book that contains the quote in the available JLabel using the setText() method.



However, when I compile, I'm getting errors that an is expected for my line to add the ActionListener for the button. I feel like I'm missing something simple, but I can't place it. Below is my source code. Thanks for any help.



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JBookQuote extends JFrame
FlowLayout flow = new FlowLayout();
JLabel msg1 = new JLabel("To begin with,");
JLabel msg2 = new JLabel("Marley was dead.");
JButton button = new JButton("Click for source");
JLabel msg3 = new JLabel();
String title = "** A Christmas Carol **";
public JBookQuote()
add(msg1);
add(msg2);
add(button);
add(msg3);
setLayout(flow);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


button.addActionListener(new ActionListener()
@Override
public void actionPerformed(ActionEvent e)

msg3.setText(title);

);
public static void main(String args)
JBookQuote aFrame = new JBookQuote();
aFrame.setSize(300, 150);
aFrame.setVisible(true);











share|improve this question






















  • button.addActionListener can't be executed outside of a executable block, like a constructor or method

    – MadProgrammer
    Nov 14 '18 at 23:54















0















I'm having issues with this simple block of Java code. I'm supposed to write an application that displays a JFrame containing the opening sentence from A Christmas Carol. When the user clicks the button, it will display the title of the book that contains the quote in the available JLabel using the setText() method.



However, when I compile, I'm getting errors that an is expected for my line to add the ActionListener for the button. I feel like I'm missing something simple, but I can't place it. Below is my source code. Thanks for any help.



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JBookQuote extends JFrame
FlowLayout flow = new FlowLayout();
JLabel msg1 = new JLabel("To begin with,");
JLabel msg2 = new JLabel("Marley was dead.");
JButton button = new JButton("Click for source");
JLabel msg3 = new JLabel();
String title = "** A Christmas Carol **";
public JBookQuote()
add(msg1);
add(msg2);
add(button);
add(msg3);
setLayout(flow);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


button.addActionListener(new ActionListener()
@Override
public void actionPerformed(ActionEvent e)

msg3.setText(title);

);
public static void main(String args)
JBookQuote aFrame = new JBookQuote();
aFrame.setSize(300, 150);
aFrame.setVisible(true);











share|improve this question






















  • button.addActionListener can't be executed outside of a executable block, like a constructor or method

    – MadProgrammer
    Nov 14 '18 at 23:54













0












0








0








I'm having issues with this simple block of Java code. I'm supposed to write an application that displays a JFrame containing the opening sentence from A Christmas Carol. When the user clicks the button, it will display the title of the book that contains the quote in the available JLabel using the setText() method.



However, when I compile, I'm getting errors that an is expected for my line to add the ActionListener for the button. I feel like I'm missing something simple, but I can't place it. Below is my source code. Thanks for any help.



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JBookQuote extends JFrame
FlowLayout flow = new FlowLayout();
JLabel msg1 = new JLabel("To begin with,");
JLabel msg2 = new JLabel("Marley was dead.");
JButton button = new JButton("Click for source");
JLabel msg3 = new JLabel();
String title = "** A Christmas Carol **";
public JBookQuote()
add(msg1);
add(msg2);
add(button);
add(msg3);
setLayout(flow);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


button.addActionListener(new ActionListener()
@Override
public void actionPerformed(ActionEvent e)

msg3.setText(title);

);
public static void main(String args)
JBookQuote aFrame = new JBookQuote();
aFrame.setSize(300, 150);
aFrame.setVisible(true);











share|improve this question














I'm having issues with this simple block of Java code. I'm supposed to write an application that displays a JFrame containing the opening sentence from A Christmas Carol. When the user clicks the button, it will display the title of the book that contains the quote in the available JLabel using the setText() method.



However, when I compile, I'm getting errors that an is expected for my line to add the ActionListener for the button. I feel like I'm missing something simple, but I can't place it. Below is my source code. Thanks for any help.



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JBookQuote extends JFrame
FlowLayout flow = new FlowLayout();
JLabel msg1 = new JLabel("To begin with,");
JLabel msg2 = new JLabel("Marley was dead.");
JButton button = new JButton("Click for source");
JLabel msg3 = new JLabel();
String title = "** A Christmas Carol **";
public JBookQuote()
add(msg1);
add(msg2);
add(button);
add(msg3);
setLayout(flow);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


button.addActionListener(new ActionListener()
@Override
public void actionPerformed(ActionEvent e)

msg3.setText(title);

);
public static void main(String args)
JBookQuote aFrame = new JBookQuote();
aFrame.setSize(300, 150);
aFrame.setVisible(true);








java swing jframe jbutton actionlistener






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 23:28









Thomas CThomas C

52




52












  • button.addActionListener can't be executed outside of a executable block, like a constructor or method

    – MadProgrammer
    Nov 14 '18 at 23:54

















  • button.addActionListener can't be executed outside of a executable block, like a constructor or method

    – MadProgrammer
    Nov 14 '18 at 23:54
















button.addActionListener can't be executed outside of a executable block, like a constructor or method

– MadProgrammer
Nov 14 '18 at 23:54





button.addActionListener can't be executed outside of a executable block, like a constructor or method

– MadProgrammer
Nov 14 '18 at 23:54












1 Answer
1






active

oldest

votes


















0














your code to add the actionlistener to your button isn't in a function, I think you want it in your constructor.






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53310301%2ferror-identifier-expected-with-addactionlistener-class%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    your code to add the actionlistener to your button isn't in a function, I think you want it in your constructor.






    share|improve this answer



























      0














      your code to add the actionlistener to your button isn't in a function, I think you want it in your constructor.






      share|improve this answer

























        0












        0








        0







        your code to add the actionlistener to your button isn't in a function, I think you want it in your constructor.






        share|improve this answer













        your code to add the actionlistener to your button isn't in a function, I think you want it in your constructor.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 23:33









        JustinJustin

        1,0431512




        1,0431512





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53310301%2ferror-identifier-expected-with-addactionlistener-class%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo