hipal
asked on
button fills whole frame :(
I am just trying to add a button to a frame... even when I setting the size and location for button, it is still occupying the whole frame. :( I mean it appears as the size of frame.
:annoyed
:annoyed
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
@objects : sorry! :-) exactly in the same time!
btw, adding button is not enought. setBounds must be invoked too to position the component
btw, adding button is not enought. setBounds must be invoked too to position the component
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
null layout mostly lead into problems, should avoid unless and otherwise needed! here null layout should be avoided!
ASKER
Hi all thanks a lot for all the responses for my silly mistake.
But, I need do to place different buttons at different places on screen .. so I think I have no other option but to use null layout.
But, I need do to place different buttons at different places on screen .. so I think I have no other option but to use null layout.
>>so I think I have no other option but to use null layout
thats a bad idea as I said earlear, check with the various layout managers and choose the best fit for you!
thats a bad idea as I said earlear, check with the various layout managers and choose the best fit for you!
ASKER
Can you please elaborate .. what kind of issues it can lead to ?
when you resize the window, componenets won't be resized/re-aligned!
ASKER
But my app will never cause resizing the window .. I am even using it as undecorated .. so no option to resize..
But still there are no work arounds ? so that they get resized on resizing window..
and I still confused, I mean placing components are desired places should be such a common thing ..how can we work with pre defined layouts everytime.
I need button to be on border of panel and also want few here and there .. can you please suggest something.. as I will really like to use layouts .. because it is better practice like you all experts suggested
But still there are no work arounds ? so that they get resized on resizing window..
and I still confused, I mean placing components are desired places should be such a common thing ..how can we work with pre defined layouts everytime.
I need button to be on border of panel and also want few here and there .. can you please suggest something.. as I will really like to use layouts .. because it is better practice like you all experts suggested
>>and I still confused, I mean placing components are desired places should be such a common thing ..how can we work with pre defined layouts everytime.
Layout managers lay out components in a predictable way and allow resizing
Layout managers lay out components in a predictable way and allow resizing
import java.awt.BorderLayout;
import java.awt.GridBagConstraint
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.WindowAdapt
import java.awt.event.WindowEvent
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* TYPE DESCRIPTION HERE
*
*/
public class TestGridbaglayout{
private JPanel buildComponent(){
JPanel pnlMain = new JPanel( new GridBagLayout() ) ;
//30%
pnlMain.add( new JButton( "Button 1" ), new GridBagConstraints( 0, 0, 1, 1, 0.3, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZON
//70%
pnlMain.add( new JButton( "Button 2" ), new GridBagConstraints( 1, 0, 1, 1, 1.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZON
//None - only component display area
pnlMain.add( new JButton( "Button 3" ), new GridBagConstraints( 2, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZON
//50%
pnlMain.add( new JButton( "Button 1" ), new GridBagConstraints( 0, 1, 1, 1, 1.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZON
//50%
pnlMain.add( new JButton( "Button 2" ), new GridBagConstraints( 1, 1, 1, 1, 1.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZON
//None - only component display area
pnlMain.add( new JButton( "Button 3" ), new GridBagConstraints( 2, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZON
//~35%
pnlMain.add( new JButton( "Button 1" ), new GridBagConstraints( 0, 2, 1, 1, 1.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZON
//~30%
pnlMain.add( new JButton( "Button 2" ), new GridBagConstraints( 1, 2, 1, 1, 0.3, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZON
//~35%
pnlMain.add( new JButton( "Button 3" ), new GridBagConstraints( 2, 2, 1, 1, 1.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZON
//~35%
pnlMain.add( new JButton( "Button 1" ), new GridBagConstraints( 0, 3, 1, 1, 1.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZON
//~30%
pnlMain.add( new JButton( "Button 2" ), new GridBagConstraints( 1, 3, 2, 1, 0.3, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZON
//~35%
pnlMain.add( new JButton( "Button 1" ), new GridBagConstraints( 0, 4, 2, 1, 1.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZON
//~30%
pnlMain.add( new JButton( "Button 2" ), new GridBagConstraints( 2, 4, 1, 1, 0.3, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZON
//~35%
pnlMain.add( new JButton( "Button 1" ), new GridBagConstraints( 0, 5, 2, 1, 1.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 0 ), 0, 0 ) );
//~30%
pnlMain.add( new JButton( "Button 2" ), new GridBagConstraints( 2, 5, 1, 1, 0.3, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 0 ), 0, 0 ) );
return pnlMain ;
}
/**
* @param args
*/
public static void main( String[] args ){
TestGridbaglayout test = new TestGridbaglayout() ;
JFrame frame = new JFrame("TestGridbaglayout"
frame.addWindowListener(ne
public void windowClosing(WindowEvent e) {System.exit(0);}
});
frame.getContentPane().add
frame.setSize( 400, 400 ) ;
// frame.pack();
frame.setVisible(true);
}
}