Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Need help figuring out the problem (GUI)

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Need help figuring out the problem (GUI)

    Hey guys this is my first time here. I was recommended by my friends to visit this site should i have any problems with Java programming and
    here i am and i need some help regarding my codes. I cant seem to figure out whats wrong with it and can't get the desired outcome.

    Desired outcome:
    DMIT bookstore.JPG


    My code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.border.*;
    /**
     *
     * @author Hui Jian
     */
    public class BookFrame extends JFrame implements ActionListener{
     
        private JLabel titlelbl,authorlbl,pricelbl,booknamelbl;
     
        private JButton bookBtn1,bookBtn2,bookBtn3,bookBtn4,bookBtn5,bookBtn6,bookBtn7,bookBtn8,bookBtn9;
     
        private ImageIcon book1,book2,book3,book4,book5,book6,book7,book8,book9;
        private JButton prevBtn,nextBtn,exitBtn,searchBtn;
     
        private JTextField titleTxt,authorTxt,priceTxt,searchTxt;
     
        private JPanel northPanel,centerPanel,southPanel;
     
        private JRadioButton programmingBtn,politicsBtn,scifiBtn;
     
        private int count = 0;
     
     
        public BookFrame(){
     
     
            titlelbl = new JLabel ("The Bookstore");
            authorlbl = new JLabel ("Author:");
            pricelbl = new JLabel ("Price:");
            booknamelbl = new JLabel ("Title:");
     
            searchBtn = new JButton ("Search");        
            prevBtn = new JButton ("Previous");
            nextBtn = new JButton ("Next");
            exitBtn = new JButton ("Exit");
     
     
            titleTxt = new JTextField(7);
            authorTxt = new JTextField(7);
            priceTxt = new JTextField(7);
            searchTxt = new JTextField(7);
     
            programmingBtn = new JRadioButton ("Programming");
            politicsBtn = new JRadioButton ("Politics");
            scifiBtn = new JRadioButton ("Science Fiction");
     
            northPanel = new JPanel ();
            centerPanel = new JPanel ();
            southPanel = new JPanel ();
     
            bookBtn1 = new JButton();
            book1 = new ImageIcon("book1.jpg");
            bookBtn1.setIcon(book1);
     
            bookBtn2 = new JButton();
            book2 = new ImageIcon("book2.jpg");
            bookBtn2.setIcon(book2);
     
            bookBtn3 = new JButton();
            book3 = new ImageIcon("book3.jpg");
            bookBtn3.setIcon(book3);
     
            bookBtn4 = new JButton();
            book4 = new ImageIcon("book4.jpg");
            bookBtn4.setIcon(book4);
     
            bookBtn5 = new JButton();
            book5 = new ImageIcon("book5.jpg");
            bookBtn5.setIcon(book5);
     
            bookBtn6 = new JButton();
            book6 = new ImageIcon("book6.jpg");
            bookBtn6.setIcon(book6);
     
            bookBtn7 = new JButton();
            book7 = new ImageIcon("book7.jpg");
            bookBtn7.setIcon(book7);
     
            bookBtn8 = new JButton();
            book8 = new ImageIcon("book8.jpg");
            bookBtn8.setIcon(book8);
     
            bookBtn9 = new JButton();
            book9 = new ImageIcon("book9.jpg");
            bookBtn9.setIcon(book9);
     
            centerPanel.add(bookBtn1);
            centerPanel.add(bookBtn2);
            centerPanel.add(bookBtn3);
            centerPanel.add(bookBtn4);
            centerPanel.add(bookBtn5);
            centerPanel.add(bookBtn6);
            centerPanel.add(bookBtn7);
            centerPanel.add(bookBtn8);
            centerPanel.add(bookBtn9);
     
            centerPanel.setLayout(new GridLayout(3,3));
     
            northPanel.add(titlelbl);
            northPanel.add(programmingBtn);
            northPanel.add(politicsBtn);
            northPanel.add(scifiBtn);
     
     
            southPanel.add(booknamelbl);
            southPanel.add(titleTxt);
            southPanel.add(authorlbl);
            southPanel.add(authorTxt);
            southPanel.add(pricelbl);
            southPanel.add(priceTxt);
            southPanel.add(searchBtn);
            southPanel.add(searchTxt);
            southPanel.add(prevBtn);
            southPanel.add(nextBtn);
            southPanel.add(exitBtn);
            southPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
     
            add(southPanel,BorderLayout.SOUTH);
            add(northPanel,BorderLayout.NORTH);
            add(centerPanel,BorderLayout.CENTER);

    Can you tell me where i went wrong? Your aid would be much appreciated. Thanks!


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Need help figuring out the problem (GUI)

    What happens instead of the desired outcome?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help figuring out the problem (GUI)

    where i went wrong?
    The end of the source is missing.

  4. #4
    Junior Member
    Join Date
    Aug 2011
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Need help figuring out the problem (GUI)

    Umm no. Those are just part of the codes. What i am showing you there are the codes that are causing problems.

    This is the full one:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.border.*;
    /**
     *
     * 
     */
    public class BookFrame extends JFrame implements ActionListener{
        private JLabel titlelbl,authorlbl,pricelbl,booknamelbl;
        private JButton bookBtn1,bookBtn2,bookBtn3,bookBtn4,bookBtn5,bookBtn6,bookBtn7,bookBtn8,bookBtn9;
        private ImageIcon book1,book2,book3,book4,book5,book6,book7,book8,book9;
        private JButton prevBtn,nextBtn,exitBtn,searchBtn;
        private JTextField titleTxt,authorTxt,priceTxt,searchTxt;
        private JPanel northPanel,centerPanel,southPanel;
        private JRadioButton programmingBtn,politicsBtn,scifiBtn;
        private int count = 0;
     
     
        public BookFrame(){
     
     
            titlelbl = new JLabel ("The Bookstore");
            authorlbl = new JLabel ("Author:");
            pricelbl = new JLabel ("Price:");
            booknamelbl = new JLabel ("Title:");
     
            searchBtn = new JButton ("Search");        
            prevBtn = new JButton ("Previous");
            nextBtn = new JButton ("Next");
            exitBtn = new JButton ("Exit");
     
     
            titleTxt = new JTextField(7);
            authorTxt = new JTextField(7);
            priceTxt = new JTextField(7);
            searchTxt = new JTextField(7);
     
            programmingBtn = new JRadioButton ("Programming");
            politicsBtn = new JRadioButton ("Politics");
            scifiBtn = new JRadioButton ("Science Fiction");
     
            northPanel = new JPanel ();
            centerPanel = new JPanel ();
            southPanel = new JPanel ();
     
            bookBtn1 = new JButton();
            book1 = new ImageIcon("book1.jpg");
            bookBtn1.setIcon(book1);
     
            bookBtn2 = new JButton();
            book2 = new ImageIcon("book2.jpg");
            bookBtn2.setIcon(book2);
     
            bookBtn3 = new JButton();
            book3 = new ImageIcon("book3.jpg");
            bookBtn3.setIcon(book3);
     
            bookBtn4 = new JButton();
            book4 = new ImageIcon("book4.jpg");
            bookBtn4.setIcon(book4);
     
            bookBtn5 = new JButton();
            book5 = new ImageIcon("book5.jpg");
            bookBtn5.setIcon(book5);
     
            bookBtn6 = new JButton();
            book6 = new ImageIcon("book6.jpg");
            bookBtn6.setIcon(book6);
     
            bookBtn7 = new JButton();
            book7 = new ImageIcon("book7.jpg");
            bookBtn7.setIcon(book7);
     
            bookBtn8 = new JButton();
            book8 = new ImageIcon("book8.jpg");
            bookBtn8.setIcon(book8);
     
            bookBtn9 = new JButton();
            book9 = new ImageIcon("book9.jpg");
            bookBtn9.setIcon(book9);
     
            centerPanel.add(bookBtn1);
            centerPanel.add(bookBtn2);
            centerPanel.add(bookBtn3);
            centerPanel.add(bookBtn4);
            centerPanel.add(bookBtn5);
            centerPanel.add(bookBtn6);
            centerPanel.add(bookBtn7);
            centerPanel.add(bookBtn8);
            centerPanel.add(bookBtn9);
     
            centerPanel.setLayout(new GridLayout(3,3));
     
            northPanel.add(titlelbl);
            northPanel.add(programmingBtn);
            northPanel.add(politicsBtn);
            northPanel.add(scifiBtn);
     
     
            southPanel.add(booknamelbl);
            southPanel.add(titleTxt);
            southPanel.add(authorlbl);
            southPanel.add(authorTxt);
            southPanel.add(pricelbl);
            southPanel.add(priceTxt);
            southPanel.add(searchBtn);
            southPanel.add(searchTxt);
            southPanel.add(prevBtn);
            southPanel.add(nextBtn);
            southPanel.add(exitBtn);
            southPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
     
            add(southPanel,BorderLayout.SOUTH);
            add(northPanel,BorderLayout.NORTH);
            add(centerPanel,BorderLayout.CENTER);
     
        }
        public void actionPerformed (ActionEvent r){
     
        }
     
        }
    Main method:

    import javax.swing.JFrame;
     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    /**
     *
     * 
     */
    public class BookFrameUser {
        public static void main(String[] args) {
            BookFrame e = new BookFrame();
            e.setTitle("Book Store");
            e.setSize(500,700);
            e.setVisible(true);
            e.setResizable(true);
            e.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
        }
     
    }

    You can ignore the actionlistener part because i added it in so that the program will run.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help figuring out the problem (GUI)

    the codes that are causing problems.
    Please explain what the problem is.

  6. #6
    Junior Member
    Join Date
    Aug 2011
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Need help figuring out the problem (GUI)

    As you can see from the desired outcome picture, i am supposed to write codes that will eventually lead to exactly the same thing as the picture.

    So basically when i run, many things like the textfield and labels are out of place and i need to figure out how to have them move to the next line.

    [ ] = textfield
    What i am having now on the frame is:

    Title:[ ] Author:[ ] Price: [ ] SearchBtn[ ] PreviousBtn NextBtn ExitBtn

    What i am trying to do:
    Title:[ ]
    Author:[ ]
    Price:[ ]
    SearchBtn[ ]
    PreviousBtn NextBtn ExitBtn

    Hope it helps.

  7. #7
    Junior Member
    Join Date
    Jul 2011
    Posts
    17
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Need help figuring out the problem (GUI)

    You're use wrong layoutManager. he's too simple to do what you want to do Try witch BoxLayout CardLayout or GroupLayout

  8. The Following User Says Thank You to remigio For This Useful Post:

    Prescott (August 6th, 2011)

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help figuring out the problem (GUI)

    What i am trying to do:
    Change the layout manager to one the puts the components in a grid.

  10. The Following User Says Thank You to Norm For This Useful Post:

    Prescott (August 6th, 2011)

  11. #9
    Junior Member
    Join Date
    Aug 2011
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Need help figuring out the problem (GUI)

    Ok thanks guys i got it all figured out. So wonder if i can ask another question here instead of making another thread?

    If so here goes,

    Assuming that i typed something into a textfield (searchTxt). So how do i use the text i wrote in searchTxt to find something in an array that is on another class?

  12. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help figuring out the problem (GUI)

    You need to get the String and the array together somewhere.
    Either call a method in the class with the array and pass it the String.
    Or call a method in the class with the array and have it return the array.
    Or have a method in the class with the array call the class with the String and have it return the String.

  13. The Following User Says Thank You to Norm For This Useful Post:

    Prescott (August 8th, 2011)

  14. #11
    Junior Member
    Join Date
    Aug 2011
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Need help figuring out the problem (GUI)

    Ahh thanks everyone for helping me i guess i am done here because i got it to fully function

Similar Threads

  1. Figuring Out Object Arrays
    By bengregg in forum Collections and Generics
    Replies: 2
    Last Post: April 5th, 2011, 05:55 AM
  2. Replies: 0
    Last Post: March 28th, 2010, 01:27 PM