Results 1 to 2 of 2
  1. #1
    new_2_java is offline Member
    Join Date
    Feb 2008
    Rep Power

    Default JFrame and SpringLayout.

    Hi all,

    I have a simple JFrame windows with two JLabel, two JTextField and two JButton. I use SpringLayout to display controls in on the frame. But when I run the application, the windows is displayed in a very small size. Only the two buttons are displayed. But when I maximize the window all the controls are placed properly.

    So, I think, the setSize() is not functioning properly. Can someone please tell me what i am doing wrong? Appreciate your help. Here's the code for this:

    Java Code:
    public class Login extends JFrame implements ActionListener {
    	public Login () {
    		this.getContentPane().setLayout(new BorderLayout());
    		JPanel btnPane = new JPanel();
    		btnPane.setLayout(new FlowLayout());
    		JPanel pane = new JPanel();
    		SpringLayout sp = new SpringLayout();
            sp.putConstraint(SpringLayout.WEST, lblUserName, 70, SpringLayout.WEST, this);
            sp.putConstraint(SpringLayout.NORTH, lblUserName, 20, SpringLayout.NORTH, this);
            sp.putConstraint(SpringLayout.WEST, txtUserName, 5, SpringLayout.EAST, lblUserName);
            sp.putConstraint(SpringLayout.NORTH, txtUserName, 20, SpringLayout.NORTH, this);
            sp.putConstraint(SpringLayout.WEST, lblPassword, 70, SpringLayout.WEST, this);
            sp.putConstraint(SpringLayout.NORTH, lblPassword, 5, SpringLayout.SOUTH, lblUserName);
            sp.putConstraint(SpringLayout.WEST, txtPassword, 5, SpringLayout.EAST, lblPassword);
            sp.putConstraint(SpringLayout.NORTH, txtPassword, 5, SpringLayout.SOUTH, txtUserName);
    		this.getContentPane().add("Center", pane);
    	public static void main (String[] args) {
    		Login login = new Login();
    		login.setSize(200, 300);
    	public void actionPerformed(ActionEvent e) {
    		// do some actions		
    	private JLabel lblUserName = new JLabel("User name: ");
    	private JTextField txtUserName = new JTextField(15);
    	private JLabel lblPassword = new JLabel("Password : ");
    	private JPasswordField txtPassword = new JPasswordField(15);
    	private JButton btnLogin = new JButton("Login");
    	private JButton btnCancel = new JButton("Cancel");
    Thanks in advancve
    Last edited by new_2_java; 11-11-2008 at 08:17 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power


    Why are you fixated on the SpringLayout here? This can be implemented with the GridBagLayout or a combination of different layouts. Myself, I like to create two JPanels like so:
    Java Code:
    JPanel labelPanel = new JPanel(new GridLayout(0, 1, 0, 10));
    JPanel fieldPanel = new JPanel(new GridLayout(0, 1, 0, 10));
    and add the JLabels ("User Name:" and "Password:") to the labelPanel and the JTextField and JPasswordField to the fieldPanel. I then add these panels to the contentPane BorderLayout.WEST and BorderLayout.CENTER respectively.

Similar Threads

  1. how to use JFrame in JSP ??
    By priyanka sharma in forum JavaServer Pages (JSP) and JSTL
    Replies: 8
    Last Post: 04-06-2011, 04:51 AM
  2. SpringLayout ... Pain!
    By new_2_java in forum New To Java
    Replies: 4
    Last Post: 11-10-2008, 04:59 PM
  3. JFrame ??
    By sweet angle in forum AWT / Swing
    Replies: 15
    Last Post: 11-06-2008, 04:25 PM
  4. jframe
    By amith in forum AWT / Swing
    Replies: 1
    Last Post: 05-15-2008, 10:03 AM
  5. Help with JFrame
    By Albert in forum AWT / Swing
    Replies: 2
    Last Post: 07-04-2007, 04:44 AM

Posting Permissions

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