Results 1 to 4 of 4
  1. #1

    java borderlayout help

    Well I'm making this applet and I want to use BorderLayout to seperate some sections of my applet, but I only know how to do that with buttons...how can I do it with other things? When I try with other things I get a load of errors.

    My code is like this...
    public static void main ( String[] args )
    {
    //north
    JFrame frame = new JFrame("SFRPG");
    frame.setSize(400,400);
    frame.setTitle("Sonic Fighters Role Playing Game");
    frame.setVisible( true );

    }


    public void paint ( Graphics g )
    {
    //west
    Image saback;
    saback = getImage(getDocumentBase(), "saback.jpg");
    g.drawImage(saback,0,0,600,400,this);
    Image sfrpg;
    sfrpg = getImage(getDocumentBase(), "sfrpg.jpg");
    g.drawImage(sfrpg,50,50,200,200,this);
    g.drawString("SFRPG", 250, 10 );
    g.drawString("Click the close button",380, 10 );
    }

    Button logButton = new Button("Login");
    Button regButton = new Button("Register");


    public void init()
    {
    setLayout(null);
    setBackground(Color.blue);
    setForeground(Color.white);
    setSize(700,400);
    //east
    logButton.setSize(60,30);
    logButton.setBackground(Color.red);
    add(logButton);
    logButton.setLocation(600,100);
    regButton.setSize(60,30);
    regButton.setBackground(Color.red);
    add(regButton);
    regButton.setLocation(600,150);

    }
    //south
    //something else
    I marked where I want each section with //north, //south, //east, //west.
    If anyone can direct me to a tutorial that doesn't use buttons would be great.

  2. #2
    Join Date
    Jul 2003
    Location
    Kuwait
    Posts
    5,099
    You can add any component to a layout manager, not just buttons.

    You first need to assign a layout manager to your applet.

    Code:
    public void init() {
    setLayout(new BorderLayout());
    /* create your components here
    I've added some components*/
    mainMenuBar = new JMenuBar();
    mainToolBar = new JToolBar();
    		 
    JMenu fileMenu = new JMenu("File");
    JMenuItem logout = new JMenuItem("Logout");
    JMenuItem exit   = new JMenuItem("Exit");
    JMenuItem print  = new JMenuItem("Print");
    logout.setMnemonic('o');
    exit.setMnemonic('x');
    print.setMnemonic('P');
    fileMenu.setMnemonic('F');
    fileMenu.add(print);
    fileMenu.add(logout);
    fileMenu.add(exit);
    		 
    mainMenuBar.add(fileMenu);
    		 
    JLabel foo = new JLabel("Foo Label");
    JButton btn = new JButton("My Button");
    
    add("North",mainMenuBar);
    add("South",foo);
    add("East",btn);
    add("West",foo);
    add("Center",btn);
    }
    Hope this helps
    In order to understand recursion, one must first understand recursion.
    If you feel like it, you can read my blog
    Signal > Noise

  3. #3
    hmm I still don't understand...for the part of the code:

    add("North",mainMenuBar);
    add("South",foo);
    add("East",btn);
    add("West",foo);
    add("Center",btn);

    I put:

    add("North", logButton);
    add("South",regButton );
    add("East",logButton);
    add("West",saback);
    add("Center",sfrpg);

    and I get loads of errors...I still don't undestand.

  4. #4
    Join Date
    Jul 2003
    Location
    Kuwait
    Posts
    5,099
    What errors?
    In order to understand recursion, one must first understand recursion.
    If you feel like it, you can read my blog
    Signal > Noise

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •