在 JDeskopPane 上写一段文字 [英] Write a text on JDeskopPane

查看:35
本文介绍了在 JDeskopPane 上写一段文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

感谢您的帮助.

解决方案

结合

import java.awt.*;导入 javax.swing.JDesktopPane;导入 javax.swing.JFrame;导入 javax.swing.JInternalFrame;/** @see http://stackoverflow.com/a/45055215/230513 */公共类 JDPTest 扩展 JDesktopPane {private MyFrame one = new MyFrame("One", 100, 100);公共 JDPTest() {this.setPreferredSize(new Dimension(640, 480));this.add(one);}@覆盖受保护的无效paintComponent(图形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));打印(g2d,3,你好,世界!");打印(g2d,2,这是一个测试.");print(g2d, 1, "这是另一个测试.");print(g2d, 0, "这又是一个测试.");}私人无效打印(Graphics2D g2d,int line,String s){FontMetrics fm = g2d.getFontMetrics();int x = this.getWidth() - fm.stringWidth(s) - 5;int y = this.getHeight() - fm.getDescent()- 行 * (fm.getHeight() + fm.getLeading());g2d.drawString(s, x, y);}私有最终类 MyFrame 扩展 JInternalFrame {MyFrame(字符串名称,int x,int y){超级(名称,真实,真实,真实,真实);this.setSize(320, 240);this.setLocation(x, y);this.setVisible(true);}}私人无效显示(){JFrame f = new JFrame("JDPTest");f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);f.add(this);f.pack();f.setLocationRelativeTo(null);f.setVisible(true);}公共静态无效主(字符串 [] args){EventQueue.invokeLater(new Runnable() {@覆盖公共无效运行(){新的 JDPTest().display();}});}}

I want to write a multiline text (3-4 lines is ok) on the bottom right corner of a JDesktopPane, how can I do this?
The text is not fixed, it can change every time i start the swing application but once the application is started it remains the same, I don't need to update from the application.

My first thought was to create an image, put it as background of the JDesktopPane and then write on it, but it doesn't seem a simple solution.

Thanks for the help.

解决方案

Combining the examples seen here and here, the print() method in the variation below illustrates using FontMetrics to right justify multiple lines of text in the bottom, right corner of a JDesktopPane.

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

/** @see http://stackoverflow.com/a/45055215/230513 */
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();
            }
        });
    }
}

这篇关于在 JDeskopPane 上写一段文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆