icons在java显示出来_在Java Swing中显示图像

public class MinesweeperMenu extends MinesweeperPanel{

private JPanel picture = new JPanel();

private JButton play = new JButton("Play");

private JButton highScores = new JButton("High Score and \nStatistics");

private JButton changeMap = new JButton("Create Custom \nor Change Map");

private JButton difficulty = new JButton("Custom or\nChange Difficulty");

private JButton user = new JButton("Change User");

Image img;

public MinesweeperMenu()

{

// Set Layout for the menu

LayoutManager menuLayout = new BoxLayout(menu, BoxLayout.Y_AXIS);

menu.setLayout(menuLayout);

// Set Layout for the window

LayoutManager windowLayout = new BorderLayout();

window.setLayout(windowLayout);

// Add buttons to the panels

menu.add(play);

menu.add(highScores);

menu.add(changeMap);

menu.add(difficulty);

menu.add(user);

// Add picture to the frame

try{

File input = new File("./setup/images/MineMenuPicture.jpg");

img = ImageIO.read(input);

}

catch(IOException ie)

{

System.out.println(ie.getMessage());

}

// Add action listeners

changeMap.addActionListener(new ChangeMapListener());

}

public void paintComponent(Graphics g)

{

// POSITION OF THE PICTURE

int x = 650;

int y = 585;

g.drawImage(img, x, y, null);

}

public void displayFrame()

{

// Display Frame

window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

window.setVisible(true);

}

public static void main(String[] args)

{

MinesweeperMenu menu = new MinesweeperMenu();

window.pack();

menu.displayFrame();

window.repaint();

}

}

public class MinesweeperPanel extends JFrame{

public static final Color COLOR_KEY = new Color(220, 110, 0);

// Initialize all the objects

public static JFrame window = new JFrame("Minesweeper++");

public static JPanel menu = new JPanel();

// Close the current window

public static void close()

{

window.setVisible(false);

window.dispose();

}

}

我无法让我的图像显示在框架中.我已经尝试了所有的东西,但是我得到的印象是,由于我是Java Swing的新手,所以我没有意识到这是一个错误.任何帮助将不胜感激.

解决方法:

你有一个非常令人困惑的程序结构让你自己变得困难,我建议你简化很多事情.

首先,您当前的MinesweeperMenu类不需要扩展MinesweeperPanel,后者类也不需要扩展JFrame.然后你在其他地方有一个静态JFrame – 这是太多的JFrame,并且要启动,你试图在一个JFrame中显示你的图像,但是显示另一个没有图片的那个.你的程序只需要一个JFrame,它应该可以创建,填充其内容,打包并显示在一个地方,而不是像你一样在这里散落.

你试图在paintComponent覆盖中显示图片,但是这个方法永远不会被调用,因为你的类扩展了JFrame(最终)并且JFrame没有这个方法.你正在使用正确的方法,但是类应该扩展JPanel,你应该在paintComponent方法块上面有一个@Override注释,以确保你实际上覆盖了父方法.

你应该摆脱这个程序中所有静态的东西.这里唯一静态的应该是main方法,也许是一些常量,但就是这样.

这里有更多的错误,我没有多少时间来讨论所有这些错误.考虑从头开始,从小开始,使小位工作,然后将它们加在一起.

例如,首先创建一个非常小的程序,尝试将图像读入Image对象,将其放在ImageIcon中,将ImageIcon放入JLabel,并在JOptionPane中显示JLabel,这很简单,只是为了看看你是否可以读取图像确定,例如,这样的事情:

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import javax.imageio.ImageIO;

import javax.swing.ImageIcon;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

public class TestImages {

// *** your image path will be different *****

private static final String IMG_PATH = "src/images/image01.jpg";

public static void main(String[] args) {

try {

BufferedImage img = ImageIO.read(new File(IMG_PATH));

ImageIcon icon = new ImageIcon(img);

JLabel label = new JLabel(icon);

JOptionPane.showMessageDialog(null, label);

} catch (IOException e) {

e.printStackTrace();

}

}

}

然后,当您完成此操作时,查看您是否现在可以创建一个在其paintComponent方法中显示相同Image的JPanel,并在JOptionPane中显示此JPanel.

然后创建一个JFrame并在JFrame中显示保存图像的JPanel.

通过连续迭代,您将测试概念,纠正错误并构建程序.

标签:imageicon,java,icons,swing

来源: https://codeday.me/bug/20190923/1815833.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值