Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2007

Tip: Looking for answers? Try searching our database.

JButton question

Thread view: 
Z.K. - 27 Aug 2007 03:06 GMT
I have a simple java gui that I wrote which adds 3 buttons and some
text.  It seems to work, but the buttons seem like they are all
different sizes.  How to I get them to be all the same size.  I tried
setSize(), but that did not seem to work.  My code is below.

  Z.K.

code:

import javax.swing.*;
import java.awt.*;

public class NotHelloWorld
{
   public static void main(String[] args)
   {
       NotHelloWorldFrame frame = new NotHelloWorldFrame();
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.setVisible(true);
   }

}

class NotHelloWorldFrame extends JFrame
{
    public NotHelloWorldFrame()
    {
        setTitle("NotHelloWorld");
        setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
        NotHelloWorldPanel panel = new NotHelloWorldPanel();
        add(panel);

    }

    public static final int DEFAULT_WIDTH = 300;
    public static final int DEFAULT_HEIGHT = 200;
}

class NotHelloWorldPanel extends JPanel
{

    public NotHelloWorldPanel()
    {
      NotHelloWorldButtonPanel bpanel = new NotHelloWorldButtonPanel();
      add(bpanel);

    }
    public void paintComponent(Graphics g)
    {
       g.drawString("Not a Hello World program", MESSAGE_X, MESSAGE_Y);

    }

    public static final int MESSAGE_X = 75;
    public static final int MESSAGE_Y = 100;

}

class NotHelloWorldButtonPanel extends JPanel
{

  public NotHelloWorldButtonPanel()
  {
     JButton yellowButton = new JButton("Yellow");
     JButton blueButton = new JButton("Blue");
     JButton redButton = new JButton("Red");

      //redButton.setSize(5,10);
     //yellowButton.setSize(5,10);
     //blueButton.setSize(5,10);

     add(yellowButton);
     add(blueButton);
     add(redButton);

  }

}
IchBin - 27 Aug 2007 04:24 GMT
>I have a simple java gui that I wrote which adds 3 buttons and some
>text.  It seems to work, but the buttons seem like they are all
[quoted text clipped - 74 lines]
>
>}

You need to use a layout manager. Look at Sun's tutorial here:
http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html

Signature

Thanks in Advance...                           http://weconsulting.org
IchBin, Philadelphia, Pa, USA http://ichbinquotations.weconsulting.org
______________________________________________________________________
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)

Richard Sweeny - 13 Sep 2007 01:34 GMT
> I have a simple java gui that I wrote which adds 3 buttons and some
> text.  It seems to work, but the buttons seem like they are all
[quoted text clipped - 74 lines]
>
> }
At times I use:
    Dimension dim = new Dimension(( 90,26 );
    yellowButton.setPreferredSize( dim );
    yellowButton.setMaximumSize( dim );
    yellowButton.setMinimumSize( dim );

And yes use a layout manager. I usually use the  GridBagLayout manager
and set any resize constraints to NONE. This way the button does not
stretch to fill its horizontal space or change size when you resize the
window. Of course this is really meant to set a minimum  and maximum
size for your buttons,

Rich
Andrew Thompson - 13 Sep 2007 04:47 GMT
>> I have a simple java gui that I wrote which adds 3 buttons and some
>> text.  ...
...
>At times I use:
>    Dimension dim = new Dimension(( 90,26 );
>    yellowButton.setPreferredSize( dim );
>    yellowButton.setMaximumSize( dim );
>    yellowButton.setMinimumSize( dim );

I am going to assume that is a dramatic over-simplification
of what you actually use, or that these are for some trivial
type of button with no text.  Otherwise, that code (in its
simplest form as expresed above) effectively takes all
the clever engineering Sun has done to ensure we can
make X-plat, X-PLAF, resizable GUI's - and throws it
out the window.

The most specific problem with it is the hard coded
dimensions that seem not to account for default font
size, borders or margins.

>And yes use a layout manager.

Now you're talkin' my language.

Signature

Andrew Thompson
http://www.athompson.info/andrew/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.