4

I have to make a Java Program using Swing and I want to show some text, but not using JLabel (including text component)! I have to extends JFrame. What other options do I have?

Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
lolllol
  • 61
  • 1
  • 4
  • 5
    That's a rather strange restriction. Do you have any other restrictions we should know about before suggesting answers? Why can you not use a JLabel? – Duncan Jones Oct 23 '12 at 17:35
  • 1
    I don't think this question is too localized—there may be a reason to render text directly—but more context would help. – trashgod Oct 23 '12 at 17:43
  • 1
    Other restrictions could be if the text must wrap the words before show. – Roman C Oct 23 '12 at 17:46
  • Dont rewrite/delete question text with nonsense, either delete it, leave it for now and come back later when you know exactly or mark as solved if it is. But editing a question to now say: *I have to think about it more...* is not good idea it will be removed. – David Kroukamp Oct 23 '12 at 18:24

1 Answers1

4

You should not:

  • Extend JFrame unnecessarily
  • Draw directly to JFrame via paint(..)

You should:

  • override JComponents paintComponent(...)
  • do custom painting in paintComponent(...),
  • add JComponent instance to JFrame

Here is an example for you:

enter image description here

import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class DrawSimpleText {

    public DrawSimpleText() {
        initComponents();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new DrawSimpleText();
            }
        });
    }

    private void initComponents() {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(new JPanel(true) {
            Font font = new Font("Serif", Font.PLAIN, 20);
            String s = "Java Source and Support";

            @Override
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2 = (Graphics2D) g;

                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);
                g2.setFont(font);

                FontMetrics fm = g.getFontMetrics(font);

                int width = fm.stringWidth(s);

                Dimension d = getSize();

                //center String/text
                int cx = (d.width - width) / 2;
                int cy = (d.height - fm.getHeight()) / 2 + fm.getAscent();

                g2.drawString(s, cx, cy);
            }

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(300, 100);
            }
        });
        f.pack();
        f.setVisible(true);
    }
}

UPDATE:

As per your comment though its going against all Swing rules and my own morals:

However if you still need to do this (which I cannot fathom why this is just bad practice) here is how:

enter image description here

import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class DrawSimpleText extends JFrame {

    public DrawSimpleText() {
        initComponents();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new DrawSimpleText();
            }
        });
    }

    private void initComponents() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }
    Font font = new Font("Serif", Font.PLAIN, 20);
    String s = "Java Source and Support";

    @Override
    public void paint(Graphics g) {

        super.paint(g);
        Graphics2D g2 = (Graphics2D) g;

        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setFont(font);

        FontMetrics fm = g.getFontMetrics(font);

        int width = fm.stringWidth(s);

        Dimension d = getSize();

        //center String/text
        int cx = (d.width - width) / 2;
        int cy = (d.height - fm.getHeight()) / 2 + fm.getAscent();

        g2.drawString(s, cx, cy);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(300, 100);
    }
}
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138