JFrame & SpringLayout problem.

Reply

Join Date: Apr 2007
Posts: 126
Reputation: new_2_java is an unknown quantity at this point 
Solved Threads: 6
new_2_java new_2_java is offline Offline
Junior Poster

JFrame & SpringLayout problem.

 
0
  #1
Nov 11th, 2008
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:

  1. public class Login extends JFrame implements ActionListener {
  2. public Login () {
  3. super("Title");
  4. this.getContentPane().setLayout(new BorderLayout());
  5.  
  6. JPanel btnPane = new JPanel();
  7. btnPane.setLayout(new FlowLayout());
  8.  
  9. JPanel pane = new JPanel();
  10. SpringLayout sp = new SpringLayout();
  11. pane.setLayout(sp);
  12.  
  13. pane.add(lblUserName);
  14. pane.add(txtUserName);
  15. pane.add(lblPassword);
  16. pane.add(txtPassword);
  17.  
  18. sp.putConstraint(SpringLayout.WEST, lblUserName, 70, SpringLayout.WEST, this);
  19. sp.putConstraint(SpringLayout.NORTH, lblUserName, 20, SpringLayout.NORTH, this);
  20.  
  21. sp.putConstraint(SpringLayout.WEST, txtUserName, 5, SpringLayout.EAST, lblUserName);
  22. sp.putConstraint(SpringLayout.NORTH, txtUserName, 20, SpringLayout.NORTH, this);
  23.  
  24. sp.putConstraint(SpringLayout.WEST, lblPassword, 70, SpringLayout.WEST, this);
  25. sp.putConstraint(SpringLayout.NORTH, lblPassword, 5, SpringLayout.SOUTH, lblUserName);
  26.  
  27. sp.putConstraint(SpringLayout.WEST, txtPassword, 5, SpringLayout.EAST, lblPassword);
  28. sp.putConstraint(SpringLayout.NORTH, txtPassword, 5, SpringLayout.SOUTH, txtUserName);
  29.  
  30. btnLogin.addActionListener(this);
  31. btnCancel.addActionListener(this);
  32.  
  33. btnPane.add(btnLogin);
  34. btnPane.add(btnCancel);
  35.  
  36. this.getContentPane().add("Center", pane);
  37. this.getContentPane().add("South",btnPane);
  38.  
  39. }
  40. public static void main (String[] args) {
  41. Login login = new Login();
  42. login.setSize(10, 20);
  43. login.setLocation(200,200);
  44. login.pack();
  45. login.setVisible(true);
  46. }
  47. public void actionPerformed(ActionEvent e) {
  48. // do some actions
  49. }
  50. private JLabel lblUserName = new JLabel("User name: ");
  51. private JTextField txtUserName = new JTextField(15);
  52. private JLabel lblPassword = new JLabel("Password : ");
  53. private JPasswordField txtPassword = new JPasswordField(15);
  54.  
  55. private JButton btnLogin = new JButton("Login");
  56. private JButton btnCancel = new JButton("Cancel");
  57. }

Thanks in advance...
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 19
Reputation: bionicseraph is an unknown quantity at this point 
Solved Threads: 6
bionicseraph bionicseraph is offline Offline
Newbie Poster

Re: JFrame & SpringLayout problem.

 
0
  #2
Nov 11th, 2008
Try using bigger numbers in set size. 10x20 pixels will give you a really small window.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 126
Reputation: new_2_java is an unknown quantity at this point 
Solved Threads: 6
new_2_java new_2_java is offline Offline
Junior Poster

Re: JFrame & SpringLayout problem.

 
0
  #3
Nov 11th, 2008
That's what I thaught. But, no matter which number I try, I get the same size. I tried with setSize(200, 300); still the same size. So, I am thinking, I may be doing something wrong.

Please advise...
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,580
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 200
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: JFrame & SpringLayout problem.

 
0
  #4
Nov 12th, 2008
Get rid of the call to pack(). pack() resizes the components based on the preferred size of each component. So either setPreferredSize() and use pack, or leave your code like it is and do not use pack.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC