0

Please help me with this assignment as well, because I am receiving a error when Running the application. The error I am receiving is:

Exception in thread "main" java.lang.Error: Do not use Stock.setLayout() use Sto
ck.getContentPane().setLayout() instead
at javax.swing.JFrame.createRootPaneException(JFrame.java:458)
at javax.swing.JFrame.setLayout(JFrame.java:524)
at Stock.<init>(Stock.java:50)
at Stock.main(Stock.java:36)
Press any key to continue . . .

Please help me to tell me what the problem is?

/*
   Chapter 8:	Programming Assignment 9
   Programmer:	T. du Preez
   Date:		September 20, 2009
   Filename:	Stock
*/

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
import javax.swing.*;

public class Stock extends JFrame implements ActionListener
{
	//Declare an input variable
   	DataInputStream input;

   	//Construct components
	JPanel fieldPanel = new JPanel();
	JPanel buttonPanel = new JPanel();
	JLabel stockLabel = new JLabel("Stock Name:");
	JLabel stock = new JLabel("                        ");
	JLabel volumeLabel = new JLabel("Volume");
	JLabel volume = new JLabel("                       ");
	JLabel priceLabel = new JLabel("Closing Price:");
	JLabel price = new JLabel("                        ");
	JLabel changeLabel = new JLabel("Change:");
	JLabel change = new JLabel("                       ");
	JButton next = new JButton("Next->");


	public static void main(String[] args)
	{
		Stock window = new Stock();
      	window.setTitle("Yesterday's 10 Hottest Stocks");
      	window.setSize(300, 175);
      	window.setVisible(true);
   	}

   	public Stock()
   	{
		//Set colors
	   	setBackground(Color.blue);
	   	setForeground(Color.white);
	   	next.setForeground(Color.black);

	   	//Set layout managers
	   	setLayout(new BorderLayout());
	   	fieldPanel.setLayout(new GridLayout(4,2));
	   	buttonPanel.setLayout(new FlowLayout());

     	//Add components and actionListener to interface
		fieldPanel.add(stockLabel);
		fieldPanel.add(stock);
		fieldPanel.add(volumeLabel);
		fieldPanel.add(volume);
		fieldPanel.add(priceLabel);
		fieldPanel.add(price);
		fieldPanel.add(changeLabel);
		fieldPanel.add(change);
		buttonPanel.add(next);
		add(fieldPanel, BorderLayout.NORTH);
		add(buttonPanel, BorderLayout.SOUTH);
		next.addActionListener(this);

     	try
     	{
     	   //Open the file
     	   input = new DataInputStream(new FileInputStream("hotStocks.dat"));
     	}
     	catch(IOException ex)
     	{
     	   closeFile();
     	}

     	//Construct window listener
     	addWindowListener(
        new WindowAdapter()
     	{
     	   public void windowClosing(WindowEvent e)
     	   {
			   closeFile();
           }
		}

	   );
	}

	public void actionPerformed(ActionEvent e)
	{
		try
		{
			stock.setText(input.readUTF());
			volume.setText(input.readUTF());
			price.setText(input.readUTF());
			change.setText(input.readUTF());
		}
  		catch(IOException e2)
  		{
			stock.setText("End of File");
			volume.setText("");
			price.setText("");
			change.setText("");
		}
	}

	public void closeFile()
	{
	      try
	      {
	         input.close();
	      }
	      catch(IOException c)
	      {
				System.exit(1);
	      }
				System.exit(0);
	}
}
2
Contributors
1
Reply
5
Views
7 Years
Discussion Span
Last Post by JamesCherrill
0

Like it says right at the top:
Do not use Stock.setLayout() use Stock.getContentPane().setLayout() instead
and...
at Stock.<init>(Stock.java:50)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.