如何将多个组件添加到JFrame [英] How to add multiple components to a JFrame

查看:91
本文介绍了如何将多个组件添加到JFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用多个JPanel将多个类添加到我的JFrame框架"中,这些JPanels显示一个敌人(红色圆圈),一个玩家(橙色菱形)和一个底座(黑色空心矩形),但是它们显示不正确.我有四个类,"Assignment6"(任务名称和我的主要人员),"Enemy"(创建敌人的类),"Player"(创建和移动玩家的类),以及"Base"(基础建立敌人无法进入的基地的职业).我还没有在基础"中为敌方的接触碰撞建立一种方法.

I'm trying to add multiple classes to my JFrame "frame" using multiple JPanels showing an enemy (red circle), a player (orange diamond), and a base (an outlined black rectangle) but they are not displaying correctly. I have four classes, "Assignment6" (the name of the assignment and my main), "Enemy" (the class for creating enemies), "Player" (the class for creating and moving the player), and "Base" (the class that creates the base that enemies cannot enter). I have not made a method in "Base" for the enemy's contact collision yet.

分配6:

import javax.swing.*;
import java.awt.Color;
import java.awt.FlowLayout;
public class Assignment6 extends JFrame {
    public static void main(String[] args){
        JPanel panel1 = new JPanel();
        JPanel panel2 = new JPanel();
        JPanel panel3 = new JPanel();
        panel1.add(new Base());
        panel2.add(new Enemy());
        panel3.add(new Player());
        JFrame frame = new JFrame("Assignment 6");
        frame.setLayout(new FlowLayout());
        frame.setBackground(Color.WHITE);
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.setSize (1000, 1000);
        frame.getContentPane().add(panel1);
        frame.getContentPane().add(panel2);
        frame.getContentPane().add(panel3);
        frame.setVisible (true);
   }
}

敌人:

import java.awt.Graphics;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.*;
public class Enemy extends JComponent {
    public Enemy(){
        setPreferredSize(new Dimension(100, 100));
    }
    public int createEnemyX(){
        int enemyX = (int)(100 * Math.random());
        return enemyX;
    }
    public int createEnemyY(){
        int enemyY = (int)(100 * Math.random());
        return enemyY;
    }
    @Override
    public void paintComponent(Graphics g){
       super.paintComponent(g);
       g.setColor(Color.RED);
       g.fillOval(createEnemyX(), createEnemyY(), 100, 100);
    }
}   

玩家:

import java.awt.Graphics;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.*;
import javax.swing.*;
public class Player extends JComponent implements KeyListener{
    public int height = getHeight();
    public int width = getWidth();
    public int x = 500;
    public int y = 500;
    public Player(){
        setPreferredSize(new Dimension(100, 100));
        addKeyListener(this);
    }
    @Override
    public void paintComponent(Graphics g){
       super.paintComponent(g);
       Color orange = new Color(210, 105,30);
       g.setColor(orange);
       int[]x1 = {x-50, x, x+50, x};
       int[]y1 = {y, y-50, y, y+50};
       g.fillPolygon(x1, y1, 4);
    }
    @Override
    public void keyPressed(KeyEvent e){
        if (e.getKeyCode() == KeyEvent.VK_LEFT)
            x -= 10;
        else if (e.getKeyCode() == KeyEvent.VK_RIGHT)
            x += 10;
        else if (e.getKeyCode() == KeyEvent.VK_UP)
            y -= 10;
        else if (e.getKeyCode() == KeyEvent.VK_DOWN)
            y += 10;
        repaint();
    }
    @Override
    public void keyReleased(KeyEvent e){
        ;
    }
    @Override
    public void keyTyped(KeyEvent e){
        ;
    }
}

基础:

import java.awt.Graphics;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.*;
public class Base extends JComponent {
    public Base(){
        setPreferredSize(new Dimension(200, 200));
    }
    @Override
    public void paintComponent(Graphics g){
       super.paintComponent(g);
       g.setColor(Color.BLACK);
       g.drawRect(400,400,200,200);

    }
}

运行项目后,敌人(红色圆圈)出现,但被白色矩形切断.我假设调用了paintComponent方法,但未呈现它们.

Once I run the project the enemy(red circle) appears but is cut off by white rectangles. I am assuming that the paintComponent methods are be called, but they are not being rendered.

我已使用以下问答作为我代码的主要基础: 如何将多个类添加到单个JFrame

I've used this question and answer as the main base of my code: How to add multiple classes to a single JFrame

推荐答案

您的总体程序设计似乎有误,需要重新执行,尤其是我在谈论拥有可绘制的实体类,即Base,Enemy和Player都扩展了JComponent,因为这使这些组件只能在其JComponent和JComponent中绘制,而不能在其他地方绘制.相反:

Your overall program design looks to be in error and needs to be re-done, and specifically I'm talking about having your drawable entity classes, Base, Enemy, and Player, each extend JComponent, since that allows these components to be drawn within their JComponent and their JComponent only and nowhere else. Instead:

  • 使您的可绘制实体类(例如Player,Base和Enemy)实现一个通用接口(例如Drawable),该接口具有绘画方法(例如draw(Graphics g)).
  • 不要让它们扩展JComponent,JPanel或任何其他Swing组件.
  • 相反,只为图形创建一个类,称其为DrawingPanel,并使其扩展JPanel或JComponent.
  • 在此DrawingPanel的单个paintComponent方法中,绘制每个实体(如果它位于需要绘制的游戏部分中).
  • 力图将视图代码,GUI代码与程序模型代码,控制逻辑实体行为和位置的代码分开,最好将它们放在单独的类中,甚至可能在单独的程序包中.
  • Make your drawable entity classes, such as Player, Base, and Enemy implement a common interface, say Drawable, that has a painting method, say draw(Graphics g).
  • Do not have them extend JComponent, JPanel or any other Swing component.
  • Instead create one single class for graphics, say call it DrawingPanel, and have it extend JPanel or JComponent.
  • Within the single paintComponent method of this DrawingPanel, draw each entity, if it is located within the section of the game that requires drawing.
  • Strive to separate your view code, the GUI code, from your program model code, the code that governs your logical entity behaviors and locations, preferably having them in separate classes, and probably even separate packages.

关于问题:

我目前正在尝试制作Drawable接口,应该在方法中添加什么?

I am currently trying to make Drawable interface, what should I put in the method?

该接口在方法内将没有任何代码.任何实现该接口的类都需要具有允许该类的对象绘制自身的代码.

The interface would not have any code within the method. Any classes that implement the interface would need to have code that allows the objects of that class to draw themselves.

如果我有DrawingPanel类,我什至需要Drawable接口吗?

If I have the DrawingPanel class, would I even need the Drawable interface?

是的,因为如上所述,DrawingPanel将保存您的Drawable对象的集合,并将在其paintComponent方法内绘制它们.

Yes because DrawingPanel would hold a collection of your Drawable objects and would draw them within its paintComponent method, as already mentioned above.

这篇关于如何将多个组件添加到JFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆