Java 图形用户界面出现图像问题';s
嘿,伙计们,我是一个初学者程序员,目前正在开发GUI。我知道如何将图像添加到JFrame的内容窗格并使其显示,但我必须做的是,当我在gui中玩这个游戏时,我进入的每个房间都必须在gui中显示一个新图像。下面是我的createRooms方法Java 图形用户界面出现图像问题';s,java,swing,user-interface,Java,Swing,User Interface,嘿,伙计们,我是一个初学者程序员,目前正在开发GUI。我知道如何将图像添加到JFrame的内容窗格并使其显示,但我必须做的是,当我在gui中玩这个游戏时,我进入的每个房间都必须在gui中显示一个新图像。下面是我的createRooms方法 private void createRooms(){ Room outside, theater, pub, lab, office, transporter1, transporter2; // create the rooms
private void createRooms(){
Room outside, theater, pub, lab, office, transporter1, transporter2;
// create the rooms
outside = new Room("outside the main entrance of the university");
theater = new Room("in a lecture theater");
pub = new Room("in the campus pub");
lab = new Room("in a computing lab");
office = new Room("in the computing admin office");
transporter1 = new TransporterRoom("in space");
transporter2 = new TransporterRoom("in a weird dimension");
// initialise room exits
outside.setExit("east", theater);
outside.setExit("south", lab);
outside.setExit("west", transporter2);
theater.setExit("west", outside);
// transporter2.getExit();
pub.setExit("east", outside);
lab.setExit("north", outside);
lab.setExit("east", office);
office.setExit("west", lab);
currentRoom = outside; // start game outside
}
我想做的是为每个房间添加一个图像图标,但我不完全确定如何做到这一点。提前感谢,如果您还需要我的代码,请告诉我
编辑:这是房间课程的代码,谢谢代码忍者指出这一点
private String description;
private HashMap<String, Room> exits; // stores exits of this room.
public static ArrayList<Room> rooms = new ArrayList<Room>();
/**
* Create a room described "description". Initially, it has
* no exits. "description" is something like "a kitchen" or
* "an open court yard".
* @param description The room's description.
*/
public Room(String description)
{
this.description = description;
exits = new HashMap<String, Room>();
image = new ImageIcon("images");
rooms.add(this);
}
/**
* Define an exit from this room.
* @param direction The direction of the exit.
* @param neighbor The room to which the exit leads.
*/
public void setExit(String direction, Room neighbor)
{
exits.put(direction, neighbor);
}
/**
* @return The short description of the room
* (the one that was defined in the constructor).
*/
public String getShortDescription()
{
return description;
}
/**
* Return a description of the room in the form:
* You are in the kitchen.
* Exits: north west
* @return A long description of this room
*/
public String getLongDescription()
{
return ("You are " + description + ".\n" + getExitString());
}
/**
* Return a string describing the room's exits, for example
* "Exits: north west".
* @return Details of the room's exits.
*/
private String getExitString()
{
String returnString = "Exits:";
Set<String> keys = exits.keySet();
for(String exit : keys) {
returnString += " " + exit;
}
return returnString;
}
/**
* Return the room that is reached if we go from this room in direction
* "direction". If there is no room in that direction, return null.
* @param direction The exit's direction.
* @return The room in the given direction.
*/
public Room getExit(String direction)
{
return exits.get(direction);
}
私有字符串描述;
私有哈希映射退出;//这个房间的出口。
公共静态ArrayList房间=新建ArrayList();
/**
*创建一个描述为“描述”的房间。最初,它是这样的
*没有出口。“描述”类似于“厨房”或
*“露天庭院”。
*@param description房间的描述。
*/
公共房间(字符串描述)
{
this.description=描述;
exits=newhashmap();
图像=新图像图标(“图像”);
房间。添加(此);
}
/**
*定义此房间的出口。
*@param方向出口的方向。
*@param是出口通向的房间的邻居。
*/
公共void setExit(字符串方向,房间邻居)
{
出口。放(方向,邻居);
}
/**
*@返回房间的简短描述
*(构造函数中定义的一个)。
*/
公共字符串getShortDescription()
{
返回说明;
}
/**
*以以下格式返回房间的说明:
*你在厨房里。
*出口:西北
*@返回此房间的详细描述
*/
公共字符串getLongDescription()
{
return(“您是”+description+“\n”+getExitString());
}
/**
*例如,返回描述房间出口的字符串
*“出口:西北”。
*@返回房间出口的详细信息。
*/
私有字符串getExitString()
{
String returnString=“Exits:”;
Set keys=exits.keySet();
用于(字符串出口:键){
returnString+=“”+退出;
}
返回字符串;
}
/**
*如果我们从这个房间出发,返回到达的房间
*“方向”。如果该方向没有空间,则返回null。
*@param方向出口的方向。
*@按指定方向返回房间。
*/
公共房间getExit(字符串方向)
{
返回出口。获取(方向);
}
}有两种方法可以做到这一点。 首先,您可以将JLabel添加到contentPane中,并将image设置为该JLabel。但这并不是最好的方法,因为若你们想把组件放在上面,你们必须使用绝对布局。 第二个是将JPanel放入框架中并实现paintComponent方法
JPanel imagePanel= new JPanel(){
@Override
public void painComponent(Graphics g){
super.painComponent(g);
g.drawImage(yourPrefImage,0,0,sizeOfPanelX(Frame),sizeOfPanelY(Frame),null);
}
}
房间
课程的代码将非常有用