1 Attachment(s)
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:
Attachment 676
My code:
Code Java:
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!
Re: Need help figuring out the problem (GUI)
What happens instead of the desired outcome?
Re: Need help figuring out the problem (GUI)
Quote:
where i went wrong?
The end of the source is missing.
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:
Code Java:
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:
Code Java:
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.
Re: Need help figuring out the problem (GUI)
Quote:
the codes that are causing problems.
Please explain what the problem is.
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.
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
Re: Need help figuring out the problem (GUI)
Quote:
What i am trying to do:
Change the layout manager to one the puts the components in a grid.
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?
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.
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 :)