gpt4 book ai didi

java - 在 1 个 JFrame 中混合两个 JPanel 的问题

转载 作者:行者123 更新时间:2023-11-30 06:17:55 26 4
gpt4 key购买 nike

我在一帧中混合了两个 JPanels,它给了我这个输出!

enter image description here

这是我添加两个 JPanel 的代码:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
import java.util.*;

public class Board extends JFrame{
private int width=500;
private int height=450;

Obstacles asd= new Obstacles();

Human human;
private Dimension mindim= new Dimension(width+10,height+10);

Board(){
human = new Human();
this.setTitle("Athwart");
//setLayout(null);
human.add(asd); //!!!we see here, I add asd (which is inherited from a JPanel)
// to another existing JPanel
this.setMinimumSize(mindim); //
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(human);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //
this.setLocationRelativeTo(null); //
this.setResizable(true); //
pack(); //
setVisible(true);
human.requestFocus(); //
}
}

这就是我的 Obstacles 类的样子。

import javax.swing.*;
import java.awt.*;

public class Obstacles extends JPanel {

private int width=500;
private int height=450;
private Dimension mindim= new Dimension(width+10,height+10);
Obstacles()
{
this.setBackground(Color.white);
// this.addKeyListener(this);
// this.setFocusable(true);
// this.setRequestFocusEnabled(true);
setSize(mindim);
this.setVisible(true);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g); //
g.fillRect(0, 0, 60, 30);
g.setColor(Color.black);
g.draw3DRect(0, 0, 60, 30, true);
g.setColor(Color.black);
}
}

所以你可以看到组件的高度是 30,宽度是 60,但是上图显示的还不到它的一半!

我能做些什么来让这两个 JPanels 混合在一起吗?顺便一提,我之前尝试过使用 BoxLayout 但它没有用。是有什么问题还是我的 IDE 没有正常工作?欢呼并感谢您的精彩回复。我只是一个入门的 gui 程序员,我真的不知道如何处理事情。是的,如果你想问完整的代码,我会编辑它。 :)

最佳答案

最后,你提出一个要求:

I was just trying to put an image of a rectangle inside a jframe together with another Jpanel of an image of a circle. to see if how i would be that far in mixing two Jpanels together without overlapping.

是的,这可以做到,比如通过使用 JLayeredPane,您可以将一个 JPanel 叠加在另一个之上,但是您需要确保上面的 JPanel 不是不透明的 (setOpaque(false)) .

但话虽如此,我仍然坚持我的意见,即您看起来是在做错事。您不应该为绘制一件事 创建一个 JPanel 并尝试组合多个 JPanel,因为这会导致困惑。相反,你应该考虑创建一个绘图 JPanel,并给它逻辑对象,比如非 GUI Obstacle 对象,将它们放在一个集合中,例如 ArrayList,然后在绘图 JPanel 中,遍历绘图 JPanel 的 paintComponent 方法中的所有 Obstacles , 按照指示绘制每个障碍物。


编辑
例如:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.util.ArrayList;
import java.util.List;

import javax.swing.*;

public class ObstacleDrawer extends JPanel {
private static final int PREF_W = 800;
private static final int PREF_H = PREF_W;
private List<Obstacle> obstacleList = new ArrayList<>();

public ObstacleDrawer() {

}

public void addObstacle(Obstacle obstacle) {
obstacleList.add(obstacle);
}

@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
// smooth out the drawing
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);

// iterate through the obstacle list, drawing each obstacle
for (Obstacle obstacle : obstacleList) {
obstacle.draw(g2);
}
}

private static void createAndShowGui() {
ObstacleDrawer mainPanel = new ObstacleDrawer();

mainPanel.addObstacle(new CircleObstacle(new Point(200, 200), 100, Color.red));
mainPanel.addObstacle(new CircleObstacle(new Point(400, 300), 150, Color.blue));

JFrame frame = new JFrame("ObstacleDrawer");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

interface Obstacle {
public Point getCenter();
public void setCenter(Point center);
public int getWidth();
public void setWidth(int width);
public Color getColor();
public void setColor(Color color);
public void draw(Graphics2D g2);
}

class CircleObstacle implements Obstacle {
private Point center;
private int width;
private Color color;

public CircleObstacle(Point center, int width, Color color) {
this.center = center;
this.width = width;
this.color = color;
}

@Override
public Point getCenter() {
return center;
}

@Override
public void setCenter(Point center) {
this.center = center;
}

@Override
public int getWidth() {
return width;
}

@Override
public void setWidth(int width) {
this.width = width;
}

@Override
public Color getColor() {
return color;
}

@Override
public void setColor(Color color) {
this.color = color;
}

@Override
public void draw(Graphics2D g2) {
Color oldColor = g2.getColor();
g2.setColor(color);
int x = center.x - width / 2;
int y = center.y - width / 2;
int height = width;
g2.fillOval(x, y, width, height);
g2.setColor(oldColor);
}
}

关于java - 在 1 个 JFrame 中混合两个 JPanel 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25435598/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com