DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2005
    Posts
    1

    Question Probably simple...

    Hello there. Just starting off in Java, I have experience in other languages, and have read a Java book (and then forgot most of it).

    Anyways, I simply cannot figure out why this program won't work properly. When I try to execute it, it just closes before actually starting, but after compiling.

    Here's what I'm trying to do:
    Make a window with 2 panels, each with their own tabs.

    Right now tab2, called content, is just plain and is using nothign special, to be changed once I get the first one to work.

    If you have any other questions feel free to ask. Thanks in advance, and check the source code attached.
    Attached Files

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Some remarks

    Note: I've made it a JApplet, after all, you were using swing components
    inside an awt component and that is not a recommended practise...

    Code:
    // our imports
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class GUIBreasts
        extends JApplet {
      // creates the border
      BorderLayout myLayout = new BorderLayout();
    
      // right now this is static, well change after we get menu working
      Panel content = new Panel();
    
      // our menu
      private JTabbedPane menu;
    
      // our menu tabs
      //private Panel System; /// <======== NOOOOOO ! System is a java.lang basic 
                             // class, and you don't want to 'disable' it like this, 
                             // no way...
    
      private Panel ASystem;
      private Panel Games;
      private Panel Apps;
    
    
      /**
       * An applet constructor is useless, the browser will use the basic
       * parameterless constructor no matter what you do,  so the init()
       * method may be regarded as *the* applet constructor
       */
      public void init() { // <========= autoinvoked by browser after instantiation
        // sets the size... duh
        setSize(300, 200);
    
        // creates the panel
        Panel topPanel = new Panel();
        // sets the layout
        topPanel.setLayout(myLayout);
        // and then adds it
        add(topPanel);
    
        // adds a button to content as a control type thing
        content.add(new Button("Test2"));
    
        // setting up our menu
        addMenu();
    
        //add out 2 panels
        topPanel.add(menu, BorderLayout.WEST);
        topPanel.add(content, BorderLayout.CENTER);
    
      }
    
      public void addMenu() {
        // makes our menu tab
        menu = new JTabbedPane();
    
        // makes our first tab
        ASystem = new Panel();
        ASystem.add(new Button("Games"));
        menu.addTab("System", null, ASystem, "System");
    
        // and our second tab
        Panel Games = new Panel();
        Games.add(new Button("Games"));
        menu.addTab("Games", null, Games, "Games");
    
        // and third
        Panel Apps = new Panel();
        Apps.add(new Button("Applications"));
        menu.addTab("Applications", null, Apps, "Applications");
    
      }
      /**
       * Standalone addition.
       * An applet is a panel descendant, and making it visible has no effect 
       * whatsoever unless it is contained in some  kind of window 
       * (a Frame/JFrame or a browser) 
       * @param args
       */
      public static void main(String[] args) {
        // creates a type of this
        GUIBreasts gui = new GUIBreasts();
        Frame f=new Frame("Breasts ...! (pant! pant! pant!)");
        f.setLayout(new GridLayout());
        gui.init(); // no browser in this mode.... must do init() "manually"
        f.add(gui);
        // ensure proper termination
        f.addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            System.exit(0);
          }
        });
        // exectutes it
        f.setVisible(true);
      }
    }
    eschew obfuscation

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links