Java 无法使图像显示在swing中
我是一名大学生,这是我第一次用Java创建gui。现在我看了这个答案,按照说明做了,但还是什么都没发生。这是代码。我删掉了所有不相关的垃圾Java 无法使图像显示在swing中,java,swing,embedded-resource,Java,Swing,Embedded Resource,我是一名大学生,这是我第一次用Java创建gui。现在我看了这个答案,按照说明做了,但还是什么都没发生。这是代码。我删掉了所有不相关的垃圾 import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Lab4Shell { // this holds the current game board private
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Lab4Shell
{
// this holds the current game board
private char[][] gameBoard = new char[7][8];
private JButton[][] gameButtons = new JButton[7][8];
private ImageIcon red = new ImageIcon("Red.jpg");
private ImageIcon black = new ImageIcon("Black.jpg");
private ImageIcon empty = new ImageIcon("Empty.jpg");
private JPanel panel = new JPanel();
private int currentPlayer = 1;
private int numMoves = 0;
//Why do you want everything in constructor?
public Lab4Shell()
{
CreateWindow();
ResetGame();
// set layout
// loop through buttons array creating buttons, registering event handlers and adding buttons to panel
// add panel to frame
// do other initialization of applet
}
public static void CreateWindow()
{
//Sets window title and create window object.
JFrame aWindow = new JFrame("Connect Four");
//Set window position and size
aWindow.setBounds(500,100,400,400);
//What close button does.
aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Make window visible.
aWindow.setVisible(true);
}
public static void main(String args[])
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
Lab4Shell game = new Lab4Shell();
}
});
}
void ResetGame()
{
JLabel label = new JLabel();
label.setIcon(empty);
for(int r=0;r<gameBoard.length;r++)
{
java.util.Arrays.fill(gameBoard[r],0,gameBoard[r].length,'0');
//loop through board columns
for(int c=0;c<gameBoard[r].length;c++)
{
}
}
// loop through array setting char array back to ' ' and buttons array back to empty pic
// reset currentPlayer and numMoves variables
}
你可以试试这个
BufferedImage myPicture = ImageIO.read(new File("path-to-file"));
JLabel picLabel = new JLabel(new ImageIcon( myPicture ));
add( picLabel );
你可以试试这个
BufferedImage myPicture = ImageIO.read(new File("path-to-file"));
JLabel picLabel = new JLabel(new ImageIcon( myPicture ));
add( picLabel );
正如Manos所说,您必须将创建的图像图标添加到面板中,并且作为Eclipse项目src文件夹中的图像,请执行以下操作:
java.net.URL url = getClass().getResource("red.JPEG");
ImageIcon red = new ImageIcon(url);
正如Manos所说,您必须将创建的图像图标添加到面板中,并且作为Eclipse项目src文件夹中的图像,请执行以下操作:
java.net.URL url = getClass().getResource("red.JPEG");
ImageIcon red = new ImageIcon(url);
如果资源与jar中的应用程序一起嵌入,则需要使用ClassgetResource加载它们 加载图像的首选机制是通过。它支持更多的图像格式,并提供了一个可插拔的体系结构,并保证一旦read方法返回,就可以显示图像
BufferedImage redImage;
// ...
URL url = getClass().getResource("red.JPEG");
if (url != null) {
redImage = ImageIO.read(url);
} else {
throw new NullPointerException("Unable to locate red image resource");
}
如果资源与jar中的应用程序一起嵌入,则需要使用ClassgetResource加载它们 加载图像的首选机制是通过。它支持更多的图像格式,并提供了一个可插拔的体系结构,并保证一旦read方法返回,就可以显示图像
BufferedImage redImage;
// ...
URL url = getClass().getResource("red.JPEG");
if (url != null) {
redImage = ImageIO.read(url);
} else {
throw new NullPointerException("Unable to locate red image resource");
}
你从来没有在你的框架中添加过任何东西,这导致了你的问题。因此,在createWindow方法中,需要调用:
aWindow.setContentPane(panel);
然后,像在resetGame方法中一样,您将向面板添加类似JLabel的内容:
panel.add(empty);
将其添加到面板的位置由面板的位置决定。面板中有许多这样的位置-默认为BorderLayout
其他有用的东西:
通常,在有意义的情况下,在构造函数中创建/初始化对象,并在运行时添加/删除/更新它们。
对于疑难解答,请在希望看到的内容上使用.setOpaquetrue和.setBackgroundColor.blue方法。如果你当时没有看到它,要么是有什么东西在掩盖它,要么就是它从未被添加
祝你好运。你从来没有在你的相框上添加过任何东西,这会导致你的问题。因此,在createWindow方法中,需要调用:
aWindow.setContentPane(panel);
然后,像在resetGame方法中一样,您将向面板添加类似JLabel的内容:
panel.add(empty);
将其添加到面板的位置由面板的位置决定。面板中有许多这样的位置-默认为BorderLayout
其他有用的东西:
通常,在有意义的情况下,在构造函数中创建/初始化对象,并在运行时添加/删除/更新它们。
对于疑难解答,请在希望看到的内容上使用.setOpaquetrue和.setBackgroundColor.blue方法。如果你当时没有看到它,要么是有什么东西在掩盖它,要么就是它从未被添加
祝你好运。Im使用Eclipse,图像位于程序的bin文件夹中。对于实验,你可以使用UI图标,如图所示。@redelman431:如果你手动操作,请查看如何使用,并进一步说明。Im使用Eclipse,图像位于程序的bin文件夹中。对于实验,你可以使用UI图标,如图所示。@redelman431:如果您是手动操作,请查看如何执行此操作,以便进一步澄清。+1表示修订版;另请参见此。add来自哪个类?我的编译器不喜欢它。修订版+1;另请参见此。add来自哪个类?我的编译器不喜欢它。但是上面的ImageSource实例变量呢?我的教授想让我用这些。我应该把它放在构造函数中吗?但我会尝试一下你的想法的,伙计,尝试一下,你就会发现它会奏效。您可以放入构造函数,如下所示:ImageIcon image=new ImageIcongetClass.getResource/red.JPEG;但是上面的ImageSource实例变量呢?我的教授想让我用这些。我应该把它放在构造函数中吗?但我会尝试一下你的想法的,伙计,尝试一下,你就会发现它会奏效。您可以放入构造函数,如下所示:ImageIcon image=new ImageIcongetClass.getResource/red.JPEG;非常感谢你!你是个英雄,非常感谢!你是个英雄。