Java 在图像上放置透明文本字段
我正在尝试编写一个程序,允许我将文本放在图像上,并保存编辑后的图像。现在我得到一个错误,上面写着:Java 在图像上放置透明文本字段,java,image,text,Java,Image,Text,我正在尝试编写一个程序,允许我将文本放在图像上,并保存编辑后的图像。现在我得到一个错误,上面写着: Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container 当我运行代码时,它会显示文本框和没有图像的白色背景。在此方面的任何帮助都将不胜感激。现在我只关注图像上的文本字段。提前谢谢你! 代码如下: import java.awt.*; import java.awt
Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container
当我运行代码时,它会显示文本框和没有图像的白色背景。在此方面的任何帮助都将不胜感激。现在我只关注图像上的文本字段。提前谢谢你!
代码如下:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
import java.util.TreeSet;
public class Try1 extends JFrame {
public Try1() {
initializeUI();
}
BufferedImage img;
public void paint(Graphics g) {
g.drawImage(img, 0, 0, null);
}
public void LoadImage() {
try {
img = ImageIO.read(new File("savedimage.jpg"));
}
catch (IOException e){}
}
private void initializeUI() {
JPanel panel = new JPanel(null);
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextField textField = new JTextField(20);
textField.setBounds(50, 50, 100, 20);
panel.add(textField);
setContentPane(panel);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Try1().setVisible(true);
}
});
JFrame f = new JFrame("Load Image Sample");
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.add(new Try1());
f.pack();
f.setVisible(true);
}
}
遵循JFrame是无用的。因为Try1本身就是一个JFrame
JFrame f = new JFrame("Load Image Sample");
基本上只是使用Try1而不是其他Jframe
f = new Try1();
f.pack();
f.setVisible(true);
但更重要的是,您不应该覆盖paint,而应该覆盖paintComponent。请参阅。这是您的问题
JFrame f = new JFrame("Load Image Sample");
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
new Try1();
f.pack();
f.setVisible(true);
通常,可以在
LabelRenderTest
中看到更好的方法
如果需要多行文本,则只需在标签中使用HTML格式。对单行消息使用纯文本
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class LabelRenderTest {
public static void main(String[] args) {
SwingUtilities.invokeLater( new Runnable() {
public void run() {
String title = "<html><body style='width: 200px; padding: 5px;'>"
+ "<h1>Do U C Me?</h1>"
+ "Here is a long string that will wrap. "
+ "The effect we want is a multi-line label.";
JFrame f = new JFrame("Label Render Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BufferedImage image = new BufferedImage(
400,
300,
BufferedImage.TYPE_INT_RGB);
Graphics2D imageGraphics = image.createGraphics();
GradientPaint gp = new GradientPaint(
20f,
20f,
Color.red,
380f,
280f,
Color.orange);
imageGraphics.setPaint(gp);
imageGraphics.fillRect(0, 0, 400, 300);
JLabel textLabel = new JLabel(title);
textLabel.setSize(textLabel.getPreferredSize());
Dimension d = textLabel.getPreferredSize();
BufferedImage bi = new BufferedImage(
d.width,
d.height,
BufferedImage.TYPE_INT_ARGB);
Graphics g = bi.createGraphics();
g.setColor(new Color(255, 255, 255, 128));
g.fillRoundRect(
0,
0,
bi.getWidth(f),
bi.getHeight(f),
15,
10);
g.setColor(Color.black);
textLabel.paint(g);
Graphics g2 = image.getGraphics();
g2.drawImage(bi, 20, 20, f);
ImageIcon ii = new ImageIcon(image);
JLabel imageLabel = new JLabel(ii);
f.getContentPane().add(imageLabel);
f.pack();
f.setLocationByPlatform(true);
f.setVisible(true);
}
});
}
}
import java.awt.*;
导入java.awt.image.buffereImage;
导入javax.swing.*;
公共类LabelRenderTest{
公共静态void main(字符串[]args){
SwingUtilities.invokeLater(新的Runnable(){
公开募捐{
String title=“”
+“你喜欢我吗?”
+“这是一条可以缠绕的长字符串。”
+“我们想要的效果是多行标签。”;
JFrame f=新JFrame(“标签渲染测试”);
f、 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BuffereImage=新的BuffereImage(
400,
300,
BuffereImage.TYPE_INT_RGB);
Graphics2D imageGraphics=image.createGraphics();
GradientPaint gp=新的GradientPaint(
20楼,
20楼,
颜色,红色,
380f,
280f,
颜色(橙色);
图像图形.setPaint(gp);
imageGraphics.fillRect(0,0,400,300);
JLabel textLabel=新的JLabel(标题);
textLabel.setSize(textLabel.getPreferredSize());
维度d=textLabel.getPreferredSize();
BuffereImage bi=新的BuffereImage(
d、 宽度,
d、 高度,
BuffereImage.TYPE_INT_ARGB);
Graphics g=bi.createGraphics();
g、 setColor(新颜色(255、255、255、128));
g、 fillRoundRect(
0,
0,
bi.getWidth(f),
bi.getHeight(f),
15,
10);
g、 设置颜色(颜色为黑色);
textLabel.paint(g);
Graphics g2=image.getGraphics();
g2.绘图图像(bi,20,20,f);
ImageIcon ii=新的ImageIcon(图像);
JLabel imageLabel=新的JLabel(ii);
f、 getContentPane().add(imageLabel);
f、 包装();
f、 setLocationByPlatform(真);
f、 setVisible(真);
}
});
}
}
感谢您迄今为止的帮助,但我想澄清一下。我试图设置'f'=新Try1();但是给我的是“错误:找不到符号”;f、 addWindowListener(新WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});这就是我删除的所有内容。这就是现在的情况:f=newImageTest();f、 包装();f、 setVisible(真);如果我错过了什么,请告诉我。再次感谢你。