我正在尝试编写一个程序,允许我将文本放在图像上,然后保存编辑过的图像。现在我收到的错误是:
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);
}
}
答案 0 :(得分:1)
在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);
}
});
}
}
答案 1 :(得分:0)
以下JFrame没用。因为Try1本身就是一个JFrame。
JFrame f = new JFrame("Load Image Sample");
基本上只使用Try1而不是其他Jframe。
f = new Try1();
f.pack();
f.setVisible(true);
但更重要的是,你不应该覆盖paint,而是覆盖paintComponent。请参阅Difference between paint() and paintcomponent()?。
答案 2 :(得分:0)
这是你的问题
JFrame f = new JFrame("Load Image Sample");
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
您的类正在扩展JFrame,因此请取出JFrame,然后执行
new Try1();
f.pack();
f.setVisible(true);