简体   繁体   English

试图从applet切换到JFrame

[英]Trying to switch from applet to JFrame

So I'm making a game and I want to switch it from an applet to a JFrame, as all my other classes use JFrames. 所以我正在制作游戏,我想将它从applet切换到JFrame,因为我所有的其他类都使用JFrame。 However, I keep getting a bunch of run-time errors that I do not understand whatsoever. 但是,我不断得到一堆我不明白的运行时错误。

I've mostly tried just replacing where it says extends Applet to 'extends JFrame', as well as making another class where it makes a JFrame that contains everything about the game, such as the background, movement, literally the whole game. 我大部分都尝试过将其extends Applet到'扩展JFrame',并创建另一个类,它创建一个包含游戏内容的JFrame,例如背景,移动,字面上整个游戏。

This is one of the classes, the one in which I declare a JFrame that contains the rest of the game: 这是其中一个类,我在其中声明了一个包含游戏其余部分的JFrame:

public class SpaceGame{
     JFrame frame = new JFrame("Space Shooter");
     AlienAttack alienAttack = new AlienAttack();
     public SpaceGame(){
          frame.setBounds(320, 25, 1000, 650);
          frame.setResizable(false);
          frame.setVisible(true);
          frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
          frame.add(alienAttack);
     }
}

Here's the AlienAttack class that you see above: 这是你在上面看到的AlienAttack类:

public class AlienAttack extends JFrame implements KeyListener, Runnable
{
  double xVel; double yVel; final double SPEED = 0.02;
  Thread thread;
  int direction;
  double x, y;
  final double FRICTION = 0.98;
  boolean upAccel, downAccel, leftAccel, rightAccel;
  ArrayList<Shot> shots;
  ArrayList<Alien> aliens;
  boolean shipActive;

  public void init(){
    x=475;
    y=300;
    direction = 1;
    xVel = 0; yVel = 0;
    shipActive = true;
    upAccel = false; downAccel = false; leftAccel = false; rightAccel = false;
    shots = new ArrayList<Shot>();
    aliens = new ArrayList<Alien>();
    this.addKeyListener(this);
    thread = new Thread(this);
    thread.start();
  }

There's more to it, but that's the stuff that I think is relevant. 还有更多,但这是我认为相关的东西。 BTW, Shot and Alien are other classes, I use those in the ArrayLists. BTW, ShotAlien是其他类,我在ArrayLists中使用它们。

Some of the errors I get are: 我得到的一些错误是:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: adding a window to a container

(That's the main one) (那是主要的一个)

t java.awt.Container.checkNotAWindow(Unknown Source)
    at java.awt.Container.addImpl(Unknown Source)
    at java.awt.Container.add(Unknown Source)
    at javax.swing.JFrame.addImpl(Unknown Source)
    at java.awt.Container.add(Unknown Source)
    at SpaceGame.<init>(SpaceGame.java:11)
    at TestingGrounds$SnakeHandler.actionPerformed(TestingGrounds.java:82)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)

That's like half of them lol 这就像他们中的一半大声笑

Simple: 简单:

JFrame frame = new JFrame("Space Shooter");

and

frame.add(alienAttack);

You simply can't just add another JFrame into your first one. 您根本无法在第一个JFrame中添加另一个JFrame。

Thing is: any GUI framework comes with a lot of complexity. 事情是:任何GUI框架都有很多复杂性。 As a consequence: programming by trial and error isn't a reasonable strategy. 因此:通过反复试验编程并不是一种合理的策略。

Therefore the real answer is: step back. 因此,真正的答案是:退后一步。 You shouldn't do thing because you assume you can do that. 你不应该做的事,因为你认为你可以做到这一点。 You have to spend the hours it requires to understand what your are doing. 你必须花费所需的时间来了解你在做什么。

In your case: research swing. 在你的情况下:研究摇摆。 The Oracle tutorials are a good starting point. Oracle 教程是一个很好的起点。 Simply spoken: read them, top to bottom. 简单地说:从上到下阅读它们。 Read the example code, copy it, and make experiments based on that working code. 阅读示例代码,复制它,并根据该工作代码进行实验。 Then, when you are proficient enough with Swing to "walk on your on legs", then come back and look at the structure of your current applet-based application. 然后,当你熟练使用Swing“走在你的腿上”时,回过头来看看当前基于applet的应用程序的结构。 Apply the things you learned, and dissect what you have into those pieces that you can then re-use within a Swing application. 应用您学到的东西,并将您拥有的内容分解为那些可以在Swing应用程序中重复使用的部分。

Depending on context, this might be rather easy, such as: deciding what your "main" frame (window) should be, to then figure out how to add the other things. 根据上下文,这可能相当容易,例如:决定你的“主”框架(窗口)应该是什么,然后找出如何添加其他东西。 I would guess that a first attempt would be about changing your AlienAttack from being a JFrame to be a JPanel. 猜想第一次尝试将你的AlienAttack从JFrame改为JPanel。 You add panels to a frame, not frames! 您将面板添加到框架,而不是框架!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM