Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JPanel positioning

807601Jan 26 2008 — edited Jan 26 2008
hi! Im quite new to java and i'm having some problems positioning certain panels in my container.
at the moment, the code creates a jlabel, some jradiobuttons and a jbutton. each is located on a different jpanel, which are put onto the borderlayout container, north, center and south, respectively.

although the code works, i dont really want the jlabel right at the top, id like it perhaps a few centimetres from the very top of the jframe.
also, id like the radiobuttons more towards the centre of the frame, and perhaps the submit button raised about a centimeter.

any idea on how to do this? do i need to use absolute positioning?

thanks for your help
torre
 
import java.awt.*;
import javax.swing.*;


public class langselect extends JFrame
                                {

 
    public langselect() {

        // Call super class constructor
       
        super("Torre's Happy Program");

        // create the layout
        
        Container contentPane = this.getContentPane();
        contentPane.setLayout(new BorderLayout());

       
        // create panel with text, add text to container
       
        JPanel textpanel = new JPanel();
        JLabel text = new JLabel("Welcome to Torre's Happy Program.  Please select your language.");
        textpanel.add(text);
        contentPane.add(textpanel, BorderLayout.NORTH);

        // create radio buttons
        
        JRadioButton eng = new JRadioButton ("English");
        JRadioButton fr = new JRadioButton ("Francais");
        
        // group ensures only one is selected
        
        final ButtonGroup group = new ButtonGroup();
        group.add(eng);
        group.add(fr);
        
        // add to buttons panel, then add to container
        
        JPanel radiopanel = new JPanel();
        radiopanel.add(eng);
        radiopanel.add(fr);
        contentPane.add(radiopanel, BorderLayout.CENTER);
        
        // now add a submit button to a panel, add panel to container
        
        JButton submit = new JButton("Submit");
        JPanel buttonpanel = new JPanel();
        buttonpanel.add(submit);
        contentPane.add(buttonpanel, BorderLayout.SOUTH);
    }


    /**
     * Sole entry point to the class and application.
     * @param args Array of String arguments.
     */
    public static void main(String[] args) {
        langselect mainFrame = new langselect();
        mainFrame.setSize(600,500);
        mainFrame.setVisible(true);
    }
   
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 23 2008
Added on Jan 26 2008
4 comments
546 views