【问题标题】:Java Swing WindowJava 摆动窗口
【发布时间】:2014-01-19 21:31:39
【问题描述】:
import javax.swing.*;
import java.awt.*;

public class Main
{
    public static void main(String[] args)
    {
        //load the card image from the gif file.
        final ImageIcon cardIcon = new ImageIcon("cardimages/tenClubs.gif");

        //create a panel displaying the card image
        JPanel panel = new JPanel()
        {
            //paintComponent is called automatically by the JRE whenever
            //the panel needs to be drawn or redrawn
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                cardIcon.paintIcon(this, g, 20, 20);
            }
        };

        //create & make visible a JFrame to contain the panel
        JFrame window = new JFrame("Title goes here");
        window.add(panel);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setBackground(new Color(100, 200, 102));
        window.setPreferredSize(new Dimension(200,200));
        window.pack();
        window.setVisible(true);
    }
}

我正在尝试制作一个在窗口上显示所有 52 张卡片的 java 项目。我的窗口可以工作,但我无法让卡片出现在窗口上。

我在 OSX 上使用 eclipse,在项目 src 文件中我有一个(默认包)容器,里面有我的 Main.java 文件。然后我将我的 cardimages 文件夹放在同一个 src 文件中。

如何让图像显示在窗口中?

【问题讨论】:

  • 你测试过ImageIcon,cardIcon,是否为空吗?如果是这样,您可能在错误的位置寻找图像。我自己,我使用 ImageIO.read(...) 将我的图像作为 BufferedImages 获取,并且不传入文件,而是从 Class 方法 getResource(...) 获取的 URL。
  • 测试正常,可能图片路径不对。
  • 原始海报,您应该显示您的 cardImages 文件夹相对于您的类文件的位置。
  • 为什么你在paintComponent中的paintIcon可以工作但是......
  • 只需将您的课程移动到有效的包中即可。您的代码可以正常工作。

标签: java swing icons paintcomponent


【解决方案1】:

您应该尝试再次使用 Class 方法 getResource(...) 将图像作为 URL 获取为资源。例如,测试这个:

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;


public class DefaultFoo {
   public static void main(String[] args) throws IOException {
      String resource = "/cardimages/tenClubs.gif";
      URL url = Class.class.getResource(resource);
      BufferedImage img = ImageIO.read(url);
      Icon icon = new ImageIcon(img);
      JOptionPane.showMessageDialog(null, icon);
   }
}

另外,不要像现在这样使用默认包。将您的课程放入有效的包中。

然后尝试这样的事情:

import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.*;

@SuppressWarnings("serial")
public class PlayWithImages extends JLayeredPane {
   private static final String RESOURCE = "/cardimages/tenClubs.gif";
   private static final int PREF_W = 500;
   private static final int PREF_H = PREF_W;
   private static final int CARD_COUNT = 8;

   public PlayWithImages() throws IOException {
      URL url = getClass().getResource(RESOURCE);
      BufferedImage img = ImageIO.read(url);
      Icon icon = new ImageIcon(img);

      for (int i = 0; i < CARD_COUNT; i++) {
         JLabel label = new JLabel(icon);
         label.setSize(label.getPreferredSize());
         int x = PREF_W - 20 - i * 40 - label.getWidth();
         int y = 20;
         label.setLocation(x, y);
         add(label);
      }
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PREF_W, PREF_H);
   }

   private static void createAndShowGui() {
      PlayWithImages mainPanel = null;
      try {
         mainPanel = new PlayWithImages();
      } catch (IOException e) {
         e.printStackTrace();
         System.exit(-1);
      }

      JFrame frame = new JFrame("PlayWithImages");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 2019-05-12
    • 1970-01-01
    • 1970-01-01
    • 2014-09-09
    • 2014-09-22
    相关资源
    最近更新 更多