Results 1 to 1 of 1
- 12-16-2012, 01:41 AM #1
Member
- Join Date
- Dec 2012
- Posts
- 3
- Rep Power
- 0
Issues with a GUI and buttons... and errors...
I'm having a (big) problem understanding how to add buttons to a GUI and add textfields into a newly opened window. Plus several other issues but right now I have a selection of error meesages coming from this code and I'm not sure how to fix them:
C:\Java Program Files\BookStore9.java:50: error: non-static method add(Component) cannot be referenced from a static context
add(labelIsbn);
^
C:\Java Program Files\BookStore9.java:52: error: non-static method add(Component) cannot be referenced from a static context
add(txtIsbn);
^
C:\Java Program Files\BookStore9.java:129: error: local variable addPanel is accessed from within inner class; needs to be declared final
addPanel.add(saveButton);
^
C:\Java Program Files\BookStore9.java:143: error: local variable addPanel is accessed from within inner class; needs to be declared final
addPanel.add(cancelButton);
^
C:\Java Program Files\BookStore9.java:154: error: local variable addPanel is accessed from within inner class; needs to be declared final
frame1.add(addPanel, BorderLayout.SOUTH); // add save and cancel button to frame
^
C:\Java Program Files\BookStore9.java:156: error: local variable addPanel is accessed from within inner class; needs to be declared final
frame1.add(addPanel, BorderLayout.CENTER);
^
and here is the code:
any suggestions onJava Code:import java.awt.*; import javax.swing.*; import java.awt.event.*; import javax.swing.text.*; import java.text.NumberFormat; import java.util.Locale; import java.util.Arrays; public class BookStore9 extends JFrame { //begin class BookStore static int bookIndex = 0; public static JTextArea prepareDisplay(Book myBookList, JTextArea myTextArea){ NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US); myTextArea.setText(""); myTextArea.append (myBookList.toString()); return myTextArea; } public static void main (String args[]) { //declares TextFields JTextField txtIsbn, txtTitle, txtPublisher, txtYear, txtPrice; //declares labels JLabel labelIsbn, labelTitle, labelPublisher, labelYear, labelPrice; //add panel for save and cancel buttons JPanel addPanel = new JPanel(); addPanel.setLayout(new GridLayout(1, 2, 3, 3)); //center area for the text fields under the add button section JPanel centerAddPanel = new JPanel(); centerAddPanel.setLayout(new BoxLayout(centerAddPanel, BoxLayout.Y_AXIS)); //attempt at making a buuton for the add new book window (found under the add button section) labelIsbn = new JLabel("ISBN:"); add(labelIsbn); txtIsbn = new JTextField(10); add(txtIsbn); //declares text area for display of book info final JTextArea textArea = new JTextArea(10,50); textArea.setText(""); textArea.setEditable(false); NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US); //book titles for array Book intro = new Book("0321498054","Introduction to Programming Java - Interdisciplinary Approach", "Robert Sedgewick & Kevin Wayne", 2007, "Addison-Wesley", 83.65); Book think = new Book("0139798099","Thinking in C++: Introduction to Standard C++", "Bruce Eckel", 2000, "Prentice Hall", 77.65); Book build = new Book("0136091814","Building Java Programs: A Back to Basics Approach", "Stuart Reges & Marty Stepp", 2010, "Addison-Wesley", 97.02); Book java = new Book("0596007736","Java in a Nutshell", "David Flanagan", 2005, "O'Reilly Media", 44.95); Book learn = new Book("1449319246","Learning Java", "Patrick Niemeyer & Daniel Leuck", 2013, "O'Reilly Media", 49.99); //array of books, with each book set to a specific array slot final Book [] bookList = new Book[5]; bookList[0] = intro; bookList[1] = think; bookList[2] = build; bookList[3] = java; bookList[4] = learn; //creates putton panel for next, previous, add ,delete, etc. JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new GridLayout(2, 4, 3, 3)); JButton firstButton = new JButton ("First"); buttonPanel.add(firstButton); firstButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e){ bookIndex = 0; prepareDisplay(bookList[bookIndex], textArea); } }); JButton prevButton = new JButton("Previous"); buttonPanel.add(prevButton); prevButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { bookIndex --; if (bookIndex == 0){ bookIndex = bookList.length -1; }prepareDisplay(bookList[bookIndex], textArea); } }); JButton nextButton = new JButton("Next"); buttonPanel.add(nextButton); nextButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { bookIndex ++; if (bookIndex == bookList.length - 1){ bookIndex = 0; } prepareDisplay(bookList[bookIndex], textArea); } }); JButton lastButton = new JButton("Last"); buttonPanel.add(lastButton); lastButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { bookIndex = (bookList.length - 1); //set index to the last item in the array prepareDisplay(bookList[bookIndex], textArea); } }); JButton addButton = new JButton("Add New Book"); buttonPanel.add(addButton); addButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JButton saveButton = new JButton("Save"); addPanel.add(saveButton); saveButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int dup = 0; //for (int i = 0; i < bookList[bookIndex]; i++){ // if (txtIsbn.getText().equals(bookList.getIsbn())){ // JOptionPane.showMessageDialog(null, "ISBN Already Exists.", "ERROR!", JOptionPane.ERROR_MESSAGE); // break; // }//end if //} //end for }//end else });;//end save button action JButton cancelButton = new JButton("Cancel"); addPanel.add(cancelButton); cancelButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //frame1.dispose(); }});//end cancel button action //build add button window JFrame frame1 = new JFrame("Book Information"); frame1.setVisible(true); frame1.setResizable(false); //frame1.add(logoPanel, BorderLayout.NORTH); //add logo frame1.add(addPanel, BorderLayout.SOUTH); // add save and cancel button to frame frame1.add(centerPanel, BorderLayout.CENTER); // add center panel to frameframe1.setDefaultCloseOperation(HIDE_ON_CLOSE); frame1.add(addPanel, BorderLayout.CENTER); frame1.pack(); }//end actionPerformed });//end JButton delButton = new JButton("Delete Book"); buttonPanel.add(delButton); delButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { bookIndex ++; //set index to the last item in the array prepareDisplay(bookList[bookIndex], textArea); } }); JLabel logoLabel = new JLabel (new ImageIcon("DukeLogosm.jpg")); JPanel logoPanel = new JPanel(); logoPanel.add(logoLabel); JPanel centerPanel = new JPanel(); centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS)); centerPanel.add(prepareDisplay(bookList[bookIndex], textArea)); //show the first employee JFrame frame = new JFrame(); frame.setLayout(new BorderLayout()); // set layout frame.add(logoPanel, BorderLayout.NORTH); //add logo frame.add(buttonPanel, BorderLayout.SOUTH); // add previous and next button to frame frame.add(centerPanel, BorderLayout.CENTER); // add center panel to frame frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); /* double total = 0; // for method Books Array for (int i = 0; i < bookList.length; i++ ) { //textArea.append("\n\nISBN: " + bookList[i].getIsbn() + "\nTitle: " + bookList[i].getTitle() + "\nAuthor: " + bookList[i].getAuthor() + "\nYear Published: " + bookList[i].getYear() + "\nPublisher: " + bookList[i].getPublisher() + "\nList Price: " + nf.format(bookList[i].getPrice())); total += bookList[i].getPrice(); //bookList = sortArray(bookList); } // end for textArea.append("\n\nTotal Cost of Inventory: " + nf.format(total)); */ //initialize the GUI window and set the parameters } //end main method //Method Book array sort public static Book[] sortArray (Book bookList[]) { String [] titles = new String[bookList.length]; //declares new array - titles Book [] sortedBooks = new Book[bookList.length]; //declares new array - sortedBooks for (int i = 0; i < bookList.length; i++){//adds all Title to titles array titles[i] = bookList[i].getTitle(); } // end for Arrays.sort(titles); //sorts titles array for (int i = 0; i < bookList.length; i++){ //Book Array for (int j = 0; j < titles.length; j++){ //Titles Array if (bookList[i].getTitle().equalsIgnoreCase(titles[j])) { sortedBooks[j] = bookList[i]; break; } //end if }//end for }//end for return sortedBooks; //returns to sortedBooks array } // end public book method }//end class BookStore class Book { private String isbn; private String title; private String author; private int year; private String publisher; private double price; private double total; public Book ( String isbn, String title, String author, int year, String publisher, double price ) { setIsbn (isbn); setTitle (title); setAuthor (author); setYear (year); setPublisher (publisher); setPrice (price); } public String getIsbn(){ return isbn; } public void setIsbn (String isbn){ this.isbn = isbn; } public String getTitle(){ return title; } public void setTitle (String title){ this.title = title; } public String getAuthor(){ return author; } public void setAuthor (String author){ this.author = author; } public int getYear(){ return year; } public void setYear (int year){ this.year = year; } public String getPublisher(){ return publisher; } public void setPublisher (String publisher){ this.publisher = publisher; } public double getPrice(){ return price; } public void setPrice (double price){ this.price = price; } public String toString(){ NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US); return "ISBN:" + "\t" + isbn + "\n" + "Title:" + "\t" + title + "\n" + "Author:" + "\t" + author + "\n" + "Year:" + "\t" + year + "\n" + "Publisher:" + "\t" + publisher + "\n" + "Price:" + "\t" + nf.format(price) + "\n\n"; } } //end class Book
1:fix the errors
2: how do I get the new window to open and display the buttons with new text fields.
Thank you
Similar Threads
-
Unsafe or unchecked errors in Command prompt issues
By zlloyd1 in forum New To JavaReplies: 1Last Post: 12-12-2012, 09:01 AM -
First Java Program-Compile Errors (errors are posted)-simple GUI
By cc11rocks in forum AWT / SwingReplies: 4Last Post: 01-04-2011, 12:36 AM -
errors or issues implementing EJB
By harish21 in forum Enterprise JavaBeans (EJB)Replies: 1Last Post: 04-07-2009, 09:53 AM -
What is the difference between Semantic Errors and Logical Errors?
By tlau3128 in forum New To JavaReplies: 3Last Post: 03-08-2009, 01:51 AM -
How to use SWT Buttons
By Java Tip in forum SWTReplies: 0Last Post: 07-11-2008, 04:44 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks