![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Jun 2005
Location: Amittyville
Posts: 60
Rep Power: 4
![]() |
GUI size
Can a GUI be created with certain button sizes and textfield sizes so that some buttons are sized larger than others, I know it can be done with VB but I don't know about Java, can anyone help?
|
|
|
|
|
|
#2 |
|
Professional Programmer
|
import java.awt.*;
import java.util.*;
import javax.swing.*;
public class NoWorriesMate{
public static void main(String[] args) {
Box box = Box.createHorizontalBox();
for(int i=0; i<10; ++i)
box.add(createButton());
JFrame f = new JFrame("NoWorriesMate");
f.setContentPane(box);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
static JButton createButton() {
JButton btn = new JButton("I'm resized");
final int pad = 10;
btn.setMargin(new Insets(pad, pad, pad, pad));
return btn;
}
}![]()
__________________
Don't take life too seriously, it's not permanent ! |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Jun 2005
Location: Amittyville
Posts: 60
Rep Power: 4
![]() |
thanx!
|
|
|
|
|
|
#4 |
|
Professional Programmer
|
Good luck to you man !
__________________
Don't take life too seriously, it's not permanent ! |
|
|
|
|
|
#5 |
|
Programmer
|
The set___Size() methods will do it for you too.
public class SizedButtons extends JFrame{
public SizedButtons(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
BoxLayout layout = new BoxLayout(panel, BoxLayout.Y_AXIS);
panel.setLayout(layout);
panel.add(makeBtn(500, 10));
panel.add(makeBtn(70, 70));
panel.add(makeBtn(60, 300));
add(panel);
pack();
setVisible(true);
}
private JButton makeBtn(int width, int height){
JButton btn = new JButton(Integer.toString(width) + " x " + Integer.toString(height));
btn.setMinimumSize(new Dimension(width, height));
btn.setMaximumSize(new Dimension(width, height));
btn.setPreferredSize(new Dimension(width, height));
btn.setMargin(new Insets(0, 0, 0, 0));
return btn;
}
public static void main(String[] args){
new SizedButtons();
}
}EDIT: As an additional note, setMargins(Insets) only changes the spacing between the edges of the button and its contents. So, in order to predict the final size of the button you have to know the exact size of the contents inside. On the other hand, if you use the set____Size() methods, you'll be guaranteed to get the exact size you want. (And you can still control the margins if you want. I set them all to zero to make sure the text would still show up in smaller buttons. When there isn't enough room, text is converted to an ellipsis.) As a general rule, try to use all three of the sizing methods. Sometimes setPreferredSIze() (the most important of the three) isn't enough. Oh, and you can't set the size of objects that are in the center position of a BorderLayout, just so you know. Objects placed there always stretch to fill the entire center space.
__________________
I can pick my friends. And I can pick my nose. So, why can't I pick my friend's nose? Last edited by EdSalamander; Jun 23rd, 2005 at 1:13 AM. |
|
|
|
|
|
#6 |
|
Programmer
Join Date: Jun 2005
Location: Amittyville
Posts: 60
Rep Power: 4
![]() |
what classes will I need to import to use this code?
__________________
I steal hippos... |
|
|
|
|
|
#7 |
|
Programmer
Join Date: Jun 2005
Location: Amittyville
Posts: 60
Rep Power: 4
![]() |
never mind I figured it out:
import javax.swing.*; import java.awt.*;
__________________
I steal hippos... |
|
|
|
|
|
#8 |
|
Programmer
Join Date: Jun 2005
Location: Amittyville
Posts: 60
Rep Power: 4
![]() |
If you want to set the exact location and size of a component use the following code:
//imports import javax.swing.JPanel; import javax.swing.JFrame; import javax.swing.JButton; import java.awt.Component; //code JPanel pnl = new JPanel(); pnl.setLayout(null); JButton btn = new JButton(); //sets the size and location of a component //[component].setBounds(int x, int y, int width, int height); btn.setBounds(50, 50, 100, 30); pnl.add(btn); add(pnl); Very helpful if you want absolute control.
__________________
I steal hippos... |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|