user10516878
user10516878

Reputation:

How to add text to a JFrame in Java

I need to add some text for the score. Here's my code I used to make my JFrame. Please give me code to add text to it.

import java.awt.Graphics;
import java.awt.Color;
import javax.swing.*;
import javax.swing.JPanel;


public class Frame{
    JFrame frame;

    Frame()
    {
        frame=new JFrame("Tetris");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 600);
        frame.setLayout(null);
        frame.setVisible(true);
        frame.setResizable(false);
        ImageIcon img = new ImageIcon("Blocks.png");
        frame.setIconImage(img.getImage());
        frame.getContentPane().setBackground(Color.blue);
        JOptionPane.showMessageDialog(null, "        Press Ok To Start","Start", JOptionPane.INFORMATION_MESSAGE);


    }

    public static void main(String[] args)
    {
        new Frame();
    }
}

If there's any frame code I have to type please tell me.

Upvotes: 0

Views: 2367

Answers (1)

Prasad Karunagoda
Prasad Karunagoda

Reputation: 2148

I assume you want to add some text to the content area of the JFrame. I've added two approaches to do this below. See whether this works for you.

Approach 1 - Draw the text on a custom panel and add it to the frame:

import java.awt.Graphics;
import java.awt.Color;
import javax.swing.*;
import javax.swing.JPanel;

public class Frame{
  JFrame frame;

  Frame()
  {
    frame=new JFrame("Tetris");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500, 600);
    frame.setLayout(null);
    frame.setResizable(false);
    ImageIcon img = new ImageIcon("Blocks.png");
    frame.setIconImage(img.getImage());
    //frame.getContentPane().setBackground(Color.blue);
    frame.setContentPane(new MainPanel());

    // This line is moved down
    frame.setVisible(true);

    JOptionPane.showMessageDialog(null, "        Press Ok To Start","Start", JOptionPane.INFORMATION_MESSAGE);   
  }

  public static void main(String[] args)
  {
    new Frame();
  }
}

class MainPanel extends JPanel
{
  MainPanel()
  {
    setOpaque(true);
    setBackground(Color.blue);
  }

  @Override
  protected void paintComponent(Graphics g)
  {
    super.paintComponent(g);
    g.setFont(g.getFont().deriveFont(20.0F));
    g.setColor(Color.white);
    g.drawString("Sample text", 50, 50);
  }
}

Approach 2 - Add a JLabel to the frame:

import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.*;

public class Frame2{
  JFrame frame;

  Frame2()
  {
    frame=new JFrame("Tetris");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500, 600);

    // *** Commented this line
    //frame.setLayout(null);

    frame.setResizable(false);
    ImageIcon img = new ImageIcon("Blocks.png");
    frame.setIconImage(img.getImage());
    frame.getContentPane().setBackground(Color.blue);

    // *** Added this JLabel
    JLabel label = new JLabel("Sample text");
    label.setFont(label.getFont().deriveFont(20.0F));
    label.setForeground(Color.white);
    frame.getContentPane().add(label, BorderLayout.CENTER);

    // This line is moved down
    frame.setVisible(true);

    JOptionPane.showMessageDialog(null, "        Press Ok To Start","Start", JOptionPane.INFORMATION_MESSAGE);    
  }

  public static void main(String[] args)
  {
    new Frame2();
  }
} 

Upvotes: 3

Related Questions