Hello.
I'm fairly new to Java and this is my first post. In the following app, I
display some buttons inside a JPanel inside a JFrame. The last command in
the constructor is a call to setBounds(), with which I try to position the
JFrame more toward the center of the screen, and size the JFrame at 100w x
400h pixels. I've also tried moving the setBounds() call to the bottom of
main(). In either case, the JFrame is positioned correctly (at 0,0 when
setBounds() gone; at 100,250 when setBounds() used) , but the width/height
numbers are ignored, resulting in a JFrame maybe 500w x 50h--emphatically
not 100w x 400h.
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Rebate extends JFrame implements ActionListener {
JButton create, update, delete, exit;
public Rebate() {
super ("Process Rebates");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
create = new JButton("Add New Rebate");
update = new JButton("Update Existing Rebate");
delete = new JButton("Delete a Completed Rebate");
exit = new JButton("Quit");
create.addActionListener(this);
update.addActionListener(this);
delete.addActionListener(this);
exit.addActionListener(this);
JPanel pane = new JPanel();
pane.setLayout( new GridLayout() );
pane.add(create);
pane.add(update);
pane.add(delete);
pane.add(exit);
setContentPane(pane);
setBounds(100,250,100,400);
}
public static void main (String[] args) {
Rebate r = new Rebate();
r.pack();
r.show();
}
public void actionPerformed (ActionEvent e) {
Object o = e.getSource();
*
* bunch of code which displays JOptionPane dialog boxes showing
* what button was clicked
*
System.exit(0);
}
}
Thanks.
Peter
Anti-spam = remove work dot x
Andrew Thompson - 09 Apr 2004 02:10 GMT
> ...numbers are ignored, resulting in a JFrame
> maybe 500w x 50h--emphatically not 100w x 400h.
.....
> public static void main (String[] args) {
> Rebate r = new Rebate();
> r.pack();
This is what is doing it..
Check the JDocs for Window.pack()
"Causes this Window to be sized to fit the
_preferred_ size and layouts of its subcomponents."
I suspect you'll find your components
_prefer_ to be the size that pack
results in.
It is important to understand that setting
the size of things is _generally_ a bad thing
to do, it destroys cross-platform UI's for
starters. It is besst to use the appropriate
layout manager(s) and let the UI find it's own
size.
If there _is_ a component with no definite size
(a drawing canvas, for instance) set it's minimum
size and rely on that to fill the GUI out.

Signature
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
Peter - 09 Apr 2004 04:06 GMT
Andrew Thompson <SeeMySites@www.invalid> wrote in news:1syee76pjo2lq
$.1ne0o82h5r174.dlg@40tude.net:
>> r.pack();
>
> This is what is doing it..
Thank you very much. I cloned some of this program from a Teach Yourself
Java in 21 Days. I've been pretty diligent about making sure I know what
cloned code was doing, but pack() slipped through the cracks.

Signature
Peter
Anti-spam =
a) Remove NS_
b) Change 3 zeroes to alpha O's