如何在JFrame内设置JPanel的大小?

huangapple go评论70阅读模式
英文:

How do I set the size of a JPanel inside a JFrame?

问题

我想为我的地图和后来的小地图创建一个自定义大小的JPanel,地图生成器和精灵图读取器运行正常,但是当我将JPanel设置为与JFrame不同的大小并且使用setSize()或setBounds()运行程序时,它的尺寸仍然与JFrame相同。

主类:

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Main extends JPanel {

    private static SpriteSheet sc = new SpriteSheet();

    private static JLabel[][] mapTiles = new JLabel[40][40];
    private static int[][] mapTileCode = new int[40][40];

    public Main() {

        JFrame f = new JFrame();
        setLayout(null);
        this.setSize(625, 625);

        f.add(this);
        f.setSize(1000, 1000);
        f.setUndecorated(true);
        f.setLocationRelativeTo(null);
        this.setBackground(new Color(150, 150, 150));
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }

    public static void main(String args[]) {

        new Main().mapSetup();

    }

    public void mapSetup() {

        Random r = new Random();

        for (int x = 0; x < 40; x++) {
            for (int y = 0; y < 40; y++) {

                int l = r.nextInt(6);
                BufferedImage i = sc.getTile(l);

                ImageIcon im = new ImageIcon(i);

                mapTileCode[y][x]=l;
                mapTiles[y][x] = new JLabel(im);
                mapTiles[y][x].setBounds(y * 25, x * 25, 25, 25);
                this.add(mapTiles[y][x]);
                repaint();

            }
        }
    }

}

SpriteSheet类:

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class SpriteSheet {

    final int W = 25;
    final int H = 25;

    BufferedImage spriteSheet;

    public BufferedImage getTile(int tile) {

        if (new File("img/SpriteSheet.png").exists()) {
            try {
                spriteSheet = ImageIO.read(new File("img/SpriteSheet.png"));
            } catch (IOException e) {
                e.printStackTrace();
            }

            int tileX = 1 + tile + (tile * 25);
            int tileY = 1 + (0 * 25);

            return spriteSheet.getSubimage(tileX, tileY, 25, 25);

        } else {

            System.err.println("no files found");

            return null;

        }

    }

}
英文:

I want to create a Custom size JPanel for my map and later mini-map, the map generator and spritesheet reader work fine however when I set the JPanel to a different size then the JFrame and I use the setSize() or setBounds() and run the program, it stays the same dimensions as the JFrame.

Main class:

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Main extends JPanel {
private static SpriteSheet sc = new SpriteSheet();
private static JLabel[][] mapTiles = new JLabel[40][40];
private static int[][] mapTileCode = new int[40][40];
public Main() {
JFrame f = new JFrame();
setLayout(null);
this.setSize(625, 625);
f.add(this);
f.setSize(1000, 1000);
f.setUndecorated(true);
f.setLocationRelativeTo(null);
this.setBackground(new Color(150, 150, 150));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
public static void main(String args[]) {
new Main().mapSetup();
}
public void mapSetup() {
Random r = new Random();
for (int x = 0; x &lt; 40; x++) {
for (int y = 0; y &lt; 40; y++) {
int l = r.nextInt(6);
BufferedImage i = sc.getTile(l);
ImageIcon im = new ImageIcon(i);
mapTileCode[y][x]=l;
mapTiles[y][x] = new JLabel(im);
mapTiles[y][x].setBounds(y * 25, x * 25, 25, 25);
this.add(mapTiles[y][x]);
repaint();
}
}
}
}

SpriteSheet class:

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class SpriteSheet {
final int W = 25;
final int H = 25;
BufferedImage spriteSheet;
public BufferedImage getTile(int tile) {
if (new File(&quot;img/SpriteSheet.png&quot;).exists()) {
try {
spriteSheet = ImageIO.read(new File(&quot;img/SpriteSheet.png&quot;));
} catch (IOException e) {
e.printStackTrace();
}
int tileX = 1 + tile + (tile * 25);
int tileY = 1 + (0 * 25);
return spriteSheet.getSubimage(tileX, tileY, 25, 25);
} else {
System.err.println(&quot;no files found&quot;);
return null;
}
}
}

答案1

得分: 0

你所看到的结果是LayoutManager的结果。您至少有两个选项:

  1. 将主类的父类的LayoutManager设置为null
  2. 将您的主类放入JScrollPane中。JScrollPane不会调整JPanel的大小,而是使用其preferredSize(您可以设置)。

注意:代码部分未翻译。

英文:

The results you are seeing are a result of the LayoutManager. You have (at least) two options:

  1. Set the LayoutManager of the parent of your Main class to null
  2. Put your Main class inside a JScrollPane. JScrollPane won't resize your JPanel, but rather use its preferredSize (which you can set) instead.

huangapple
  • 本文由 发表于 2020年10月1日 07:26:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/64147087.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定