Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Can someone tell me what is wrong with my Java program?

I am using "Starting out with JAVA" by Tony Gaddis, and I'm putting in code directly from the book with my own alterations of course, otherwise it wouldn't be my own program. The way the book says to do it should technically be right, but when I try to build the program, it says that "public windows()" line needs a return type, but the book does not say to use return types for these.

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class Jokes extends JFrame

{

private JPanel panel;

private JButton button;

private JLabel joke;

public windows() //ERROR IS HERE

{

setTitle("Jokes!");

setSize(300, 500);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

add(panel);

buildPanel();

setVisible(true);

}

private void buildPanel()

{

button = new JButton("Get Answer");

button.addActionListener(new ButtonListener());

joke = new JLabel("Why did the chicken cross the road?");

panel = new JPanel();

panel.add(button);

panel.add(joke);

}

private class ButtonListener implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

JOptionPane.showMessageDialog(null, "Because it was actually a rooster, and not a chicken.");

}

}

public static void main(String[] args)

{

int answer = JOptionPane.showConfirmDialog(null, "Do you want to hear a joke?");

if(answer == JOptionPane.YES_OPTION)

new windows();

if(answer == JOptionPane.NO_OPTION)

JOptionPane.showMessageDialog(null, "Then why do you even bother to live?");

}

}

Update:

I have fixed the issue, I did have a main by the way... Take a look at what I put one more time.

1 Answer

Relevance
  • 1 decade ago
    Favorite Answer

    You left out the constructor. A constructor is a special method, must by 'public' and have the same name as the class. You have no main() either, required to start a program.

    public class Jokes extends JFrame

    {

    private JPanel panel;

    private JButton button;

    private JLabel joke;

    public Jokes() //ERROR IS HERE

    {

    this.setTitle("Jokes!");

    this.setSize(300, 500);

    this.setDefaultCloseOperation(JFrame.EXIT_O…

    panel = new buildPanel();

    add(panel, BorderLayout.CENTER);

    this.setLoacationRelativeTo( null ); // comment this out and see what you get

    this.setVisible(true);

    }

    private JPanel buildPanel()

    {

    panel = new JPanel();

    button = new JButton("Get Answer");

    button.addActionListener(new ButtonListener());

    joke = new JLabel("Why did the chicken cross the road?");

    panel = new JPanel();

    panel.add(button);

    panel.add(joke);

    return panel;

    }

    public static void main( String [] args {

    new Jokes(); // this calls the constructor

    }

    any time you see a word() that means sub-routine (or method) it will branch execute and return to the line after the call.

Still have questions? Get your answers by asking now.