• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what exactly is a contentpane because i got the same frame after commenting line 13 14 15

??
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
swing is hard
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Did you ever grow anything in the garden of your mind? - Fred Rogers. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic