Java Swing Window

2019-09-19 11:14发布

问题:

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

I am trying to make a java project that will display all 52 cards on a window. I have the window working but I cant get a card to appear on the window.

I am using eclipse for OSX, inside the project src file I have a (default package) container with my Main.java file. Then I have my cardimages folder in the same src file.

How can I get the image to show in the window?

回答1:

You should try to get the image as a URL, as a resource again using the Class method getResource(...). For example, test this:

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

Also, don't use the default package like you're doing. Put your class into a valid package.

Then try something like this:

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