Creating Java Panels with BorderLayout

  • Java
  • Thread starter Chrono
  • Start date
  • Tags
    Java
In summary: NO1HW2() {...}In summary, the problem is that the code to setup the JFrame is in a method, ShowBorderLayout(), which is never called. You should either call it from main() after creating the NO1HW2 object, or you should move the setup code into the class's constructor.
  • #1
Chrono
425
2
Ok, guys, I need some help here. This was done using Java, by the way. One of the homework questions we had a while ago was to create a frame and within the frame create two panels and on the two panels create three buttons using BorderLayout. Here's what I had:

import javax.swing.*;
import java.awt.*;

public class NO1HW2 extends JFrame
{
public void ShowBorderLayout()
{
Container mycontainer = getContentPane();

mycontainer.setLayout(new BorderLayout(10,10));

JPanel p1 = new JPanel();

for(int i = 1; i <= 3; i++)
p1.add(new JButton("Button " + i));

JPanel p2 = new JPanel();

for(int i = 4; i <= 6; i++)
p2.add(new JButton("Button " + i));

mycontainer.add(p1, BorderLayout.CENTER);

mycontainer.add(p2, BorderLayout.SOUTH);
}


public static void main(String args[])
{
NO1HW2 frame = new NO1HW2();
frame.setTitle("Show BorderLayout");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setVisible(true);

}
}

When I run the program the frame will pop up but that's it. The panels or buttons won't show. I asked my professor what the problem was and after making sure I set the frame visible to true he didn't know what else was wrong. Do y'all see anything that can cause it to not show the buttons?
 
Technology news on Phys.org
  • #2
I'm not really up to date with Java (I parted ways with it before Java 1.2), but why are you writing your code in the ShowBorderLayout method? I don't even know if it's called automatically or not. Try moving all that code into the main method, before the call to setVisible.
 
  • #3
I think you started from a program which started something like this

import javax.swing.*;
import java.awt.*;

public class ShowBorderLayout extends JFrame
{
public ShowBorderLayout ()
{


and then changed the name of the class. The ShowBorderLayout () is the constructor and has to have the same name as the class.
 
  • #4
chronon,

No. The name of the class is NO1HW2, and ShowBorderLayout() is a public member method of the class. The class has a default constructor.

Chrono,

The problem is that your code to setup the JFrame is in a method, ShowBorderLayout(), which is never called. You should either call this method from main() after creating the NO1HW2 object, or you should move the setup code into the class's constructor, which would be declared as simply "public NO1HW2() {...}"

- Warren
 
  • #5
Greetings all, and permit me to shed some light on this issue. There is no default method called ShowBorderLayout within Swing that one could try to override in this manner. Setting that hypothesis aside, this is probably a copy-and-paste error where the name of the class was changed, but not the name of the constructor, as chronon suggested. My guess is that the old constructor originally failed to compile (since constructors do not return anything), and the return type was added to void.

In fact, a Google search quickly reveals some candidates for the original class:
http://cspeech.ucd.ie/~fred/teaching/oldcourses/cs1001/lectures/ShowBorderLayout.java
http://www.utdallas.edu/~csteinh/CS%201337/examples/ShowBorderLayout.java

... and possibly others. There are any numbers of solutions, some more elegant than others. The method can be called explicitly from main , eg
NO1HW2 frame = new NO1HW2();
frame.ShowBorderLayout();
...
... but having a separate class method for this task seems superfluous - unless of course it would be too costly to perform this operation every time an object is generated, which is not the case here. The ShowBorderLayout() method can be converted to the class constructor (chroot), or the code in the method can be moved into the main method (zefram_c). For my part, I would go along with chroot's approach as it seems the most natural way to encapsulate the code. Farewell.
 
Last edited by a moderator:
  • #6
chroot said:
The problem is that your code to setup the JFrame is in a method, ShowBorderLayout(), which is never called. You should either call this method from main() after creating the NO1HW2 object, or you should move the setup code into the class's constructor, which would be declared as simply "public NO1HW2() {...}"

I thought it was called from the line "NO1HW2 frame = new NO1HW2();".
 
  • #7
Chrono said:
I thought it was called from the line "NO1HW2 frame = new NO1HW2();".
What makes you think that? The ShowBorderLayout() method is not a standard method or anything -- it's just one you made up. It won't be called unless you explicitly call it.

- Warren
 
  • #8
chroot said:
What makes you think that? The ShowBorderLayout() method is not a standard method or anything -- it's just one you made up. It won't be called unless you explicitly call it.

I guess I should have realized that since it wasn't working. How would I call it?
 
  • #9
As has been said already, you can either call it from main as "frame.ShowBorderLayout()", or by calling it explicitly from the NO1HW2 class's constructor, like so:

PHP:
public NO1HW2() {
   ShowBorderLayout();
}

- Warren
 
  • #10
How's this for a solution, Warren? Take out the void and rename the ShowBorderLayout() to NO1HW2().

import javax.swing.*;
import java.awt.*;

public class NO1HW2 extends JFrame
{
public void ShowBorderLayout()
{
}
 
  • #11
Yes, Chrono, that would do fine. All you'd be doing is putting that code into the NO1HW2's constructor, rather than in a method called "ShowBorderLayout()." The code in the constructor will be run automatically when the NO1HW2 object is created.

- Warren
 
  • #12
chroot said:
Yes, Chrono, that would do fine. All you'd be doing is putting that code into the NO1HW2's constructor, rather than in a method called "ShowBorderLayout()." The code in the constructor will be run automatically when the NO1HW2 object is created.

And it works! Thanks, Warren, I appreciate your help with this.
 

Related to Creating Java Panels with BorderLayout

1. What is BorderLayout in Java and why is it used?

BorderLayout is a layout manager in Java that divides a container into five regions: north, south, east, west, and center. It is used to organize and position the components within a panel or frame in a specific way, allowing for a more structured and visually appealing user interface.

2. How do I create a Java panel with BorderLayout?

To create a panel with BorderLayout, you can use the setLayout() method and pass in new BorderLayout() as the parameter. This will set the layout of the panel to BorderLayout. You can then add components to the panel using the add() method, specifying the region where you want the component to be placed.

3. Can I have multiple components in one region of a BorderLayout?

No, each region in a BorderLayout can only hold one component at a time. If you want to add multiple components in one region, you can create another panel with a different layout and add it to the desired region.

4. How do I change the size of a component in a BorderLayout?

You can use the setPreferredSize() method to specify the desired size of a component in a BorderLayout. However, the final size of the component will also depend on the size of the container and the other components in the layout.

5. Are there any alternatives to using BorderLayout in Java?

Yes, there are other layout managers in Java such as FlowLayout, GridLayout, and GridBagLayout that offer different ways of organizing components within a container. The best layout manager to use will depend on the specific needs and design of your application.

Similar threads

  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
14
Views
3K
  • Programming and Computer Science
Replies
15
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
969
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
2
Views
4K
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top