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);
    }
}
4

3 に答える 3

1

一般的なより良いアプローチは、 で見ることができますLabelRenderTest

複数行のテキストが必要な場合にのみ、ラベルに HTML 形式を使用する必要があります。1 行のメッセージにはプレーン テキストを使用します。

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);
            }
        });
    }
}
于 2013-07-25T03:11:31.880 に答える
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);
    
于 2013-07-24T22:04:57.660 に答える
0

次の JFrame は役に立ちません。Try1 自体が JFrame であるためです。

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

基本的に、他の Jframe の代わりに Try1 を使用します。

f = new Try1();
f.pack();
f.setVisible(true);

しかし、もっと重要なことは、ペイントをオーバーライドするべきではなく、代わりに paintComponent をオーバーライドすることです。paint() と paintcomponent() の違いを参照してください。.

于 2013-07-24T22:00:55.893 に答える