Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > when is java2 not java2

when is java2 not java2
Thread Tools
Forum Regular
Join Date: Nov 2000
Status: Offline
Reply With Quote
Mar 27, 2001, 10:21 AM
 
hello, i am learning java and typed in the following example from a learningjava book.What should happen is a window with a button should be drawn. the screenshot of the results(windows) shows a small button on a window. under OSX though the button fills the ENTIRE window.

does this happen with you? the commentted lines produced the same results. is swing broken of osx? or is it a beginners mistake?

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

public class AddAButton
{
public AddAButton ()
{
//Dimension size =new Dimension(80,20);
JFrame frame = new JFrame("This is the title...");
frame.setSize(300,300);

Container content = frame.getContentPane();
JButton button = new JButton("Look at me");

button.setToolTipText("I am a Jbutton");
//button.setPreferredSize(size);
content.add(button);

frame.setVisible(true);
}
public static void main(String[] args)
{
new AddAButton();
}
}
     
Fresh-Faced Recruit
Join Date: Jun 2000
Location: Oslo, Norway
Status: Offline
Reply With Quote
Mar 27, 2001, 12:00 PM
 
Originally posted by iloveOSX:
[b]hello, i am learning java and typed in the following example from a learningjava book.What should happen is a window with a button should be drawn. the screenshot of the results(windows) shows a small button on a window. under OSX though the button fills the ENTIRE window.

does this happen with you? the commentted lines produced the same results. is swing broken of osx? or is it a beginners mistake?

[b]
To layout, position and size widgets, Java uses a concept called LayoutManagers. Look this up in your book. What this means is that most of the time (ideally, never) won't explicitly be sizing your widgets like your trying to do here.

My guess is that the frame's default layout manager lays out your component a little differently than what it has done for the author of your book. Strange as it may seem, this is not nescessarily wrong.

When you've understood the concept of layout managers you'll understand that the example I'm about to give you is "wrong". But try doing the following modification to your code, and see if you now get the result you want (be sure to remove the comments you've made in your code, so that it's just like the example before you insert my modification):

Code:
JFrame frame = new JFrame("This is the title..."); // --> Insert the following line frame.setLayoutManager(null); // <-- frame.setSize(300,300);
I haven't got a computer with java on it right now, so I'm not entirely sure that this will work.

[This message has been edited by scotty (edited 03-27-2001).]
     
Forum Regular
Join Date: Nov 2000
Status: Offline
Reply With Quote
Mar 27, 2001, 12:32 PM
 
that line did not work but i ma going to look in the book now and see if i made a slight syntax letter.

thank you for your response. I'll post the solution if/when i get it.
     
Senior User
Join Date: Nov 2000
Status: Offline
Reply With Quote
Mar 27, 2001, 08:56 PM
 
Code:
import java.awt.*; import javax.swing.*; public static void main(String[] args) { JFrame frame = new JFrame("This is the title..."); frame.setSize(300,300); JButton button = new JButton("Look at me"); button.setToolTipText("I am a Jbutton"); //ContentPane is a Container with a BorderLayout by default. frame.getContentPane().add(button, BorderLayout.SOUTH); frame.setVisible(true); }
Alternitively you can set the layout manager to a FlowLayout. FlowLayout is the most commonly used, I think. Mixing them is good too. You can get some neat layouts that way.

docs you might want to have a look at:
FlowLayout
BorderLayout

[typo]

[This message has been edited by IamBob (edited 03-27-2001).]
     
   
Thread Tools
Forum Links
Forum 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
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:50 AM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2