问题
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.
回答1:
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();
}
});
}
}
回答2:
Use this example class. The background if set will be scaled.
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));
}
}
来源:https://stackoverflow.com/questions/45052346/write-a-text-on-jdeskoppane