文件-退出不再在Java GUI中工作
在整理下面的java gui代码的过程中,我已设法使文件/退出打开无法操作-我看不出有任何错误,请提供任何建议文件-退出不再在Java GUI中工作,java,Java,在整理下面的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
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 );
}
}
}
也许这就是原因:
//System.exit( 0 );
:-)也许这就是原因:
//System.exit( 0 );
:-)您忘记将操作侦听器添加到退出的
JMenuItem
。
有点像这样:
quitItem.addActionListener(this);
然后在
onActionPerformed
中添加适当的代码。您忘记将操作侦听器添加到JMenuItem
以退出。
有点像这样:
quitItem.addActionListener(this);
然后在
onActionPerformed
中添加适当的代码。您所说的“不可操作”是什么意思?首先删除代码中不必要的部分,例如JButton初始化。那么您就可以更清楚地看到问题了。@tobiask-我的意思是,如果我选择file并退出它,它不会做任何事情。您所说的“不可操作”是指从删除代码中不必要的部分开始,例如JButton初始化。这样你就可以更清楚地看到这个问题了。@tobiask-我的意思是,如果我选择文件并退出,它就不行了anything@Neil,这不是唯一的问题,但我可以告诉您,在您取消注释该行之前,它肯定不会起作用;-)@尼尔,这不是唯一的问题,但我可以告诉你,在你取消注释那一行之前,它肯定不会起作用;-)