在 JFrame 内移动 JPanels 对象并调整其大小

发布于 2024-08-28 10:41:49 字数 1297 浏览 7 评论 0原文

在继续通过做一个简单的游戏来学习 Java 的过程中,我偶然发现了一个小问题。我的游戏板扩展了 JPanel 以及游戏板的每一块。现在,这带来了一些问题:

  1. 无法设置每个块的大小,因此,每个块 JPanel 占据整个 JFrame,隐藏其余块和背景(游戏板)。

  2. 无法设置棋子的位置。

我有默认的流程管理器。尝试过设置限制但没有运气。

也许我应该制作这块来扩展其他 JComponent? 添加的图像: 这就是那块,现在灰色区域也是那块!通过创建 mousePressed 侦听器并为其分配一些内容来检查这一点。灰色区域下方是游戏板(或者至少应该是!),另一个 JPanel。

替代文本 http://img42.imageshack.us/img42/2227/screenshotvdy.png< /a>

一些代码:

package TheProject;

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class GameWindow extends JFrame {
public GameWindow() {
    setSize(800, 600);
    setLocationRelativeTo(null);
    Map map = new Map(800, 600, 2);
    add(map);

    MilitaryUnit imperialRussia = new MilitaryUnit(30, Color.BLACK, Color.YELLOW, Color.WHITE);
    imperialRussia.setPreferredSize(new Dimension(30, 30));
    add(imperialRussia);

}
 }

当我应用 pack() 方法时会发生这种情况: 替代文本 http://img442.imageshack.us/img442/5813/screenshot2ml.png< /a>

包裹在单位周围,而不是更大并填满 JFrame 的地图。

Continuing my quest of learning Java by doing a simple game, i stumbled upon a little issue. My gameboard extends JPanel as well as each piece of the board. Now, this presents some problems:

  1. Cant set size of each piece, therefore, each piece JPanel ocupy the whole JFrame, concealing the rest of the pieces and the background (gameboard).

  2. Cant set the position of the pieces.

I have the default flow manager. Tried setbounds and no luck.

Perhaps i should make the piece to extend other JComponent?
Added image:
That's the piece, now the greyed area is also the piece! Checked that by making a mousePressed listener and assigning some stuff to it. Below the grey area, is the gameboard (or at least, should be!), another JPanel.

alt text http://img42.imageshack.us/img42/2227/screenshotvdy.png

Some code:

package TheProject;

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class GameWindow extends JFrame {
public GameWindow() {
    setSize(800, 600);
    setLocationRelativeTo(null);
    Map map = new Map(800, 600, 2);
    add(map);

    MilitaryUnit imperialRussia = new MilitaryUnit(30, Color.BLACK, Color.YELLOW, Color.WHITE);
    imperialRussia.setPreferredSize(new Dimension(30, 30));
    add(imperialRussia);

}
 }

This happens when i apply the pack() method:
alt text http://img442.imageshack.us/img442/5813/screenshot2ml.png

Packs around the Unit, not the map which is bigger and fills the JFrame.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

以可爱出名 2024-09-04 10:41:50

对于棋子随机移动的游戏,您可能会使用“空布局”。

阅读 Swing 教程中有关 绝对定位 的部分,了解更多信息信息。

For a game that has random movement of pieces you would probably use a "null layout".

Read the section from the Swing tutorial on Absolute Positioning for more information.

灯下孤影 2024-09-04 10:41:50

我使用 JPanel 编写了一些游戏。基本上,我使用 JPanel 的方式就像使用 Canvas 一样,即我通过重写 paint() 方法直接在其上绘图。我之所以使用JPanel是因为我可以确定我的游戏世界的大小,然后使用setPreferredSize()来设置面板的大小。然后,我将该面板添加到 JScrollPane 中。所以这将处理平移等问题。

假设我正在编写一个 2D 游戏。这就是我使用 JPanel 的方式。我有一个逻辑地图(二维数组)来保存我的游戏地图。假设每个位置都是 32x32 像素。所以你开始画地面以及那个位置的地面上有什么。例如,在x=1,y=2(即屏幕位置x=32,y=64)中,您先绘制地面,然后绘制地面上的内容。因此,渲染循环的大致轮廓如下所示。

for (int y = 0; y < map.length; y++)
   for (int x = 0; x < map[y].length; x++) {
      drawGround(map[y][x])
      for very element on on map[y][x] - render them
}      

您为 JPanel 设置了一个 MouseListener 侦听器,因此每次鼠标单击都会转换回地图,例如。鼠标单击 x=54, y=72 将对应 x=1, y=2。如果您有等轴测视图,则计算会有点棘手。

这里需要注意的是,每次通过滚动面板滚动面板时,都会调用 paint() 。因此,最好在 BufferedImage 上渲染游戏板,然后在 paint() 方法中绘制 BufferedImage,否则它会太慢了。

I wrote a few games using JPanel. Basically the way I use JPanel is like I'm using a Canvas, viz I draw directly on it by overriding the paint() method. The reason why I use JPanel is because I can determine the size of my game world, then use the setPreferredSize() to set the size of the panel. I then add the panel to a JScrollPane. So this will take care of the panning, etc.

Say I'm writing a 2D game. This is how I use JPanel. I have a logical map (a 2D array) which holds my game map. Say each location is 32x32 pixel. So you start drawing the ground and what is on that ground in that location. eg in x=1, y=2 which is screen location x=32, y=64, you draw the ground first, then draw what is on the ground. So a rough outline of the render loop would be something like this

for (int y = 0; y < map.length; y++)
   for (int x = 0; x < map[y].length; x++) {
      drawGround(map[y][x])
      for very element on on map[y][x] - render them
}      

You set a MouseListener listener to the JPanel, so every mouse click you translate back to the map eg. mouse click x=54, y=72 would correspond to x=1, y=2. The calculation is a bit tricky if you have an isometric view.

The thing you have to be careful here is that everytime when you scroll the panel via the scroll panel, paint() will be called. So it is good to render your game board on a BufferedImage and then in the paint() method just draw the BufferedImage otherwise it'll be too slow.

冰葑 2024-09-04 10:41:50

您尝试过 NetBeans 的可视化编辑器吗?您可以使用它在设计视图中拖放 Swing 对象并调整其大小以方便使用,然后可以切换到代码视图以查看生成的代码。你可以这样学习。

Have you tried NetBeans' visual editor? You can use it to drag, drop, and resize to your convenience Swing objects in Design View, and can then switch to Code View to see the generated code. You can learn that way.

白龙吟 2024-09-04 10:41:50

您是否尝试过setPreferredSize(Dimension d)

编辑:添加组件后,您需要在 JFrame 上调用 pack()

JFrame frame = new JFrame();
frame.setPreferredSize(new Dimension(800,600));
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(200,200));
frame.add(panel);
frame.pack();
frame.setvisible(true);

另外,您还可以向 JFrame 添加一个 Map,它的尺寸与 JFrame 相同 - 很好。但随后,您将另一个组件添加到默认的流程布局中。这无法适合您的框架,因为地图已经占据了 100% 的空间。

Have you tried setPreferredSize(Dimension d)?

Edit: You need to call pack() on your JFrame, after you've added your components:

JFrame frame = new JFrame();
frame.setPreferredSize(new Dimension(800,600));
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(200,200));
frame.add(panel);
frame.pack();
frame.setvisible(true);

Also, you add a Map to your JFrame, which has the same dimensions as your JFrame - fine. But then afterwards, you add another component, to the default flowlayout. This cannot fit into your frame, as the Map already occupies 100% of the space.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文