Results 1 to 10 of 10
- 02-20-2010, 05:17 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 12
- Rep Power
- 0
so whats going on? (things aren't showing up)
I have the following code below, which is well commented and explains pretty much what it does. in theory it creates a window 800x600, puts "Sample Application" in the top, under the words (SOUTH i believe) creates a wrapper panel with a grid lay out of two, thus holding two more panels, one containing buttons and one containing a description of the buttons.
Now when run all that shows up is "Sample Application" why is that?
Java Code:package OtherGUI; import java.awt.BorderLayout; import java.awt.Font; import java.awt.GridLayout; import java.util.Vector; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.border.EtchedBorder; import javax.swing.border.TitledBorder; public class MainWindow { private Vector<JPanel> panel; /** * Call MainWindow which in turns calls the CreateWindow that contains * all the properties and contents of the entire application. * * @param args */ public static void main(String args[]) { new MainWindow(); } /** * Call the CreateWindow method that contains all the * contents and properties of the Frame. */ public MainWindow() { panel = new Vector<JPanel>(4); JPanel temp = CreateContent(); panel.add(temp); CreateWindow(); } /** * Create the basis of the frame, the size, and location on the * desktop of the application. populate the application with the * CreateContent() method * */ public void CreateWindow() { JFrame window = new JFrame("Main Window"); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setBounds(10, 10, 800, 600); JLabel title = new JLabel("Sample Application"); title.setFont(new Font("Serif", Font.PLAIN, 26)); window.add(title, BorderLayout.CENTER); CreateContent(); window.setVisible(true); } /** * Gather all the contents together and display * them with in a grid Layout. */ private JPanel CreateContent() { JPanel addButtons = Buttons(); //Gather all the conents JPanel contents = new JPanel(); contents.setLayout(new GridLayout(4,2)); contents.add(addButtons); return addButtons; } /** * Create a Panel specifically for Buttons. * in this case the four main buttons which are * Movies, Customer, Games and Home. * * @return - buttonHolder which is the JPanel * that contains the buttons */ private JPanel Buttons() { JPanel wrapper = new JPanel(); wrapper.setLayout(new GridLayout(2,4)); JButton home = new JButton("home"); JButton customer = new JButton("customer"); JButton movies = new JButton("movies"); JButton games = new JButton("games"); //des = description JTextArea desOne = new JTextArea("Sample Text"); desOne.setEditable(false); JTextArea desTwo = new JTextArea("Sample Text"); desTwo.setEditable(false); JTextArea desThree = new JTextArea("Sample Text"); desThree.setEditable(false); JTextArea desFour = new JTextArea("Sample Text"); desFour.setEditable(false); //desHolder = descriptionHolder JPanel desHolder = new JPanel(); desHolder.setLayout(new GridLayout(1, 4)); desHolder.add(desOne); desHolder.add(desTwo); desHolder.add(desThree); desHolder.add(desFour); //Hold all the buttons JPanel buttonHolder = new JPanel(); buttonHolder.setLayout(new GridLayout(1,4)); buttonHolder.add(home); buttonHolder.add(customer); buttonHolder.add(movies); buttonHolder.add(games); buttonHolder.setBorder(new TitledBorder(new EtchedBorder(), "Navagation")); /* * Hold both contents of Button Holder and * desHolder */ wrapper.add(buttonHolder); wrapper.add(desHolder); return buttonHolder; } }
- 02-20-2010, 05:42 PM #2
The CreateContent method returns a JPanel which you want to add to your gui.
Java Code:public void CreateWindow() { ... window.add(title, BorderLayout.NORTH); window.add(CreateContent(), BorderLayout.CENTER); ...
-
Is this your code or someone else's? If someone else's, what is your source?
As per hardwire, you are only adding the JLabel to the JFrame and nothing else.
- 02-20-2010, 05:53 PM #4
Member
- Join Date
- Feb 2010
- Posts
- 12
- Rep Power
- 0
Ok so that fixed that issue but upon running the code my theory is a mess, I have a etched border Called "Navigation" that does not contain or appear to contain the wrapper panel that splits the panel in two thus allowing there to be two panel win in located next to each other.
So in theory it should look something like:
{ [] [] }
where the curly braces are the wrapper and the two inner square brackets are the two panels known as buttonHolder and desHolder.
instead I get something like:
[Navagation]
which then takes up the whole center "pane"
what in my code could be causing this?
Because I add buttonHolder and desHolder to the wrapper to which has a 2,4 and button Holder has a 1,4 so contents should be put out like so:
wrapper
---------
[nav][des]
[b1 ][d1 ]
[b2 ][d2 ]
[b3 ][d3 ]
[b4 ][d4 ]
----------
you see where I am going with this?
So how would I fix this?
to the last poster above me:
my code, im trying to learn not rip off some one else work.
-
what JPanel does Button() return? the wrapper panel or the button holder panel?
-
Oh, I'm not trying to accuse you of ripping off anyone; it's just that the code looks very familiar, as if I recently saw a snippet of it either here or in the Sun fora. Heck, I borrow code all the time, often to study it and find out what's decent about it.
One other unsolicited suggestion: read up on Java naming conventions and use them whenever possible. By sticking to conventions, you make it much easier for others (including us!) to read and understand your code.
Again, much luck!
- 02-20-2010, 06:27 PM #7
Member
- Join Date
- Feb 2010
- Posts
- 12
- Rep Power
- 0
this was posted on sun fora but they locked my thread so i came here.
as for naming conventions I am just hacking this together for now, after I have this second issue Fixed I will deffently be renaming things, and fixing the readabillity
- 02-20-2010, 06:29 PM #8
Member
- Join Date
- Feb 2010
- Posts
- 12
- Rep Power
- 0
OMG i see now It returns ButtonHolder but should return wrapper >_<
but now the desHolder is out side navigation, the buttons take up all the space when they should be button size and the text areas are ok for now.
New code for Button();
Java Code:private JPanel Buttons() { JPanel wrapper = new JPanel(); wrapper.setLayout(new GridLayout(1,2)); JButton home = new JButton("home"); JButton customer = new JButton("customer"); JButton movies = new JButton("movies"); JButton games = new JButton("games"); //des = description JTextArea desOne = new JTextArea("Sample Text"); desOne.setEditable(false); JTextArea desTwo = new JTextArea("Sample Text"); desTwo.setEditable(false); JTextArea desThree = new JTextArea("Sample Text"); desThree.setEditable(false); JTextArea desFour = new JTextArea("Sample Text"); desFour.setEditable(false); //desHolder = descriptionHolder JPanel desHolder = new JPanel(); desHolder.setLayout(new GridLayout(4,1)); desHolder.add(desOne); desHolder.add(desTwo); desHolder.add(desThree); desHolder.add(desFour); //Hold all the buttons JPanel buttonHolder = new JPanel(); buttonHolder.setLayout(new GridLayout(1,4)); buttonHolder.add(home); buttonHolder.add(customer); buttonHolder.add(movies); buttonHolder.add(games); buttonHolder.setBorder(new TitledBorder(new EtchedBorder(), "Navagation")); /* * Hold both contents of Button Holder and * desHolder */ wrapper.add(buttonHolder); wrapper.add(desHolder); return wrapper; }Last edited by Adrien; 02-20-2010 at 06:34 PM.
-
-
The buttons are only doing what the layout managers are telling them to do. A GridLayout creates a grid of equal sized cells, so per your code, your buttons are guaranteed to be the same size as your text areas. Solution: don't rely on just GridLayout. Instead experiment with others. For example, try commenting out either createPanelA() or createPanelB() below (and uncommenting the other), to see what you're doing:
Java Code:import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.GridLayout; import javax.swing.*; public class LayoutEg { private static void createAndShowUI() { //!! comment out one of the two lines below: //JPanel mainPanel = createPanelA(); JPanel mainPanel = createPanelB(); JFrame frame = new JFrame("LayoutEg"); frame.getContentPane().add(mainPanel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } private static JPanel createPanelA() { JPanel buttonPanel = new JPanel(new GridLayout(1, 0)); for (int i = 0; i < 3; i++) { JButton button = new JButton("Button " + (i + 1)); buttonPanel.add(button); } JPanel mainPanel = new JPanel(new GridLayout(2, 0)); mainPanel.setPreferredSize(new Dimension(500, 300)); mainPanel.add(buttonPanel); mainPanel.add(new JLabel("Bottom Label", SwingConstants.CENTER)); return mainPanel; } private static JPanel createPanelB() { JPanel buttonPanel = new JPanel(new GridLayout(1, 0, 10, 0)); for (int i = 0; i < 3; i++) { JButton button = new JButton("Button " + (i + 1)); buttonPanel.add(button); } JPanel mainPanel = new JPanel(new BorderLayout(20, 20)); mainPanel.setPreferredSize(new Dimension(500, 300)); mainPanel.add(buttonPanel, BorderLayout.NORTH); mainPanel.add(new JLabel("Bottom Label", SwingConstants.CENTER), BorderLayout.CENTER); return mainPanel; } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } }
Similar Threads
-
Things you do if your not in Front of the Computer.
By Eku in forum Forum LobbyReplies: 28Last Post: 09-15-2010, 10:21 AM -
Program That Can Do Things On The Internet
By IronLegion in forum EclipseReplies: 1Last Post: 12-09-2009, 08:06 PM -
Move things in Applet
By Chasingxsuns in forum New To JavaReplies: 5Last Post: 07-25-2009, 02:16 AM -
A good Java installation package can make things much easier
By freezea in forum Reviews / AdvertisingReplies: 2Last Post: 02-01-2009, 12:03 PM -
May someone explain what these things means in Java
By quickfingers in forum New To JavaReplies: 2Last Post: 01-26-2008, 05:45 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks