2011-03-10 13 views
0

在整理下面的Java gui代碼的過程中,我設法渲染文件/退出打開不可操作 - 我看不到任何錯誤,請提出任何建議?文件 - 退出不再在Java GUI中工作

import java.awt.FlowLayout; 
import java.awt.Container; 
import java.awt.event.ActionEvent; 
import javax.swing.*; 
import java.awt.*; 


import java.awt.event.ActionListener; 


public class BookGUI extends JFrame implements ActionListener 

{ 

    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 = 600; 
    public static final int HEIGHT = 90; 

    //Creates & displays a window of the class FlowLayoutDemo 
    public static void main(String[] args) 
    { 
     BookGUI gui = new BookGUI(); 
     gui.setVisible(true); 
    } 

    public void setTitle(String title) 
    { 
     this.title = title; 
    } 

    public void setAuthor(String author) 
    { 
     this.author = author; 
    } 

    public void setYear(int year) 
    { 
     this.year = year; 
    } 
    public void setPublisher(String publisher) 
    { 
     this.publisher = publisher; 
    } 

    public void setCost(double cost) 
    { 
     this.cost = cost; 
    } 

    public BookGUI() 
    { 
     JFrame frame = this; 

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

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

     JMenu fileMenu = new JMenu("File"); 
     menubar.add(fileMenu); 
     JMenuItem quitItem = new JMenuItem("Quit"); 
     fileMenu.add(quitItem);; 

     setSize(WIDTH, HEIGHT); 
     addWindowListener(new WindowDestroyer()); 
     setTitle("GUI Assignment"); 
     Container content = getContentPane(); 

     content.setLayout(new FlowLayout()); 

     JButton button1 = new JButton("Add Book"); 
     content.add(button1); 
     button1.addActionListener(this); 

     JButton button2 = new JButton("Hightest Price Paid"); 
     content.add(button2); 
     button2.addActionListener(this); 

     JButton button3 = new JButton("Cost of BookShelf"); 
     content.add(button3); 
     button3.addActionListener(this); 

     JButton button4 = new JButton("Size of BookShelf"); 
     content.add(button4); 
     button4.addActionListener(this); 

     } 

    public void actionPerformed(ActionEvent e) 
    { 

     if (e.getActionCommand().equals("Add Book")) 

     {  
     title = JOptionPane.showInputDialog("Title"); 
     author = JOptionPane.showInputDialog("Author"); 
     publisher = JOptionPane.showInputDialog("Publisher"); 

     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 
     { 
      if (e.getActionCommand().equals("Hightest Price Paid")) 
      {   
       JOptionPane.showMessageDialog(this, "This facility is not currently available"); 
        } 
      else 
      { 
       System.out.println("Error!"); 
      } 
      // 
     } 
     //else 
     { 
    // Alows the class to quit. 
     //System.exit(0); 
     } 




    } 
    } 
+0

你是什麼意思的「無法操作」 – Tobias 2011-03-10 15:06:17

+0

開始刪除不必要的代碼部分,如JButton初始化。那麼你可能會更清楚地看到問題。 – 2011-03-10 15:07:34

+0

@tobiask - 我的意思是,如果我選擇文件,並退出它不會做任何事情 – Neil 2011-03-10 15:11:18

回答

3

你忘了動作監聽器添加到JMenuItem表示退出。 有點像這樣:

quitItem.addActionListener(this); 

然後在onActionPerformed添加適當的代碼。

2

也許這就是原因:

//System.exit(0); 

:-)

+0

謝謝,但我已經試過取消註釋,它仍然無法工作.. – Neil 2011-03-10 15:10:14

+0

@尼爾,以及它不是唯一的問題,但我可以告訴你,除非你取消該行的註釋,否則肯定無法工作;-) – 2011-03-10 17:01:36