Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
pras
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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."
 
pras
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what exactly is a contentpane because i got the same frame after commenting line 13 14 15

??
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
swing is hard
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you seen the Swing Tutorial?

Note especially the part on Layout Managers.
[ December 04, 2007: Message edited by: marc weber ]
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic