2013-07-24 26 views
0

我正在尝试编写一个程序,该程序允许我在图像上放置文本,并保存编辑后的图像。现在,我得到一个错误,指出:在图像上放置透明文本字段

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

以下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()?

+0

感谢您的帮助,但我希望澄清一下。我试图设置'f'= new Try1();但是给我“错误:找不到符号。” JFrame f = new JFrame(“Load Image Sample”); f.addWindowListener(新WindowAdapter的(){ 公共无效的windowClosing(WindowEvent E){ System.exit(0);} }); 这就是我删除的所有内容。 这是现在有什么: f = new ImageTest(); f.pack(); f.setVisible(true); 如果我错过了什么,请告诉我。再次感谢你。 –

0

这是你的问题

JFrame f = new JFrame("Load Image Sample"); 


f.addWindowListener(new WindowAdapter(){ 
     public void windowClosing(WindowEvent e) { 
      System.exit(0); 
     } 
    }); 
  1. 这不是一个窗口。这是一个JFrame,所以它是不明智的把WindowAdapter的上
  2. 你的类扩展一个JFrame,所以采取了JFrame的,只是做你的

    new Try1(); 
    f.pack(); 
    f.setVisible(true); 
    
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); 
      } 
     }); 
    } 
}