简体   繁体   English

Java多对象JFrame

[英]Java Multiple Objects JFrame

I'm creating a Java game for fun and I want to add many things to a JFrame at once. 我正在创建一个有趣的Java游戏,我想一次将许多东西添加到JFrame中。 But for some reason, one class and the main method class executes, but the third class containing the second object I want to add doesn't execute. 但是由于某种原因,一个类和main方法类会执行,但是包含我要添加的第二个对象的第三个类却无法执行。 I'm still new to Java so I might get some terms wrong. 我还是Java的新手,所以我可能会误解一些术语。

Basically I have 3 classes: 基本上我有3个班级:

main.java (main method class + JFrame constructor class) main.java (主方法类+ JFrame构造函数类)

Infout.java (class that draws a keyboard-controlled circle + some stationary rectangles) Infout.java (绘制键盘控制的圆和一些固定矩形的类)

obj2.java (class that draws a second stationary circle) obj2.java (绘制第二个固定圆的类)

Here is the code: 这是代码:

main.java main.java

import javax.swing.JFrame;

public class main
{
  public static void main(String[] args)
  {
JFrame frame = new JFrame();
Infout m = new Infout();
obj2 o = new obj2();

frame.add(o);
frame.add(m);
frame.setVisible(true);
frame.setDefaultCloseOperation(3);
frame.setSize(300, 400);
frame.setTitle("Circle");


  }
}

Infout.java Infout.java

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

public class Infout extends JPanel implements ActionListener, KeyListener {

    Timer t = new Timer(5, this);
    double x = 0, y = 0, velx = 0, vely = 0;

    public Infout(){

        t.start();
        addKeyListener(this);
        setFocusable(true);
        setFocusTraversalKeysEnabled(false);

        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.fill(new Ellipse2D.Double(x, y, 40, 40));
        g2.fill(new Rectangle2D.Double(0, 270, 300, 5));
        g2.fill(new Rectangle2D.Double(140, 270, 5, 300));
        g2.fill(new Rectangle2D.Double(140, 60, 70, 5));
        g2.fill(new Rectangle2D.Double(50, 140, 5, 70));
        g2.fill(new Rectangle2D.Double(150, 130, 5, 40));
        g2.fill(new Rectangle2D.Double(190, 210, 40, 5));
        if (x >= 120 && y >= 270) {
            System.out.println("sum1 has reached tha corner");
            g.drawString("You win!",115,35);
        }
        if (x <= 120 && y >= 270) {
            System.out.println("sum1 has reached tha corner");
            g.drawString("You lose!",115,35);
        }
        if (x == 120 && y >= 270){
            velx = 0;
            vely = 0;
        }
        if (x == 31.5 && y <= 200 && y >= 100){
            velx = 0;
        }
        if (x == 132 && y <= 170 && y >= 100){
            velx = 0;
        }
        if (x <= 190 && x >= 120 && y == 42){
            velx = 0;
        }
        if (x <= 210 && x >= 171 && y == 192){
            velx = 0;
        }

    }


    public void actionPerformed(ActionEvent e) {

        System.out.println("x "+x+"y "+y);

        repaint();
        x += velx;
        y += vely;

        if (x < 0 || x > 260)
        {
            velx = 0;
            vely = 0;
        }
        if (y < 0 || y > 340)
        {
            velx = 0;
            vely = 0;
        }
    }

    public void up() {
        vely = -1.5;
        velx = 0;
    }

    public void down() {
        vely = 1.5;
        velx = 0;
    }

    public void left() {
        velx = -1.5;
        vely = 0;
    }

    public void right() {
        velx = 1.5;
        vely = 0;
    }

    public void keyPressed(KeyEvent e) {
        int code = e.getKeyCode();
        if (code == KeyEvent.VK_UP) {
            up();
        }
        if (code == KeyEvent.VK_DOWN) {
            down();
        }
        if (code == KeyEvent.VK_RIGHT) {
            right();
        }
        if (code == KeyEvent.VK_LEFT) {
            left();
        }

    }

    public void keyTyped(KeyEvent e) {}
    public void keyReleased(KeyEvent e) {}
}

obj2.java obj2.java

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;

import javax.swing.JPanel;


public class obj2 extends JPanel {

 public void paintComponent(Graphics g1)
  {
      super.paintComponent(g1);
    Graphics2D g3 = (Graphics2D)g1;
    Ellipse2D circle = new Ellipse2D.Double(50.0D, 50.0D, 40.0D, 40.0D);
    g3.fill(circle);
  }

 public obj2(){

     try {
        Thread.sleep(100);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
 }
}

So basically, I get no compile errors, but even though I created an instance of each class variable in the main method, and added them both to the JFrame, they cannot both execute at once. 因此,基本上,我没有编译错误,但是即使我在main方法中创建了每个类变量的实例,并将它们都添加到JFrame中,它们却不能一次执行。 If I comment out obj2 from the main method, Infout will show up. 如果我从主方法中注释掉obj2Infout将会出现。 If I comment out Infout from the main method, obj2 will show up. 如果我从主要方法中注释掉Infout ,则将显示obj2 But not both at once. 但不是两者都一次。 If I try both at once, only Infout shows up. 如果我一次尝试两次,则只会显示Infout

As you may have saw, I thought maybe it had something to do with multithreading so I added some code for multithreading that you may have noticed but I'm sure it's wrong since I only learned about multithreading like an hour ago. 如您所见,我想也许与多线程有关,所以我添加了一些您可能已经注意到的多线程代码,但是我确定这是错误的,因为我仅在一小时前才了解多线程。

May someone pleeeease help me figure this out? 有人可以帮助我解决这个问题吗? I've tried everything I know to solve it, but it just won't work :C. 我已经尽我所能解决了所有问题,但是:C。

I would absolutely LOVE example code of maybe a simple program you guys could quickly whip up showing me how this works. 我绝对喜欢示例代码,也许是一个简单的程序,你们可以迅速了解我的工作原理。 I would even love more if you could explain why/how it works the way it does. 如果您能解释它为什么/如何工作,我什至会更喜欢。 I like learning! 我喜欢学习! :) :)

Thanks! 谢谢!

  • Ab 抗体

By default a JFrame uses a BorderLayout. 默认情况下,JFrame使用BorderLayout。 When you use the add(...) method without a constraint the component will be added tot he CENTER or the BorderLayout. 当您使用无限制的add(...)方法时,组件将被添加到CENTER或BorderLayout中。 However, only one component (panel) can be displayed in the CENTER, so only the last panel added is visible. 但是,CENTER中只能显示一个组件(面板),因此只有最后添加的面板可见。

Read the section from the Swing tutorial on How to Use the BorderLayout for more information and examples. 阅读Swing教程中有关如何使用BorderLayout的部分, 获取更多信息和示例。

Generally when I see code like you have you can display the components on top of one another with code like: 通常,当我看到类似您的代码时,可以使用类似以下代码的方式将组件彼此显示:

panel1.add( panel2 );
frame.add( panel1 );

Or if you want the coponents side by side then you can change the layout manager of the content pane to use a FlowLayout . 或者,如果您希望并排放置这些组件,则可以更改内容窗格的布局管理器以使用FlowLayout Again, read the tutorial. 同样,请阅读本教程。 There are examples for the FlowLayout and other layout managers. 有一些FlowLayout和其他布局管理器的示例。

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

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