2

我正在将图像添加到 JLabel 中并尝试设置宽度和高度......但它不起作用......帮助?

正如你在这里看到的......瓷砖不是32乘32......

替代文字

代码:

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]);
}

}
4

2 回答 2

2

通常,您可以调用label.setMinimumSize(new Dimension(width, height))以强制某个大小。不确定您要在这里做什么 - 您的 SSCCE 并不是那么

于 2010-07-26T01:45:37.970 回答
1

发布的代码存在许多问题:

a) 不要在 Swing 应用程序中使用 AWT 组件。所有使用的组件都应以“J”开头(即 JLabel、JTextArea、JTextField)

b) 您发布的代码是从旧的 AWT Applet 复制的。Swing Applet 的编码方式不同。首先阅读 Swing 教程中有关如何制作小程序的部分,以获取基本示例。例如,您永远不应该覆盖 JApplet 或 JFrame 或 JDialog 中的 update() 或 paint() 方法。

c) 不需要设置 JLabel 的大小。标签将自动成为添加到它的图标的大小。

如果仍有问题,请清理您的代码,然后发布 SSCCE ( http://sscce.org )。我不打算通过一百行代码来找出您在哪里读取图像并将其添加到标签中。

于 2010-07-26T01:40:55.797 回答