Create Jframe with label?

Asked

Viewed 1,517 times

2

How to create a Jframe, containing a Jlabel with text, and can be positioned anywhere in the frame.

I need this label to be anywhere in the frame area.

1 answer

3


package teste02;

import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class TelaSistema extends JFrame 
{
    public TelaSistema() 
    {
        setSize(600, 400);       
        setTitle("Sistema "); 
        JPanel painel = new JPanel();
        painel.setLayout(null);        
        JLabel label = new JLabel();
        label = new JLabel("Olá mundo !");
        painel.add(label);
        label.setBounds(250, 150, 300, 50);
        add(painel);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }      

    public static void main(String[] args) {
        TelaSistema teste = new TelaSistema();
    }
}
  • 1

    Give a sought before posting the questions, It may be that have already been put here.

  • can leave, it is that I am new here, but thanks! Thank you, I will test the Cod.

  • 1

    can change label text color ?

  • 2

    @Javinha does. A very simple way is to implement HTML tags. Example: <html>The <font color=#3061AB field> text here </font> </html>

  • Why use html if jlabel has native resource? Another thing, one should avoid using absolute position, unless this is of extreme necessity, this makes the screen construction and addition of components highly complex, so there are layoutmanagers, to do the "heavy work" of positioning for us.

  • jLabel1.setForeground(Color.red); for example, this way changes to red. See more colors in documentation of the Color class

  • 1

    Thanks guys, I tested both ways for colors. HTML solved better, due to the amount of words I edited within the text, and as for positioning, it was extremely necessary. Thanks again !

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.