Panels Using Java

  • Thread starter Chrono
  • Start date
  • #1
420
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?
 

Answers and Replies

  • #2
259
0
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
498
0
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
chroot
Staff Emeritus
Science Advisor
Gold Member
10,239
39
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
anti_crank
Science Advisor
128
0
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 [Broken]
http://www.utdallas.edu/~csteinh/CS%201337/examples/ShowBorderLayout.java [Broken]

... 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
420
2
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
chroot
Staff Emeritus
Science Advisor
Gold Member
10,239
39
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
420
2
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
chroot
Staff Emeritus
Science Advisor
Gold Member
10,239
39
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
420
2
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
chroot
Staff Emeritus
Science Advisor
Gold Member
10,239
39
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
420
2
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 Threads on Panels Using Java

Replies
4
Views
2K
  • Last Post
Replies
3
Views
1K
  • Last Post
Replies
11
Views
9K
  • Last Post
Replies
4
Views
2K
  • Last Post
Replies
4
Views
3K
  • Last Post
6
Replies
129
Views
22K
  • Last Post
Replies
22
Views
4K
  • Last Post
Replies
4
Views
3K
  • Last Post
Replies
6
Views
1K
  • Last Post
Replies
13
Views
6K
Top