在 JDeskopPane 上写一段文字

Write a text on JDeskopPane

我想在 JDesktopPane 的右下角写多行文本(3-4 行即可),我该怎么做?
文本不是固定的,每次我启动 swing 应用程序时它都会改变,但是一旦应用程序启动它就保持不变,我不需要从应用程序更新。

我的第一个想法是创建一个图像,将其作为 JDesktopPane 的背景,然后在上面写字,但这似乎不是一个简单的解决方案。

感谢您的帮助。

使用这个例子class。 背景如果设置将被缩放。

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.swing.JDesktopPane;

public class MyDesktopPane extends JDesktopPane {
    Image img;
    public MyDesktopPane() {
        super();
    }

@Override
public void paintComponent(Graphics g) {
    int width = this.getWidth();
    int height = this.getHeight();
    int infoWidth = 150;
    int infoHeight = 100;
    super.paintComponent(g);

    // alpha
    final Float alpha = new Float(0.9);
    final Graphics2D g2d = (Graphics2D) g;
    g2d.setComposite(makeComposite(alpha.floatValue()));

    // draw bacground image is set
    if (img != null) {
        g.drawImage(img, 0, 0, width, height, this);
    }

    //draw 3 line text in red reound rectangle
    g.setColor(Color.RED);
    g.fillRoundRect(width - infoWidth, height - infoHeight, infoWidth, infoHeight, 5, 5);
    g.setColor(Color.BLACK);

    g.drawString("Line 1", width - infoWidth + 5, height - infoHeight + 20);
    g.drawString("Line 2", width - infoWidth + 5, height - infoHeight + 40);
    g.drawString("Line 3", width - infoWidth + 5, height - infoHeight + 60);

}

public void setBackGroundImage(String path) {
    try {
        boolean file = new File(path).isFile();
        if (file) {
            img = javax.imageio.ImageIO.read(new FileInputStream(path));
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    this.repaint();
}

private AlphaComposite makeComposite(final float alpha) {
    final int type = AlphaComposite.SRC_OVER;
    return (AlphaComposite.getInstance(type, alpha));
}
}

结合看到的示例 here and here, the print() method in the variation below illustrates using FontMetrics 右对齐 JDesktopPane.

底部右角的多行文本

import java.awt.*;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;

/** @see  */
public class JDPTest extends JDesktopPane {

    private MyFrame one = new MyFrame("One", 100, 100);

    public JDPTest() {
        this.setPreferredSize(new Dimension(640, 480));
        this.add(one);
    }

    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setColor(Color.lightGray);
        g2d.fillRect(0, 0, getWidth(), getHeight());
        g2d.setColor(Color.BLACK);
        g2d.setFont(new Font(Font.SERIF, Font.BOLD, 16));
        print(g2d, 3, "Hello, world!");
        print(g2d, 2, "This is a test.");
        print(g2d, 1, "This is another test.");
        print(g2d, 0, "This is still another test.");
    }

    private void print(Graphics2D g2d, int line, String s) {
        FontMetrics fm = g2d.getFontMetrics();
        int x = this.getWidth() - fm.stringWidth(s) - 5;
        int y = this.getHeight() - fm.getDescent()
            - line * (fm.getHeight() + fm.getLeading());
        g2d.drawString(s, x, y);
    }

    private final class MyFrame extends JInternalFrame {

        MyFrame(String name, int x, int y) {
            super(name, true, true, true, true);
            this.setSize(320, 240);
            this.setLocation(x, y);
            this.setVisible(true);
        }
    }

    private void display() {
        JFrame f = new JFrame("JDPTest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new JDPTest().display();
            }
        });
    }
}