Java Swing窗口 [英] Java Swing Window

查看:158
本文介绍了Java Swing窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我正在尝试创建一个将显示的java项目窗户上的所有52张卡片。我有窗口工作,但我不能得到一张卡出现在窗口。

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.

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

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?

推荐答案

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

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.

然后尝试这样的事情:

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

这篇关于Java Swing窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆