Java - 设置 JLabel 的宽度/高度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3331720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java - Set width/height of JLabel
提问by test
I am adding an image into a JLabel and trying to set the width and height... but it's not working... help?
我正在将图像添加到 JLabel 并尝试设置宽度和高度...但它不起作用...帮助?
as you can see here... the tiles are not 32 by 32...
正如你在这里看到的……瓷砖不是 32 x 32 的……
code:
代码:
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]);
}
}
采纳答案by Melody Horn
回答by camickr
There are numerous problems with the posted code:
发布的代码有很多问题:
a) Don't use AWT components in a Swing Application. All components used should start with "J" (ie. JLabel, JTextArea, JTextField)
a) 不要在 Swing 应用程序中使用 AWT 组件。使用的所有组件都应以“J”开头(即 JLabel、JTextArea、JTextField)
b) The code you have posted was copied from an old AWT Applet. Swing Applets are coded differently. Start by reading the section from the Swing tutorial on How to Make Appletsfor a basic example. For example you should NEVER override the update() or paint() methods in a JApplet or JFrame or JDialog.
b) 您发布的代码是从旧的 AWT 小程序复制而来的。Swing Applet 的编码方式不同。首先阅读 Swing 教程中关于如何制作小程序的部分,作为一个基本示例。例如,您永远不应该覆盖 JApplet、JFrame 或 JDialog 中的 update() 或paint() 方法。
c) There is no need to set the size of a JLabel. The label will automatically be the size of the Icon added to it.
c) 不需要设置 JLabel 的大小。标签将自动是添加到它的图标的大小。
Clean up your code and then post a SSCCE (http://sscce.org) if you still have problems. I'm not about to look through a hundred lines of code to figure out where you read the image and add it to a label.
如果仍有问题,请清理您的代码,然后发布 SSCCE ( http://sscce.org)。我不打算通过一百行代码找出您在哪里读取图像并将其添加到标签中。


