Eclipse 不会显示菜单栏、菜单项或 JLabel

发布于 2024-10-21 01:57:39 字数 5100 浏览 2 评论 0原文

关于下面的代码,Eclipse在第72行到第86行附近给了我很多错误(框架无法解析),我也看不到标签或添加菜单栏,我认为所有问题都是相关的,如果有人请用更有经验的眼光来看待它。埃里克

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

    }
    }

regarding the code below, eclipse is giving me a lot of errors around line 72 to 86 (frame cannot be resolved) and i also can't see the label or add a menu bar, i think all issues are related and would appreciate if someone would cast a more experienced eye over it please. Eric

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

    }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天赋异禀 2024-10-28 01:57:39

无法识别frame,因为您从未声明过它。

JFrame frame = new JFrame ("Books");

frame = new JFrame ("Books");

但是,您确实扩展了 JFrame。您的意思是使用 this 而不是 frame 吗?

frame can't be identified because you never declared it.

JFrame frame = new JFrame ("Books");

vs.

frame = new JFrame ("Books");

You did, however, extend JFrame. Did you mean to use this instead of frame?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文