Java 在JDeskopPane上编写文本
我想在JDesktopPane的右下角写一个多行文本(3-4行就可以了),我该怎么做?Java 在JDeskopPane上编写文本,java,swing,jdesktoppane,Java,Swing,Jdesktoppane,我想在JDesktopPane的右下角写一个多行文本(3-4行就可以了),我该怎么做? 文本不是固定的,它可以在每次启动swing应用程序时更改,但一旦启动应用程序,它将保持不变,我不需要从应用程序进行更新 我的第一个想法是创建一个图像,将其作为JDesktopPane的背景,然后在其上书写,但这似乎不是一个简单的解决方案 谢谢你的帮助。使用这个示例类。 背景(如果设置)将被缩放 import java.awt.AlphaComposite; import java.awt.Color; imp
文本不是固定的,它可以在每次启动swing应用程序时更改,但一旦启动应用程序,它将保持不变,我不需要从应用程序进行更新 我的第一个想法是创建一个图像,将其作为JDesktopPane的背景,然后在其上书写,但这似乎不是一个简单的解决方案 谢谢你的帮助。使用这个示例类。 背景(如果设置)将被缩放
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));
}
}
使用这个示例类。
背景(如果设置)将被缩放
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));
}
}
下面的变体中的
print()
方法结合所看到的示例和,说明了如何使用右对齐JDesktopPane
右下角的多行文本
下面的变体中的
print()
方法结合所看到的示例和,说明了如何使用右对齐JDesktopPane
右下角的多行文本
@JudeNiroshan我试着把一张图片作为JDeskopPane的背景(没有在上面写),但是图片不能缩放,我不知道如何在背景上“重复”它使用
JLabel
?覆盖组件的paintComponent
方法?@JudeNiroshan我试图将一个图像作为JDeskopPane的背景(没有在其上书写),但图像无法缩放,我不知道如何在背景上“重复”它使用JLabel
?覆盖组件的paintComponent
方法?无需调用super.paintComponent(g)
在paintComponent(…)
?@res1:这是一种尊重的方式;另一种方法是使用fillRect()
来“绘制矩形边界内包含的所有位”在paintComponent(…)
?@res1:这是一种尊重的方式;另一种方法是使用fillRect()
来“绘制矩形边界内包含的所有位。”