Solved

Jave program has created two GUI's instead of one with combined appearance and function

Posted on 2011-03-08
4
228 Views
Last Modified: 2012-05-11
hi, regarding the attached code, i've made a mistake somewhere so instead of a single GUI with red background, a label and a file/quit option and my buttons with action listeners i have two GUI's one with the buttons and listeners and the other with a red background, any suggestions how to incorporate the two please?  i'm new to this and i'm obviously missing something fundamental.  thakns
import java.awt.FlowLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

import java.awt.event.ActionListener; 


public class BookGUI extends JFrame implements ActionListener

{
	

	//String addBook="";
	// public ArrayList<Book> books;
	
	//Book books = new Book ("", "", 0, "", 0);
	Book book = new Book("", "", 0, "", 0);
	String title  = "";
	String author  = "";
	int year = 0;
	String publisher  = "";
	double cost = 0;
	double total = 0;
	boolean goodInput = false;
	
		
	public BookShelf bookShelf = new BookShelf();
    public static final int WIDTH = 400;
    public static final int HEIGHT = 100;

    //Creates & displays a window of the class FlowLayoutDemo
    public static void main(String[] args)
    {
        BookGUI gui = new BookGUI( );
        gui.setVisible(true);
    }
    
   // public String getTitle()
   // {
    //    return title;
    //}
    
    public void setTitle(String title) //this is relevant
    {
        this.title = title;
    }
    
    public void setAuthor(String author) //this is relevant
    {
        this.author = author;
    }
    
    public void setYear(int year) //this is relevant
    {
        this.year = year;
    }
    public void setPublisher(String publisher) //this is relevant
    {
        this.publisher = publisher;
    }
    
    public void setCost(double cost) //this is relevant
    {
        this.cost = cost;
    }
    
    public BookGUI( )
    {
    	JFrame frame = new JFrame ("Books");

    	Container contentpane = frame.getContentPane();
    	
    	JLabel label = new JLabel ("Assignment One");
    	contentpane.add(label);
    	
    	JMenuBar menubar = new JMenuBar();
    	frame.setJMenuBar(menubar);
    	
    	frame.pack();
    	frame.setVisible(true);
    	
    	JMenu fileMenu = new JMenu("File");
    	JMenuItem quitItem = new JMenuItem("Quit");
    	fileMenu.add(quitItem);;
    	contentpane.setBackground(Color.red);
    	
    	setSize(WIDTH, HEIGHT);
        addWindowListener(new WindowDestroyer( ));
        setTitle("GUI Assignment");
        Container content = getContentPane( );

        content.setLayout(new FlowLayout());
             
        JButton button1 = new JButton("Hightest Price Paid");
        content.add(button1);
        button1.addActionListener(this);
        //contentPane.add(button1);
        
        JButton button2 = new JButton("Cost of BookShelf");
        content.add(button2);
        button2.addActionListener(this);
        
        JButton button3 = new JButton("Size of BookShelf");
        content.add(button3);
        button3.addActionListener(this);
        
        JButton button4 = new JButton("Add Book");
        content.add(button4);
        button4.addActionListener(this); 
        
     // Alows the class to quit.
        //private void quit() 
            //System.exit(0);
              
    }
         
    public void actionPerformed(ActionEvent e)
    {
    	
        if (e.getActionCommand().equals("Add Book"))
       //book = JOptionPane.showInputDialog("Add Book");
        {     //set up the book object with all the data passed in
        title = JOptionPane.showInputDialog("Title");
        author = JOptionPane.showInputDialog("Author");
        publisher = JOptionPane.showInputDialog("Publisher");
        //cost = JOptionPane.showInputDialog("Cost");
        //cost = Double.parseDouble(JOptionPane.showInputDialog("Cost"));
        do{
        try { 
        	cost = Double.parseDouble(JOptionPane.showInputDialog("Cost"));
        	book.setCost(cost);
        	goodInput = true;
        	} 
        catch (NumberFormatException nfe){          
        JOptionPane.showMessageDialog(this, "Numerical entry required. Please try again");
        	} 
        }while (!goodInput);
        	
        	
        book.setTitle(title);
        book.setAuthor(author);
        book.setPublisher(publisher);
       	bookShelf.addBook(book);
        	
        String message =  "The title of the book is :" + title + 
		"the Author of the Book is : " + author + " and it's published by " + publisher + "and it costs" + cost + "euro";
        JOptionPane.showMessageDialog(null, message, "Book Details", JOptionPane.PLAIN_MESSAGE);
        }
        else if (e.getActionCommand().equals("Size of BookShelf")) {
            int sizeOfBookShelf = bookShelf.sizeOfBookshelf();
            String message = "The book shelf has " + sizeOfBookShelf + " book(s)";
            JOptionPane.showMessageDialog(this, message);
        }
        else if (e.getActionCommand().equals("Cost of BookShelf")) 
        {
        	double costOfBookshelf = bookShelf.costOfBookShelf();
        	String message = "The book shelf value is " + total + costOfBookshelf + "Euro";
        	JOptionPane.showMessageDialog(this, message);
        }
        
        else
        {
        	System.out.println("Error!");
        }
        
        System.exit( 0 );

Open in new window

0
Comment
Question by:t38
  • 2
  • 2
4 Comments
 
LVL 92

Expert Comment

by:objects
ID: 35074152
>           JFrame frame = new JFrame ("Books");

change that to:


          JFrame frame = this;

public BookGUI( )
    {
    	JFrame frame = new JFrame ("Books");

    	Container contentpane = frame.getContentPane();
    	
    	JLabel label = new JLabel ("Assignment One");
    	contentpane.add(label);
    	
    	JMenuBar menubar = new JMenuBar();
    	frame.setJMenuBar(menubar);
    	
    	frame.pack();
    	frame.setVisible(true);
    	
    	JMenu fileMenu = new JMenu("File");
    	JMenuItem quitItem = new JMenuItem("Quit");
    	fileMenu.add(quitItem);;
    	contentpane.setBackground(Color.red);
    	
    	setSize(WIDTH, HEIGHT);
        addWindowListener(new WindowDestroyer( ));
        setTitle("GUI Assignment");
        Container content = getContentPane( );

        content.setLayout(new FlowLayout());
             
        JButton button1 = new JButton("Hightest Price Paid");
        content.add(button1);
        button1.addActionListener(this);
        //contentPane.add(button1);
        
        JButton button2 = new JButton("Cost of BookShelf");
        content.add(button2);
        button2.addActionListener(this);
        
        JButton button3 = new JButton("Size of BookShelf");
        content.add(button3);
        button3.addActionListener(this);
        
        JButton button4 = new JButton("Add Book");
        content.add(button4);
        button4.addActionListener(this); 
        
     // Alows the class to quit.
        //private void quit() 
            //System.exit(0);
              
    }

Open in new window

0
 

Author Comment

by:t38
ID: 35074315
Thanks - File/Quit is still missing though.
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 35074321
you don't add fileMenu to the menubar
0
 

Author Closing Comment

by:t38
ID: 35074453
Thanks for your assistance.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now