The Artima Developer Community
Sponsored Link

Java Answers Forum
Text in a JFrame?

4 replies on 1 page. Most recent reply: Sep 28, 2005 11:14 PM by Kondwani Mkandawire

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Andrew

Posts: 4
Nickname: barkera0
Registered: Jun, 2005

Text in a JFrame? Posted: Sep 28, 2005 9:05 PM
Reply to this message Reply
Advertisement
How can I put text in a JFrame? Like not text where you can write, but just plain text I want to display. How do u do that?


Amol Brid

Posts: 43
Nickname: amolbrid
Registered: Feb, 2004

Re: Text in a JFrame? Posted: Sep 28, 2005 9:51 PM
Reply to this message Reply
Use JLabel.

Andrew

Posts: 4
Nickname: barkera0
Registered: Jun, 2005

Re: Text in a JFrame? Posted: Sep 28, 2005 10:18 PM
Reply to this message Reply
ty vm.

Andrew

Posts: 4
Nickname: barkera0
Registered: Jun, 2005

Re: Text in a JFrame? Posted: Sep 28, 2005 10:36 PM
Reply to this message Reply
How do you actually use a JLabel? I tried this and nothing showed up in my window:

JLabel label = new JLabel("Text-Only Label");
label.setFont(new Font("Serif", Font.PLAIN, 36));

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Text in a JFrame? Posted: Sep 28, 2005 11:14 PM
Reply to this message Reply
> How do you actually use a JLabel? I tried this and nothing
> showed up in my window:
>
> JLabel label = new JLabel("Text-Only Label");
> label.setFont(new Font("Serif", Font.PLAIN, 36));


You need to add it to the frame and make sure the frame is
set as myFrame.setVisible(true);

Here is an example:
    JFrame someFrame = new JFrame();
    JLabel label = new JLabel("Text-Only Label");
    label.setFont(new Font("Serif", Font.PLAIN, 36));
    someFrame.add(label);
    someFrame.setSize(230, 230);
    someFrame.setVisible(true);


or if your class is extending JFrame then it would
simply be:
    //  set Layout to whatever Layout Manager
    //  flowLayout, GridLayout, etc...
    getContentPane().setLayout(new FlowLayout());
    JLabel label = new JLabel("Text-Only Label");
    label.setFont(new Font("Serif", Font.PLAIN, 36));
    getContentPane().add(label);
    setSize(230, 230);  //  or whatever size you want
    //  Place Frame in middle of Screen
    setLocationRelativeTo(null);  
    setVisible(true);


Good luck,
Spike

Flat View: This topic has 4 replies on 1 page
Topic: Local variable:  null declaration Previous Topic   Next Topic Topic: chained exception doubt

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use