Java

Lambel
Lambel used Ask the Experts™
on
I'm a beginner Java programmer and I'm trying to get this code working, and I can't figure out why I can't get a main method installed so I can run it.  Can someone tell me why my main method won't compile??  The error message is:
"The constructor Book Review is undefined"

Thanks, Lynn
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class BookReview extends JFrame implements ActionListener {

	private JLabel titleLabel;
	private JTextField titleTxtFd;
	private JComboBox typeCmb;
	private ButtonGroup ratingGP;
	private JButton processBnt;
	private JButton endBnt;
	private JButton clearBnt;
	private JTextArea entriesTxtAr;
	private JRadioButton excellentRdBnt;
	private JRadioButton veryGoodRdBnt;
	private JRadioButton fairRdBnt;
	private JRadioButton poorRdBnt;
	private String ratingString;
	private final String EXCELLENT = "Excellent";
	private final String VERYGOOD = "Very Good";
	private final String FAIR = "Fair";
	private final String POOR = "Poor";
	String lineSeparator = System.getProperty("line.separator");

	
public BookReview(String name) {

		super(name);
		JPanel infoPanel = new JPanel();
		titleLabel = new JLabel("Title: ");
		infoPanel.add(titleLabel);
		titleTxtFd = new JTextField(20);
		infoPanel.add(titleTxtFd);
		typeCmb = new JComboBox();
		typeCmb.addItem("Novel");
		typeCmb.addItem("Short Stories");
		typeCmb.addItem("Essays");
		typeCmb.addItem("Poems");
		typeCmb.addItem("Biography");
		typeCmb.addItem("Travelogue");
		typeCmb.addItem("Recipes");
		infoPanel.add(typeCmb);
		getContentPane().add(infoPanel, BorderLayout.NORTH);

		JPanel ratingPanel = new JPanel();
		ratingGP = new ButtonGroup();
		excellentRdBnt = new JRadioButton("Excellent");
		veryGoodRdBnt = new JRadioButton("Very Good");
		fairRdBnt = new JRadioButton("Fair");
		poorRdBnt = new JRadioButton("Poor");
		ratingGP.add(excellentRdBnt);
		ratingGP.add(veryGoodRdBnt);
		ratingGP.add(fairRdBnt);
		ratingGP.add(poorRdBnt);
		ratingPanel.add(excellentRdBnt);
		ratingPanel.add(veryGoodRdBnt);
		ratingPanel.add(fairRdBnt);
		ratingPanel.add(poorRdBnt);
		getContentPane().add(ratingPanel, BorderLayout.WEST);

		JPanel actionPanel = new JPanel();
		processBnt = new JButton("process");
		endBnt = new JButton("end");
		clearBnt = new JButton("clear");
		actionPanel.add(processBnt);
		actionPanel.add(clearBnt);
		actionPanel.add(endBnt);
		getContentPane().add(actionPanel, BorderLayout.EAST);

		entriesTxtAr = new JTextArea(20, 20);
		getContentPane().add(entriesTxtAr, BorderLayout.SOUTH);

		pack();
		setVisible(true);

		processBnt.addActionListener(this);
		clearBnt.addActionListener(this);
		//(3 pts) hand write the code for registering the other 6 components
		//in the space below.  The 6 components are: endBnt, 
		//excellentRdBnt, veryGoodRdBnt, fairRdBnt, poorRdBnt,
		//and typeCmb.
		
		endBnt.addActionListener(this);
		excellentRdBnt.addActionListener(this);
		veryGoodRdBnt.addActionListener(this);
		fairRdBnt.addActionListener(this);
		poorRdBnt.addActionListener(this);
		typeCmb.addActionListener(this);

		
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent event) {
				System.exit(0);
			}
		});
		
}
			

	public void actionPerformed(ActionEvent event) {

		Object srcObj = event.getSource();

		if (srcObj == clearBnt) {
			titleTxtFd.setText("");
			entriesTxtAr.setText("");
		} else if (srcObj == endBnt) {
			System.exit(0);
		} else if (srcObj == processBnt) {
			entriesTxtAr.append(lineSeparator +
			titleTxtFd.getText() + "  " +
			(String) typeCmb.getSelectedItem()
			+ "  " + ratingString);
		} else if (srcObj == excellentRdBnt) {
			ratingString = EXCELLENT;
		} else if (srcObj == veryGoodRdBnt) {
			ratingString = VERYGOOD;
		} else if (srcObj == fairRdBnt) {
			ratingString = FAIR;
		} else if (srcObj == poorRdBnt) {
			ratingString = POOR;
		}
	}

	public static void main(String[] args) {
		JFrame frame = new BookReview();
	    frame.setTitle("Book Review");
	    frame.setSize(300, 150);
	    frame.setLocationRelativeTo(null); // Center the frame
	    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    frame.setVisible(true);

	}

	
	
	
	
	
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
This is because your constructor takes a String argument, and you didn't give it one.

Do this instead:
JFrame frame = new BookReview("Book Review");

Open in new window

Top Expert 2016
Commented:
You don't have an empty ctor. Try

    public static void main(String[] args) {
        BookReview frame = new BookReview("Book Review");
        frame.setSize(300, 150);
        frame.setLocationRelativeTo(null); // Center the frame
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

Open in new window

Author

Commented:
Thank you, CEHJ.  That worked.  I'm still trying to understand.  Do you mean that I was missing the string parameter? - which you set as "Book Review"?  
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

Mick BarryJava Developer
Top Expert 2010

Commented:
> public BookReview(String name) {

you constructor expects a title string to be passed to it

>             JFrame frame = new BookReview();

When you create your BookReview object, you don't pass any parameter

So you need to either pass the title

JFrame frame = new BookReview("My Title");

or:

add a constructor that has no parameters

public BookReview() {
    this("");
}

Author

Commented:
Ok, I see. Thanks much!
Lynn

Author

Commented:
thanks for the solution and explanation!
Lynn
Top Expert 2016

Commented:
:)
Mick BarryJava Developer
Top Expert 2010

Commented:
> thanks for the solution and explanation!

you marked the wrong comment as assisting :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial