简体   繁体   中英

Can't print any string using drawString() in JFrame

I'm trying to find what is wrong with this short code. I can't print the String TEXT in my JFrame using drawString() method. Please Help . Only a plain white screen will appear if you run the program .

Code:

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

public class sample extends JFrame
{

    private JPanel panel;

    public sample()
    {
        setSize(500,500);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);

        panel =new JPanel();

        Container mainP= getContentPane();
        mainP.add(panel);
        panel.setBounds(0,0,500,500);
        panel.setBackground(Color.WHITE);
    }

    public void paintComponent(Graphics g)
    {
        Graphics2D eg = (Graphics2D)g;
        eg.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        eg.setColor(Color.BLACK);
        eg.drawString("TEXT", 40, 120);
    }

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

}

JFrame has no paintComponent method. So you aren't override anything , and not painting will be done.

On that note JPanel does have a paintComponent method, and you should be painting on a JComponent or JPanel , which do have the method. You don't want to paint on top-level containers like JFrame . (if you really need to know though, the correct method to override is paint for JFrame ).

That being said, you should also call super.paintComponent inside the paintComponent method so you don't break the paint chain and leave paint artifacts.


Side Notes

  • As good practice, make use of the @Override annotation, so you know you are correctly overriding a method. You would've seen that paintComponent doesn't override one of JFrame s methods.

  • setVisible(true) after add your components.

  • panel.setBounds(0,0,500,500); will do absolutely nothing, since the JFrame has a default BorderLayout

  • Follow Java naming convention and use capital letters for class names.

  • Run Swing apps from the Event Dispatch Thread. See more at Initial Threads


FINAL

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

public class Sample extends JFrame {

    private JPanel panel;

    public Sample() {
        setSize(500, 500);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        panel = new JPanel() {
            @Override
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D eg = (Graphics2D) g;
                eg.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
                eg.setColor(Color.BLACK);
                eg.drawString("TEXT", 40, 120);
            }
        };

        Container mainP = getContentPane();
        mainP.add(panel);
        panel.setBackground(Color.WHITE);
        setVisible(true);
    }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM