Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 19th, 2005, 11:34 PM   #1
squishiful
Programmer
 
Join Date: Jun 2005
Location: Amittyville
Posts: 60
Rep Power: 4 squishiful is on a distinguished road
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?
squishiful is offline   Reply With Quote
Old Jun 20th, 2005, 12:13 AM   #2
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 407
Rep Power: 5 xavier is on a distinguished road
Send a message via Yahoo to xavier
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;
    }
}
This is an example for making a certain size buttons. It's simple, try searching google for the other one. Google is a coder's best friend
__________________
Don't take life too seriously, it's not permanent !
xavier is offline   Reply With Quote
Old Jun 22nd, 2005, 12:58 PM   #3
squishiful
Programmer
 
Join Date: Jun 2005
Location: Amittyville
Posts: 60
Rep Power: 4 squishiful is on a distinguished road
thanx!
squishiful is offline   Reply With Quote
Old Jun 22nd, 2005, 2:49 PM   #4
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 407
Rep Power: 5 xavier is on a distinguished road
Send a message via Yahoo to xavier
Good luck to you man !
__________________
Don't take life too seriously, it's not permanent !
xavier is offline   Reply With Quote
Old Jun 23rd, 2005, 1:05 AM   #5
EdSalamander
Programmer
 
EdSalamander's Avatar
 
Join Date: Dec 2004
Location: Tucson, AZ, USA
Posts: 80
Rep Power: 5 EdSalamander is on a distinguished road
Send a message via AIM to EdSalamander
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.
EdSalamander is offline   Reply With Quote
Old Jun 23rd, 2005, 12:43 PM   #6
squishiful
Programmer
 
Join Date: Jun 2005
Location: Amittyville
Posts: 60
Rep Power: 4 squishiful is on a distinguished road
what classes will I need to import to use this code?
__________________
I steal hippos...
squishiful is offline   Reply With Quote
Old Jun 23rd, 2005, 12:47 PM   #7
squishiful
Programmer
 
Join Date: Jun 2005
Location: Amittyville
Posts: 60
Rep Power: 4 squishiful is on a distinguished road
never mind I figured it out:
import javax.swing.*;
import java.awt.*;
__________________
I steal hippos...
squishiful is offline   Reply With Quote
Old Jun 26th, 2005, 4:48 PM   #8
squishiful
Programmer
 
Join Date: Jun 2005
Location: Amittyville
Posts: 60
Rep Power: 4 squishiful is on a distinguished road
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...
squishiful is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:51 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC