Java 2D:为什么我的 JPanel 不允许我调整它的大小?
好的,我尝试将 JPanel 添加到 JFrame 中,如下所示:
gameClasses[2] = new a2();
gameClasses[2].setSize(100, 100);
menu.add(gameClasses[2]);
menu.setVisible(true);
a2() 是一个单独的类,充当 JPanel,我使用 PaintComponent 向其绘制图像。 “菜单”是JFrame。我的问题是当我调用“gameClasses[2].setSize(100, 100);”时它不会调整 JPanel 的大小,但会保持相同的大小。有谁知道我做错了什么或者应该如何做,因为互联网上似乎没有其他人对此有任何问题。谢谢。
编辑:这是与菜单和a2相关的代码:
menu.setSize(swidth / 2 + swidth / 5, sheight / 2 + sheight / 5);
menu.setLocation((swidth - menu.getWidth()) / 2, (sheight - menu.getHeight()) / 3);
menu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
menu.setResizable(true);
menu.remove(main);
menu.add(gameClasses[0] = new a3());
menu.add(gameClasses[1] = new a4());
gameClasses[2] = new a2();
gameClasses[2].setSize(100, 100);
gameClasses[2].validate();
menu.add(gameClasses[2]);
menu.setVisible(true);
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class a2 extends JPanel {
public static int size = 48;
public static Image grsX = Toolkit.getDefaultToolkit().getImage("tiles/grsX.png");
public static Image grsY = Toolkit.getDefaultToolkit().getImage("tiles/grsY.png");
public static Image grsX1 = Toolkit.getDefaultToolkit().getImage("tiles/grsX1.png");
public static Image grsY1 = Toolkit.getDefaultToolkit().getImage("tiles/grsY1.png");
public a2() {
System.out.println("a2 loaded...");
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
//draw interface
for(int y = 0; y < a6.ay; y++) {
for(int x = 0; x < a6.ax; x++) {
g.drawImage(a5.Tile_Img.get(a5.ID_Tile.get(a6.area[x][y])), x * size, y * size, size, size, this);
if(x > 0) {
if(a6.area[x - 1][y].equals("00") && a6.area[x][y].equals("01")) {
g.drawImage(grsX, x * size, y * size, size, size, this);
}
}
if(x < a6.ax - 1) {
if(a6.area[x + 1][y].equals("00") && a6.area[x][y].equals("01")) {
g.drawImage(grsX1, x * size, y * size, size, size, this);
}
}
if(y > 0) {
if(a6.area[x][y - 1].equals("00") && a6.area[x][y].equals("01")) {
g.drawImage(grsY, x * size, y * size, size, size, this);
}
}
if(y < a6.ay - 1) {
if(a6.area[x][y + 1].equals("00") && a6.area[x][y].equals("01")) {
g.drawImage(grsY1, x * size, y * size, size, size, this);
}
}
}
}
repaint();
}
}
a3和a4是一个KeyListener类和一个MouseListener类,它们都扩展了JPanel
Ok so I am trying to add a JPanel to a JFrame as so:
gameClasses[2] = new a2();
gameClasses[2].setSize(100, 100);
menu.add(gameClasses[2]);
menu.setVisible(true);
a2() is a separate class that acts as a JPanel which I use the paintComponent to paint images to it. "menu" is the JFrame. My problem is when I call "gameClasses[2].setSize(100, 100);" it does not resize the JPanel but it stays the same size. Does anyone know what I am doing wrong or how this is supposed to be done because no one else seems to have any issues with this on the internet. Thanks.
EDIT: Here is the code related to menu and a2:
menu.setSize(swidth / 2 + swidth / 5, sheight / 2 + sheight / 5);
menu.setLocation((swidth - menu.getWidth()) / 2, (sheight - menu.getHeight()) / 3);
menu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
menu.setResizable(true);
menu.remove(main);
menu.add(gameClasses[0] = new a3());
menu.add(gameClasses[1] = new a4());
gameClasses[2] = new a2();
gameClasses[2].setSize(100, 100);
gameClasses[2].validate();
menu.add(gameClasses[2]);
menu.setVisible(true);
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class a2 extends JPanel {
public static int size = 48;
public static Image grsX = Toolkit.getDefaultToolkit().getImage("tiles/grsX.png");
public static Image grsY = Toolkit.getDefaultToolkit().getImage("tiles/grsY.png");
public static Image grsX1 = Toolkit.getDefaultToolkit().getImage("tiles/grsX1.png");
public static Image grsY1 = Toolkit.getDefaultToolkit().getImage("tiles/grsY1.png");
public a2() {
System.out.println("a2 loaded...");
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
//draw interface
for(int y = 0; y < a6.ay; y++) {
for(int x = 0; x < a6.ax; x++) {
g.drawImage(a5.Tile_Img.get(a5.ID_Tile.get(a6.area[x][y])), x * size, y * size, size, size, this);
if(x > 0) {
if(a6.area[x - 1][y].equals("00") && a6.area[x][y].equals("01")) {
g.drawImage(grsX, x * size, y * size, size, size, this);
}
}
if(x < a6.ax - 1) {
if(a6.area[x + 1][y].equals("00") && a6.area[x][y].equals("01")) {
g.drawImage(grsX1, x * size, y * size, size, size, this);
}
}
if(y > 0) {
if(a6.area[x][y - 1].equals("00") && a6.area[x][y].equals("01")) {
g.drawImage(grsY, x * size, y * size, size, size, this);
}
}
if(y < a6.ay - 1) {
if(a6.area[x][y + 1].equals("00") && a6.area[x][y].equals("01")) {
g.drawImage(grsY1, x * size, y * size, size, size, this);
}
}
}
}
repaint();
}
}
a3 and a4 are a KeyListener class and a MouseListener class that both extend JPanel
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
pack()
将使其成为显示内部组件所需的最小尺寸。添加完所有内容后调用它。setLayout(null)
(在注释中称为“不相关”代码)。 使用布局。pack()
on the frame will make it become the minimum size needed to display the components inside. Call it after everything is added.setLayout(null)
(mentioned in comment as 'non-relevant' code). Use layouts.