Link to home
Start Free TrialLog in
Avatar of Lambel
Lambel

asked on

Java

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

ASKER CERTIFIED SOLUTION
Avatar of rodness
rodness
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Lambel
Lambel

ASKER

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"?  
> 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("");
}
Avatar of Lambel

ASKER

Ok, I see. Thanks much!
Lynn
Avatar of Lambel

ASKER

thanks for the solution and explanation!
Lynn
:)
> thanks for the solution and explanation!

you marked the wrong comment as assisting :)