Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Panels Using Java

  1. Sep 20, 2004 #1
    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?
     
  2. jcsd
  3. Sep 20, 2004 #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.
     
  4. Sep 21, 2004 #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.
     
  5. Sep 21, 2004 #4

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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
     
  6. Sep 21, 2004 #5

    anti_crank

    User Avatar
    Science Advisor

    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 1337/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
    ... 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.
     
  7. Sep 21, 2004 #6
    I thought it was called from the line "NO1HW2 frame = new NO1HW2();".
     
  8. Sep 21, 2004 #7

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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
     
  9. Sep 21, 2004 #8
    I guess I should have realized that since it wasn't working. How would I call it?
     
  10. Sep 21, 2004 #9

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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
     
  11. Sep 22, 2004 #10
    How's this for a solution, Warren? Take out the void and rename the ShowBorderLayout() to NO1HW2().

     
  12. Sep 22, 2004 #11

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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
     
  13. Sep 22, 2004 #12
    And it works! Thanks, Warren, I appreciate your help with this.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Panels Using Java
  1. Java's Scanner (Replies: 6)

  2. Java Help (Replies: 7)

  3. Java help (Replies: 15)

  4. Java q (Replies: 3)

  5. Java problem. (Replies: 4)

Loading...