Results 1 to 5 of 5
Like Tree2Likes
  • 1 Post By milovan
  • 1 Post By milovan

Thread: JComponents remained on side when frame size was increased? How to fix it?

  1. #1
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    6

    Default JComponents remained on side when frame size was increased? How to fix it?

    Below is a picture of a frame which needs is formed/opens as result of the button-click event of a button. I am copying the code of the entire action listener as well. The problem is that the size of the frame set by setSize(x, y), but the components don't appear accordingly. The size of the frame has increased (by setSize(x, y), i had set a different size earlier which was smaller) and so the components appear at one side on the frame.

    Are there any properties or methods which can be used to correct this behavior? I'll be grateful if somebody can guide me about it.

    JComponents remained on side when frame size was increased? How to fix it?-9.png

    Java Code:
    static class btninstr_Action implements ActionListener{
    		public void actionPerformed(ActionEvent arg0) {
    			JFrame frame2= new JFrame();
    			frame2.setVisible(true); 
    			frame2.setSize(500,500);// add custom panel to get perfect work
    			
    			frame2.setBackground(SystemColor.textInactiveText);
    			frame2.setLayout(null);
    			
    			JPanel panel = new JPanel();
    			panel.setBackground(SystemColor.inactiveCaption);
    			panel.setBounds(10, 11, 131, 240);
    			frame2.add(panel);
    			panel.setLayout(null);
    			
    			JButton btnNewButton = new JButton("New button");
    			btnNewButton.setBounds(10, 30, 111, 59);
    			panel.add(btnNewButton);
    			
    			JButton btnNewButton_1 = new JButton("New button");
    			btnNewButton_1.setBounds(10, 100, 111, 59);
    			panel.add(btnNewButton_1);
    			
    			JButton btnNewButton_2 = new JButton("New button");
    			btnNewButton_2.setBounds(10, 170, 111, 59);
    			panel.add(btnNewButton_2);
    			
    			JLabel label = new JLabel("New label");
    			label.setBounds(10, 5, 111, 14);
    			panel.add(label);
    			
    			JPanel panel_1 = new JPanel();
    			panel_1.setBackground(SystemColor.inactiveCaptionBorder);
    			panel_1.setBounds(151, 11, 228, 240);
    			frame2.add(panel_1);
    			panel_1.setLayout(null);
    			
    			JPanel panel_2 = new JPanel();
    			panel_2.setBackground(SystemColor.textHighlightText);
    			panel_2.setBounds(10, 110, 208, 119);
    			panel_1.add(panel_2);
    			panel_2.setLayout(null);
    			
    			JTable table = new JTable();
    			table.setBounds(200, 112, -194, -99);
    			panel_2.add(table);
    			
    			JPanel panel_3 = new JPanel();
    			panel_3.setBackground(SystemColor.inactiveCaptionBorder);
    			panel_3.setBounds(136, 11, 82, 88);
    			panel_1.add(panel_3);
    			
    			JLabel lblNewLabel_1 = new JLabel("New label");
    			panel_3.add(lblNewLabel_1);
    			
    			JPanel panel_4 = new JPanel();
    			panel_4.setBackground(SystemColor.inactiveCaptionBorder);
    			panel_4.setBounds(10, 11, 116, 88);
    			panel_1.add(panel_4);
    			
    			JLabel lblNewLabel = new JLabel("New label");
    			panel_4.add(lblNewLabel);
    			
    			
    			
    			
    			
    		}
    		
    	}

  2. #2
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    311
    Rep Power
    9

    Default Re: JComponents remained on side when frame size was increased? How to fix it?

    didn't read through your code, but one question from first glance: why not use Layouts???

  3. #3
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    6

    Default Re: JComponents remained on side when frame size was increased? How to fix it?

    why not use Layouts???
    They confuse me and I am really out of time... So I used absolute layout everywhere. If you think layouts can help this situation, and you can tell me what layout to use on which component, I'll be grateful

  4. #4
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    311
    Rep Power
    9

    Default Re: JComponents remained on side when frame size was increased? How to fix it?

    Well, without layouts you hardly can do proper positioning of gui elements when re-sizing.. For the beginning, why don't you start with default (BorderLayout) layout and see how it works (check here How to Use BorderLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container))
    Zarah likes this.

  5. #5
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    311
    Rep Power
    9

    Default Re: JComponents remained on side when frame size was increased? How to fix it?

    And then, for "inner" panels you can try some more flexible layout. Again, try the simplest, GridLayout.. (sorry, I just take a look at the picture and saw your requirements)
    Zarah likes this.

Similar Threads

  1. Replies: 7
    Last Post: 05-26-2012, 08:07 PM
  2. Replies: 4
    Last Post: 01-18-2011, 09:22 AM
  3. JComponents Are Wrong Size
    By MrFish in forum AWT / Swing
    Replies: 2
    Last Post: 12-21-2010, 04:13 AM
  4. Replies: 2
    Last Post: 02-08-2010, 01:03 AM
  5. Setting frame size to the size of an image
    By Yoruichi in forum AWT / Swing
    Replies: 5
    Last Post: 04-22-2009, 05:37 PM

Posting Permissions

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