|
-
May 15th, 2001, 07:40 PM
#1
Thread Starter
Dazed Member
Sizing Components? [resolved]
Does anyone know how i can go about adding buttons to a
container in a single vertical column where all the buttons have
the same size regardless of there text? I tried The setPerferredSize() and setMaximumSize() methods from the JComponent class but i dont get the results i want....
Thanks....
JFrame jf = new JFrame("TestFrame");
Container content = jf.getContentPane();
content.setLayout(new BoxLayout(content,BoxLayout.Y_AXIS));
JButton Big = new JButton("Big");
JButton Bigger = new JButton("Bigger");
JButton Even Bigger = new JButton("Even Bigger");
JButton Really really Big = new JButton("Really really Big ");
JButton = The Biggest of the Big new JButton(" The Biggest of the Big");
content.add(Big);
content.add(Bigger);
content.add(Even Bigger);
content.add(Really really Big);
content.add(The Biggest of the Big);
Last edited by Dilenger4; Oct 15th, 2001 at 12:05 PM.
-
Aug 23rd, 2001, 02:03 AM
#2
Thread Starter
Dazed Member
Any ideas?
-
Oct 12th, 2001, 03:12 PM
#3
Thread Starter
Dazed Member
I just felt like posting this again....... I posted this a while ago so maybe ill get a response this time.
-
Oct 12th, 2001, 04:53 PM
#4
Addicted Member
I think this is what you want!
The important part is the this.pack() command. It packs the components down to the prefferedSize dimension.
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class VBWorld_Test extends JFrame
{
public VBWorld_Test()
{
super ("This is the title");
//create the panel we add to the root content pane
JPanel thePane = new JPanel(new GridLayout(5,1));
//create all the buttons
JButton big = new JButton("Big");
JButton bigger = new JButton("Bigger");
JButton evenBigger = new JButton("Even Bigger");
JButton reallyBig = new JButton("Really really Big ");
JButton biggest = new JButton(" The Biggest of the Big");
//create the dimension that they are all going to have
Dimension allTheSame = new Dimension(200, 50);
//set there preffered size to the dimension
big.setPreferredSize(allTheSame);
bigger.setPreferredSize(allTheSame);
evenBigger.setPreferredSize(allTheSame);
reallyBig.setPreferredSize(allTheSame);
biggest.setPreferredSize(allTheSame);
//add the buttons to the panel
thePane.add(big);
thePane.add(bigger);
thePane.add(evenBigger);
thePane.add(reallyBig);
thePane.add(biggest);
//add the panel to the content pane
this.getContentPane().add(thePane, BorderLayout.CENTER);
//add in the window listener so it close properly
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0);
}
});
//stop it resizing
this.setResizable(false);
//pack it down to preffered sizes
this.pack();
//show it
this.setVisible(true);
}
public static void main(String args[])
{
VBWorld_Test mainFrame = new VBWorld_Test();
}
}
Hope thats helped
Ford? Theres an infinite number of monkeys outside that want to talk to you about a script of hamlet they've produced!
-
Oct 13th, 2001, 07:30 PM
#5
Thread Starter
Dazed Member
hummmm. Ill give it a try. But im not quite sure if it will work.
The public void pack() method from the java.awt.Window
class resizes the window to encompass the perfered size of
all the contained components, as placed by the current layout manager. So it sounds like it actually resizes the Window not the
actual components. But ill try it. Thanks
-
Oct 14th, 2001, 01:40 PM
#6
Thread Starter
Dazed Member
Didnt seem to work. But that's ok. What works is setting the Maximum size not the Perfered Size. Why this is so im not too sure. I dont think that the pack() method has anything to do with the resizing of components, it just resizes the parent container.
Thanks.
Code:
import javax.swing.*;
import java.awt.*;
public class Test{
public static void main(String[] args){
JFrame jf = new JFrame("TestFrame");
jf.addWindowListener(new WindowMonitor());
Container content = jf.getContentPane();
content.setLayout(new BoxLayout(content,BoxLayout.Y_AXIS));
JButton Big = new JButton("Big");
JButton Bigger = new JButton("Bigger");
JButton EvenBigger = new JButton("Even Bigger");
JButton ReallyreallyBig = new JButton("Really really Big ");
JButton TheBiggestoftheBig = new JButton(" The Biggest of the Big");
Dimension allTheSame = new Dimension(200, 50);
Big.setPreferredSize(allTheSame);
Bigger.setPreferredSize(allTheSame);
EvenBigger.setPreferredSize(allTheSame);
ReallyreallyBig.setPreferredSize(allTheSame);
TheBiggestoftheBig.setPreferredSize(allTheSame);
content.add(Big);
content.add(Bigger);
content.add(EvenBigger);
content.add(ReallyreallyBig);
content.add(TheBiggestoftheBig);
jf.pack();
jf.setSize(400,300);
jf.setVisible(true);
}
}
-
Oct 15th, 2001, 10:59 AM
#7
Thread Starter
Dazed Member
yes i think being able to resize an interface is a bad idea.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|