JAVA显示图像-GUI

标签 java swing user-interface

我正在尝试使用 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();
    }
}

请帮助我使用 JAVA 在我的简单 GUI 中显示图像 我正在使用 Eclipse

最佳答案

Hovercraft Full Of Eels 是对的,他/她通常是这样的。它看起来真的不像你尝试过的那样。

看看教程,但我相信当 Hovercraft Full Of Eels 说的正确方式时,hover 的意思如下。

让我解释一下我在下面做了什么。首先,我创建了一个扩展 JFrame 的新类。 JFrame 是用来保存窗口中所有组件的。然后在 JPanel 上绘图,这样您的所有绘图都包含在一个轻量级容器中。由于 StackOverflow,我使用刚刚发现的新布局设置了布局,我非常感谢。该布局称为 MigLayout,它是第三方资源。您必须下载并导入它。 请注意,您不必拥有 MigLayout,但由于它易于使用,因此最好使用。在我将布局约束设置为填充并将 JPanel 停靠在中心后,我创建了一个扩展 JPanel 的新类,以便我可以更改 paint 方法。 @Override 允许您在某种程度上为该扩展类重新创建方法。正如您所看到的那样,一旦绘制到那个图形类,您就已经准备就绪。您还应该阅读更多内容。 阅读您帖子下方的评论,它们提供了相当不错的 Material 。

任何我弄错的 Hovercraft 都会在下面的评论中说出来。所以也要寻找它。

悬停更正:

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

}

关于JAVA显示图像-GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11385823/

相关文章:

android - 如何实现类似 UI 的 Google Reader?

java - 使用自定义比较器的最大流

java - 如何将 Java 中 SQL 查询的结果打印到每列的单独 JTextField 中?

java - 如何让 DocumentFilter 与 JComboBox 一起使用

java - JFrame 打开空白,运行循环,然后显示组件

user-interface - UI 设计 - 是否包含取消按钮?

java - Java 中 NaN 的数学规则

java - 如何为 Long 值 < 10 添加零前缀,例如 DB Id?

java - 在Java中解析Java日期格式

java - 在按钮中插入字符数组