4

我必须使用 Swing 制作一个 Java 程序,我想显示一些文本,但不使用JLabel(包括文本组件)!我必须扩展JFrame。我还有什么其他选择?

4

1 回答 1

4

你不应该:

  • JFrame不必要地扩展
  • 直接画到JFrameviapaint(..)

你应该:

  • 覆盖JComponent_paintComponent(...)
  • 做定制绘画paintComponent(...)
  • JComponent实例添加到JFrame

这是一个例子:

在此处输入图像描述

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);
    }
}

更新:

根据您的评论,尽管它违反了所有 Swing 规则和我自己的道德

但是,如果您仍然要这样做(我无法理解为什么这只是不好的做法),方法如下:

在此处输入图像描述

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);
    }
}
于 2012-10-23T17:33:30.733 回答