Show text in Swing without JLabel

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

+4
source share
1 answer

You should not:

  • Do not use JFrame unnecessarily
  • Draw directly a JFrame through paint(..)

You must:

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

Here is an example:

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:

According to your comment , although it contradicts all Swing rules and my own morality :

However, if you still need to do this (that I cannot understand why this is just bad practice), here's 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); } } 
+4
source

Source: https://habr.com/ru/post/1441613/


All Articles