程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

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

发布于2021-10-16 09:35     阅读(830)     评论(0)     点赞(29)     收藏(0)


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

在此处输入图片说明

这是我添加两个 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 程序员,我真的不知道如何处理事情。是的,如果你要问完整的代码,我会编辑它,如果有关系的话。:)


解决方案


最后,你提出一个要求:

我只是想将一个矩形图像与另一个圆形图像的 Jpanel 放在 jframe 中。看看我是否会在不重叠的情况下将两个 Jpanel 混合在一起。

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

但话虽如此,我仍然支持我的评论,你看起来是在做这个错误的。您不应该为绘制一个东西创建一个 JPanel并尝试组合多个 JPanel,因为这会导致一团糟。相反,您应该考虑创建一个绘图 JPanel,并为其赋予逻辑对象,例如非 GUI Obstacle 对象,将它们放在一个集合中,例如一个 ArrayList,然后在绘图 JPanel 中,遍历绘图 JPanel 的paintComponent 方法中的所有障碍,按照它的指示绘制每个障碍物。


编辑
例如:

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


所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:http://www.javaheidong.com/blog/article/303951/8d63c131ecc152274377/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

29 0
收藏该文
已收藏

评论内容:(最多支持255个字符)