1

JAVAを使用して簡単なGUIを作成しようとしています。画像が表示されません。

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.swing.*;
import javax.imageio.*;

public class EmiloLadderSnack {
    public JFrame frame=new JFrame("EmiloLadderSnack");
    public Image img;
    public Graphics g;
    public EmiloLadderSnack()
    {
        frame.setBounds(0, 0, Toolkit.getDefaultToolkit().getScreenSize().width, Toolkit.getDefaultToolkit().getScreenSize().height);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        try
        {
            img= ImageIO.read(new File("/media/01CCE00FA6888D80/Achieve/Eclipse/EmiloLadderSnack/src/photo.jpg"));
            g.drawImage(img, 50, 50, null);
        }
        catch(Exception e)
        {
            System.out.println(e.toString());
        }
    }

    public static void main(String args[])
    {
        new EmiloLadderSnack();
    }
}

Eclipseを使用しているJAVAを使用してシンプルなGUIで画像を表示するのを手伝ってください

4

2 に答える 2

4

彼/彼女が通常そうであるように、HovercraftFullOfEelsは正しいです。それはあなたが試したようには本当に見えませんでした。

チュートリアルを見てください、しかし私はホバークラフトフルオブイールが正しい方法を言うとき、ホバーは次のように意味すると信じています。

私がしたことを以下に説明させてください。まず、JFrameを拡張する新しいクラスを作成しました。JFrameは、ウィンドウ内のすべてのコンポーネントを保持するために想定されているものです。次に、JPanelを使用して、すべての図面が軽量のコンテナに含まれるようにします。StackOverflowのおかげで、発見したばかりの新しいレイアウトでレイアウトを設定しました。これは非常にありがたいことです。レイアウトはMigLayoutと呼ばれ、サードパーティのリソースです。ダウンロードしてインポートする必要があります。MigLayoutを持っている必要はありませんが、使いやすさから使用することをお勧めします。レイアウト制約を塗りつぶして中央にJPanelをドッキングした後、ペイントメソッドを変更できるようにJPanelを拡張する新しいクラスを作成しました。@Overrideを使用すると、ある意味で、その拡張クラスのメソッドを再作成できます。ご覧のとおり、その1つのグラフィックスクラスに描画すると、すべて設定されます。あなたが読むべきことがもっとたくさんあります。あなたの投稿の下のコメントを読んでください、彼らはかなり良い資料を示唆しています。

私が間違っていることは何でもホバークラフトはコメントで以下に言うでしょう。だからそれも探してください。

ホバーの修正:

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class GraphicExample extends JPanel {
   private static final String IMG_FILE_PATH = "/media/01CCE00FA6888D80/" +
        "Achieve/Eclipse/EmiloLadderSnack/src/photo.jpg";
   private BufferedImage img;

   public GraphicExample(BufferedImage img) {
      this.img = img;
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      if (img != null) {
         g.drawImage(img, 0, 0, this);
      }
   }

   @Override
   public Dimension getPreferredSize() {
      if (img != null) {
         return new Dimension(img.getWidth(), img.getHeight());
      }
      return super.getPreferredSize();
   }

   private static void createAndShowGui() {
      try {
         BufferedImage img = ImageIO.read(new File(IMG_FILE_PATH));
         JFrame frame = new JFrame("GraphicExample");
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.getContentPane().add(new GraphicExample(img));
         frame.pack();
         frame.setLocationRelativeTo(null);
         frame.setVisible(true);

         // the easy way to display an image -- in a JLabel:
         ImageIcon icon = new ImageIcon(img);
         JLabel label = new JLabel(icon);
         JOptionPane.showMessageDialog(frame, label);

      } catch (IOException e) {
         e.printStackTrace();
         System.exit(-1);
      }

   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

私の最初の推奨事項:

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;

import net.miginfocom.swing.MigLayout;

public class DrawCircle extends JFrame {

    JPanel panel;

    public DrawCircle(String title, int width, int height) {
        this.setTitle(title);
        this.setSize(width, height);
        this.setLocationRelativeTo(null); // Center JFrame
        this.setLayout(new MigLayout("fill"));  // Download external jar
        this.panel = new DrawOval();
        this.add(panel, "dock center");  // Link: http://www.miglayout.com/
        this.setVisible(true);
    }

    public class DrawOval extends JPanel {

            Color color = new Color(1, 1, 1);

        public DrawOval() {

        }

        @Override
        public void paint(Graphics g) {
            g.setColor(color.RED);
            g.fillOval(0, 0, this.getWidth(), this.getHeight());
        }
    }

}
于 2012-07-08T19:37:25.440 に答える
1

これがコンパイルされているとは想像できません。が必要NullPointerExceptionです。

何かを描画したい場合は、通常、サブクラス化して、次のようにメソッドでJPanel描画を行います。paintComponent()

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawImage(img, 50, 50, null);
}
于 2012-07-08T19:22:30.893 に答える