Java 多对象 JFrame

标签 java multithreading swing paintcomponent keylistener

我正在创建一个 Java 游戏来娱乐,我想一次向 JFrame 添加许多内容。但由于某种原因,一个类和主方法类执行,但包含我要添加的第二个对象的第三个类不执行。我对 Java 还很陌生,所以我可能会弄错一些术语。

基本上我有 3 门课:

ma​​in.java(主方法类+JFrame构造函数类)

Infoout.java(绘制键盘控制的圆圈+一些固定矩形的类)

obj2.java(绘制第二个固定圆的类)

这是代码:

ma​​in.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");


  }
}

Infoout.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

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();
    }
 }
}
<小时/>

所以基本上,我没有遇到编译错误,但即使我在 main 方法中创建了每个类变量的实例,并将它们都添加到 JFrame 中,它们也不能同时执行。如果我从 main 方法中注释掉 obj2Infoout 将显示。如果我从主方法中注释掉 Infooutobj2 将显示。但不能同时两者兼而有之。如果我同时尝试两者,则只会显示 Infoout

正如您可能已经看到的,我认为这可能与多线程有关,因此我添加了一些您可能已经注意到的多线程代码,但我确信这是错误的,因为我大约一个小时前才了解多线程。

有人可以帮我解决这个问题吗?我已经尝试了我所知道的一切来解决这个问题,但它就是行不通:C。

我绝对喜欢示例代码,也许是一个简单的程序,你们可以快速制作出来,向我展示它是如何工作的。如果你能解释它为什么/如何工作,我会更喜欢。我喜欢学习! :)

谢谢!

  • 抗体

最佳答案

默认情况下,JFrame 使用 BorderLayout。当您在没有约束的情况下使用 add(...) 方法时,组件将被添加到 CENTER 或 BorderLayout。但是,CENTER 中只能显示一个组件(面板),因此只有最后添加的面板可见。

阅读 Swing 教程中关于 How to Use the BorderLayout 的部分了解更多信息和示例。

通常,当我看到像您这样的代码时,您可以使用如下代码将组件显示在另一个组件之上:

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

或者,如果您希望组件并排,则可以更改内容 Pane 的布局管理器以使用 FlowLayout。再次,阅读教程。有 FlowLayout 和其他布局管理器的示例。

关于Java 多对象 JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22945150/

相关文章:

java.util.Properties.load() 问题 OWASP Top 10 2017

java - docker-compose:容器之间没有连接

java - 将消息发送到 IP 地址后进行 AES 解密

c# - 在 C# 上测试并发循环缓冲区的好例子

java - 填充 JTable 时重复值

Java:具有类型化单元的元胞自动机

C#:HashSet的volatile读写

c++ - 从代码中覆盖 OMP_NUM_THREADS - 真实

java - 需要使用 JLabel 设置背景图像的帮助吗?

java - JPanel 更新高度需要多长时间?