2011-03-08 86 views
0

,eclipse在第72行到第86行(框架無法解析)時給了我很多錯誤,而且我也看不到標籤或添加一個菜單欄,我認爲所有的問題都是相關的,如果有人會投入更有經驗的眼光,請稍等。埃裏克關於下面的代碼,Eclipse不會顯示菜單欄,菜單項或JLabel

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() 
    { 
     frame = new JFrame ("Books"); 
     Container contentpane = frame.getContentPane(); 

     JLabel label = new JLabel ("Book GUI"); 
     contentPane.add(label); 

     JMenuBar menubar = new JMenuBar(); 
     frame.setJMenuBar(menubar); 

     frame.pack(); 
     frame.setVisible(true); 

     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); 

    // Alows the class to quit. 
     //private void quit() { 
      //System.exit(0); 

    } 
    } 
+0

你在結尾處有很多註釋行。這些與這個問題真的有關嗎? – 2011-03-08 21:02:42

+0

艾薩克 - 沒有遺憾的意思是在我發佈前整理它... – Eric 2011-03-08 21:07:44

回答

1

frame無法識別,因爲您從未聲明它。

JFrame frame = new JFrame ("Books");

frame = new JFrame ("Books");

你沒有,但是,延長JFrame。您的意思是使用this而不是frame

+0

@伊薩克 - 感謝一堆,這真的讓我盯着我的臉,但我是新來的這個東西。第87行的文件菜單和第88行的conentPAne仍然說他們無法解析,爲什麼會這樣? – Eric 2011-03-08 21:07:08

+0

您聲明'contentpane'時小寫'p','fileMenu'永遠不會聲明。 – 2011-03-08 21:18:34

+0

謝謝,代碼現在運行沒有錯誤,但我有兩個GUI - 一個與我的按鈕和動作監聽器,另一個有一個標題'書籍',但沒有文件菜單 - 我想將標籤和文件菜單合併到我的GUI與按鈕,有什麼建議? – Eric 2011-03-08 21:28:14