Java-设置JLabel的宽度/高度
我正在将图像添加到JLabel中,并尝试设置宽度和高度。。。但它不起作用。。。帮忙 正如你在这里看到的。。。瓷砖不是32乘32 代码:Java-设置JLabel的宽度/高度,java,applet,jlabel,Java,Applet,Jlabel,我正在将图像添加到JLabel中,并尝试设置宽度和高度。。。但它不起作用。。。帮忙 正如你在这里看到的。。。瓷砖不是32乘32 代码: import java.applet.applet; 导入java.awt.*; 导入javax.swing.*; 导入java.lang.*; 导入java.util.*; 公共类LayoutExample扩展了JApplet{ 标签msgL=新标签(“消息”); 标签状态L=新标签(“状态”); Label inventoryTitleL=新标签(“您的
import java.applet.applet;
导入java.awt.*;
导入javax.swing.*;
导入java.lang.*;
导入java.util.*;
公共类LayoutExample扩展了JApplet{
标签msgL=新标签(“消息”);
标签状态L=新标签(“状态”);
Label inventoryTitleL=新标签(“您的库存”);
标签idL=新标签(“名称、统计数据”);
Label messageInL=新标签(“在此处读取消息”);
Label messageOutL=新标签(“在此处键入消息”);
JLabel标签2;
JPanel-topP;
JPanel-topRightP;
JPanel-gameGridP;
JPanel清单;
JPanel inventoryTitleP;
JPanel inventoryGridP;
JPanel idP;
JPanel-messageInP;
JPanel-messageOutP;
按钮槽;
文本区域聊天;
TextField-msg;
/**博弈变量**/
图像[]瓷砖;
图像[]武器;
图像播放器;//播放器图像
int x,y,px,py,tx,ty;//x tile-y tile//player x-player y//tile x-tile y
布尔左,右,下,上,可以移动,重生;
布尔拉丝连枷,拉丝斧;
公共静态int[][]板;
最终整块数=522;
最终整数WEAPS=2;
挑选的武器;
标签lx,ly;//看看我们在哪里!
int r1,r2,u1,u2,l1,l2,d1,d2,spawnX,spawnY;
int右侧=480;
整数下行=320;
int-mapX=15;
int-mapY=10;
字符串txtLastFX,txtLastFY;
boolean boolRC、boolLC、boolUC、boolDC;
int leftMap、upMap、rightMap、downMap、currentMap、map;
公共静态TextArea c=新TextArea();
public static TextArea stats=new TextArea();
公共静态字符串换行符;
公共静态java.awt.List;
公共静态字体时间r=新字体(“等距”,Font.BOLD,15);
公共静态数组列表arr;
公共静态tileGen系统;
公共静态布尔值firstpush=false;
字符串名称、描述、类型、攻击添加、防御添加、canSell、canEat、Earned Coins、canEquip;
公共静态int blkStr;
int lastX、lastY、row、col;
标签lbl1、lbl2、p1、p2、lblRC;
字符串用户名;
智力攻击,防御,黄金,等级,经验;
公共void init(){
设置最小尺寸(新尺寸(840520));
瓷砖=新图像[522];
对于(int i=0;i<522;i++){
tiles[i]=getImage(getClass().getResource(String.format(“line_tile/t%d.png”,i));
}
板=载重板(1);
GridLayout frameGL=新的GridLayout(3,2);
frameGL.setVgap(0);
frameGL.setHgap(0);
//setLayout(新网格布局(3,1));
setLayout(frameGL);
topP=新JPanel(新网格布局(1,2));
//顶部立根(0,0640520);;
顶部设置最小尺寸(新尺寸(840520));
顶部设置尺寸(新尺寸(840520));
GridLayout gameGridGL=新的GridLayout(15,10);
gameGridGL.setVgap(0);
gameGridGL.setHgap(0);
gameGridP=新JPanel(gameGridGL);
gameGridP.setPreferredSize(新维度(580320));
顶部添加(gameGridP);
topRightP=新JPanel(新网格布局(2,1));
inventoryP=新JPanel(新网格布局(2,1));
inventoryTitleP=新的JPanel(新的FlowLayout());
库存量(480,0160,30);
inventoryGridP=新JPanel(新网格布局(5,4));
//存货清单(480,0160128);
//目录网格设置高度(“160px”);
inventoryGridP.setPreferredSize(新维度(100128));
idP=新JPanel(新网格布局(3,1));
messageInP=newjpanel(newflowlayout());
messageOutP=newjpanel(newflowlayout());
聊天=新文本区(10,50);
msg=新文本字段(50);
inventorytilep.add(inventorytilel);
inventoryP.add(inventorytilep);
顶部添加(inventoryGridP);
inventoryP.add(inventorytilep);
inventoryP.add(inventoryGridP);
topRightP.add(inventoryP);
对于(int number=1;number,发布的代码存在许多问题:
a) 不要在Swing应用程序中使用AWT组件。所有使用的组件都应该以“J”开头(即JLabel、JTextArea、JTextField)
b) 您发布的代码是从旧的AWT小程序复制而来的。Swing小程序的编码不同。请先阅读上的Swing教程中的部分,了解一个基本示例。例如,您永远不应该覆盖JApplet、JFrame或JDialog中的update()或paint()方法
c) 无需设置JLabel的大小。标签将自动为添加到标签中的图标的大小
清理代码,如果仍然有问题,请发布SSCCE()。我不打算浏览100行代码来找出您读取图像的位置并将其添加到标签中。通常,您可以调用label.setMinimumSize(新尺寸(宽度、高度))
将某个东西强制设置为某个大小。不确定您在这里到底想做什么-您的SSCE并不是全部
import java.applet.Applet;
import java.awt.*;
import javax.swing.*;
import java.lang.*;
import java.util.*;
public class LayoutExample extends JApplet {
Label msgL = new Label("Messages");
Label statusL = new Label("Status");
Label inventoryTitleL = new Label("Your Inventory");
Label idL = new Label("Name, Stats");
Label messageInL = new Label("read message here");
Label messageOutL = new Label("type message here");
JLabel label2;
JPanel topP;
JPanel topRightP;
JPanel gameGridP;
JPanel inventoryP;
JPanel inventoryTitleP;
JPanel inventoryGridP;
JPanel idP;
JPanel messageInP;
JPanel messageOutP;
JButton slot;
TextArea chat;
TextField msg;
/** Game variables **/
Image[] tiles;
Image[] weapon;
Image player; // player image
int x, y, px, py, tx, ty; // x tile - y tile // player x - player y // tile x - tile y
boolean left, right, down, up, canMove, respawn;
boolean drawFlail, drawBattleaxe;
public static int[][] board;
final int NUM_TILES = 522;
final int NUM_WEAPS = 2;
boolean[] weaponPicked;
Label lx, ly; // to see where we are!
int r1, r2, u1,u2,l1,l2,d1,d2, spawnX, spawnY;
int rightSide = 480;
int downSide = 320;
int mapX = 15;
int mapY = 10;
String txtLastFX, txtLastFY;
boolean boolRC, boolLC, boolUC, boolDC;
int leftMap, upMap, rightMap, downMap, currentMap, map;
public static TextArea c = new TextArea();
public static TextArea stats = new TextArea();
public static String newline;
public static java.awt.List list;
public static Font TimesR = new Font("MonoSpaced", Font.BOLD, 15);
public static ArrayList<String> arr;
public static tileGen anItem;
public static boolean firstpush = false;
String name, desc, typeOf, attackAdd, defenseAdd, canSell, canEat,earnedCoins,canEquip;
public static int blkStr;
int lastX, lastY, row, col;
Label lbl1, lbl2, p1, p2, lblRC;
String username;
int attack, defense, gold, level, exp;
public void init() {
setMinimumSize(new Dimension(840,520));
tiles = new Image[522];
for(int i = 0;i < 522;i++) {
tiles[i] = getImage(getClass().getResource(String.format("line_tile/t%d.png", i)));
}
board = loadBoard(1);
GridLayout frameGL = new GridLayout(3,2);
frameGL.setVgap(0);
frameGL.setHgap(0);
//setLayout(new GridLayout(3,1));
setLayout(frameGL);
topP = new JPanel(new GridLayout(1,2));
//topP.setBounds(0,0,640,520);
topP.setMinimumSize(new Dimension(840,520));
topP.setSize(new Dimension(840,520));
GridLayout gameGridGL = new GridLayout(15,10);
gameGridGL.setVgap(0);
gameGridGL.setHgap(0);
gameGridP = new JPanel(gameGridGL);
gameGridP.setPreferredSize(new Dimension(580,320));
topP.add(gameGridP);
topRightP = new JPanel(new GridLayout(2,1));
inventoryP = new JPanel(new GridLayout(2,1));
inventoryTitleP = new JPanel(new FlowLayout());
inventoryTitleP.setBounds(480,0,160,30);
inventoryGridP = new JPanel(new GridLayout(5,4));
//inventoryGridP.setBounds(480,0,160,128);
//inventoryGridP.setHeight("160px");
inventoryGridP.setPreferredSize(new Dimension(100,128));
idP = new JPanel(new GridLayout(3,1));
messageInP = new JPanel(new FlowLayout());
messageOutP = new JPanel(new FlowLayout());
chat = new TextArea(10,50);
msg = new TextField(50);
inventoryTitleP.add(inventoryTitleL);
inventoryP.add(inventoryTitleP);
topP.add(inventoryGridP);
inventoryP.add(inventoryTitleP);
inventoryP.add(inventoryGridP);
topRightP.add(inventoryP);
for (int number = 1; number <= 20; number++) {
inventoryGridP.add(slot = new JButton("Slot " + number));
}
idP.add(idL);
topRightP.add(idP);
topP.add(topRightP);
add(topP);
messageOutP.add(chat);
messageOutP.setSize(640,20);
//add(messageOutP);
messageInP.add(msg);
messageInP.setSize(150,640);
//add(messageInP);
player = getImage(getClass().getResource("me.gif")); // our player
}
private static final HashSet<Integer> BLOCKED_TILES = new HashSet<Integer>();
static {
BLOCKED_TILES.add(118);
BLOCKED_TILES.add(67);
BLOCKED_TILES.add(190);
BLOCKED_TILES.add(40);
BLOCKED_TILES.add(135);
BLOCKED_TILES.add(520);
BLOCKED_TILES.add(25);
BLOCKED_TILES.add(91);
BLOCKED_TILES.add(394);
BLOCKED_TILES.add(1);
BLOCKED_TILES.add(121);
//add more tiles here
}
public void stop() {
}
public void paint(Graphics g) {
for (row = 0; row < board.length; row++) {
for (col = 0; col < board[row].length; col++) {
int index = board[row][col];
Icon newTile = new ImageIcon(tiles[index]);
label2 = new JLabel(newTile);
//g.drawImage(tiles[index], 32 * col, 32 * row, this);
gameGridP.add(label2);
System.out.println(row + " " + col);
}
}
}
public void update(Graphics g)
{
paint(g);
}
public int[][] loadBoard(int map) {
if (map == 1) {
return new int[][] {
{ 118, 118, 118, 118, 34, 343, 67, 1, 1, 1, 1, 1, 1, 1, 343, 343
},
{ 118, 118, 118, 118, 34, 343, 1, 1, 1, 1, 7, 40, 7, 1, 343, 343
},
{ 118, 118, 34, 34, 34, 34, 135, 167, 167, 1, 167, 167, 167, 1, 343, 343
},
{ 118, 118, 34, 343, 343, 343, 1, 167, 167, 167, 167, 167, 167, 1, 343, 343
},
{ 118, 67, 34, 343, 343, 343, 1, 1, 1, 1, 1, 1, 1, 1, 343, 343
},
{ 118, 67, 34, 343, 394, 343, 1, 167, 167, 167, 167, 167, 167, 1, 343, 343
},
{ 67, 67, 34, 343, 343, 343, 1, 167, 167, 167, 167, 167, 167, 1, 343, 343
},
{ 34, 34, 34, 34, 34, 343, 1, 1, 167, 167, 167, 167, 167, 1, 343, 343
},
{ 67, 121, 121, 67, 34, 343, 70, 1, 439, 25, 91, 25, 439, 1, 343, 343
},
{ 118, 190, 190, 67, 34, 343, 343, 343, 343, 343, 34, 343, 343, 343, 343, 520
},
{ 118, 190, 190, 67, 34, 34, 34, 34, 34, 34, 34, 520, 520, 520, 520, 520
}
};
}else if (map == 4) {
return new int[][] {
{ 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343
},
{ 343, 67, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343
},
{ 343, 67, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343
},
{ 343, 67, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343
},
{ 343, 67, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343
},
{ 343, 67, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343
},
{ 343, 67, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343
},
{ 343, 67, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343
},
{ 343, 67, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 67, 343
},
{ 343, 67, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 67, 343
},
{ 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343
}
};
}else if (map == 2) {
return new int[][] {
{ 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343
},
{ 343, 67, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343
},
{ 343, 67, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343
},
{ 343, 67, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343
},
{ 343, 67, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343
},
{ 343, 67, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343
},
{ 343, 67, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343
},
{ 343, 67, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343
},
{ 343, 67, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 67, 343
},
{ 343, 67, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 67, 67
},
{ 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 67
}
};
}else if (map == 3) {
return new int[][] {
{ 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 124, 343
},
{ 343, 118, 343, 118, 343, 118, 118, 118, 343, 118, 343, 343, 118, 343, 124, 343
},
{ 343, 118, 343, 118, 343, 118, 343, 343, 343, 118, 343, 343, 118, 343, 124, 343
},
{ 343, 118, 118, 118, 343, 118, 118, 343, 343, 118, 343, 343, 118, 343, 343, 124
},
{ 343, 118, 343, 118, 343, 118, 343, 343, 343, 118, 343, 343, 118, 343, 343, 124
},
{ 343, 118, 343, 118, 343, 118, 118, 118, 343, 118, 118, 343, 118, 118, 343, 124
},
{ 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 124
},
{ 343, 121, 343, 121, 121, 343, 121, 121, 121, 343, 121, 121, 121, 343, 343, 124
},
{ 343, 121, 343, 121, 343, 343, 121, 343, 121, 121, 121, 343, 121, 343, 343, 124
},
{ 343, 121, 343, 343, 121, 343, 121, 343, 121, 343, 121, 343, 121, 343, 343, 124
},
{ 343, 121, 343, 121, 121, 343, 121, 121, 121, 121, 121, 121, 124, 124, 124, 124
}
};
}else if (map == 5) {
return new int[][] {
{ 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343
},
{ 343, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 343
},
{ 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343
},
{ 343, 133, 133, 133, 133, 343, 343, 343, 1, 343, 343, 343, 343, 343, 343, 343
},
{ 343, 133, 167, 167, 133, 343, 343, 1, 1, 343, 343, 343, 343, 343, 343, 343
},
{ 343, 133, 167, 167, 133, 343, 1, 343, 1, 343, 343, 343, 343, 343, 343, 343
},
{ 343, 133, 167, 167, 133, 343, 1, 343, 1, 343, 343, 343, 343, 343, 343, 343
},
{ 343, 133, 167, 167, 133, 343, 1, 1, 1, 1, 343, 343, 343, 343, 343, 343
},
{ 343, 133, 133, 140, 133, 343, 343, 343, 1, 343, 343, 343, 343, 343, 343, 343
},
{ 343, 343, 343, 343, 343, 343, 343, 343, 1, 343, 343, 343, 343, 343, 1, 1
},
{ 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 1, 1
}
};
}
return board;
}
public boolean blocked(int tx, int ty) {
return BLOCKED_TILES.contains(board[ty][tx]);
}
}