permaculture playing cards*
The moose likes Swing / AWT / SWT and the fly likes Doubt in this Swing program. can anyone clear my doubt? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Doubt in this Swing program. can anyone clear my doubt?" Watch "Doubt in this Swing program. can anyone clear my doubt?" New topic
Author

Doubt in this Swing program. can anyone clear my doubt?

pras
Ranch Hand

Joined: Apr 04, 2007
Posts: 188
1>import java.awt.*;
2>import java.awt.event.*;
3>import javax.swing.*;
4>
5>
6>public class FrameDemo {
7>
8> private static void createAndShowGUI() {
9>
10> JFrame frame = new JFrame("FrameDemo");
11> frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
12>
13> JLabel emptyLabel = new JLabel("Prasanna");
14> emptyLabel.setPreferredSize(new Dimension(1000, 1000));
15> frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
16>
17>
18> frame.pack();
19> frame.setVisible(true);
20> }
21>
22> public static void main(String[] args) {
23> javax.swing.SwingUtilities.invokeLater(new Runnable() {
24> public void run() {
25> createAndShowGUI();
26> }
27> });
28> }
29>}


/************ doubts*******************/
FIRST DOUBT : on line 11 even if i skip that line the frame exits on pressing x then whats the use?

SECOND DOUBT : on line 14 the dimensions are set for Jlabel ie the string "Prasanna" should be Displayed larger then why Jframe dimension changed??
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

If you don't use EXIT_ON_CLOSE, then the frame will probably "disappear" when its close button is clicked, but the Java program will continue to run because System.exit is not called. You will need to kill the program by entering Ctrl+C at the command prompt.

When you specify a larger preferred size for the label, then the JFrame's size will likely increase when pack() is called, which "causes this Window to be sized to fit the preferred size and layouts of its subcomponents."


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
pras
Ranch Hand

Joined: Apr 04, 2007
Posts: 188
Hi mark,

Dimensions if i specify as say 5000,5000 at line 14 and at line 15 if i mention as NORTH.

why isnt the TEXT "Prasanna" printed

one more question
if i specify Dimensions as 1000,1000 at line 14 and specify NORTH. then why "Prasanna" doesnt come at top left??
pras
Ranch Hand

Joined: Apr 04, 2007
Posts: 188
what exactly is a contentpane because i got the same frame after commenting line 13 14 15

??
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
swing is hard


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Have you seen the Swing Tutorial?

Note especially the part on Layout Managers.
[ December 04, 2007: Message edited by: marc weber ]
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121


Dimensions if i specify as say 5000,5000 at line 14 and at line 15 if i mention as NORTH.

why isnt the TEXT "Prasanna" printed


It is printed - it's just that your frame and label are now so enormous that the text on the label is off the screen.


if i specify Dimensions as 1000,1000 at line 14 and specify NORTH. then why "Prasanna" doesnt come at top left??


There's no other content in the contentPane, so your label in NORTH gets the entire area, the same as if it were in CENTER. BorderLayout only really changes if more than one area of the layout has components in it.


what exactly is a contentpane because i got the same frame after commenting line 13 14 15


This was only added recently - JDK 1.5 or 1.6 - you used to have to call frame.getContentPane().add(...) to add any components to a frame. Now frame.add(...) does the same thing.


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in this Swing program. can anyone clear my doubt?