如何在JFrame中将来自两个不同类的两个圆一起移动 [英] How to move two circles together in a JFrame from two different classes

查看:57
本文介绍了如何在JFrame中将来自两个不同类的两个圆一起移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将两个圆圈一起显示在同一帧上 通过两个不同的类.但是,即使在child1类中"x"的值不断变化,一次也仅显示一个移动,而paintComponent()仅取值"x1"并显示了从child2类中移出的圆.

I am trying to display the two circles moving together on a single frame through two different classes. But only one is shown moving at a time,even though value of "x" is changing continuously in class child1, paintComponent() is only taking the value of "x1" and showing the circle in moving from class child2.

如果将两个单独的帧分配给两个类,则它们可以很好地工作. 这是我的代码

If two separate frames are assigned to both the classes they works perfectly fine. Here is my code

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Test13
{
    public static void main(String[] args)
    {
        child1 c1 = new child1();
        child2 c2 = new child2();
        JFrame f1 = new JFrame("Frame Test1");
        f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//passing a single JFrame to both methods as parameters    
        c1.cfunc1(f1);   
        c2.cfunc2(f1);   // but this line always hides the upper one
        f1.setSize(1000,700);
        f1.setVisible(true);
    }

}

class child1 extends JPanel implements ActionListener
{
    int x,y;
    int delay1;
    Timer timer1; //timer for 1st class constructor

    child1()
    {
        x=1;
        y=100;
        timer1 = new Timer(50,this);
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        if(x <= 500)
        {
            x += 1;
            y = 100;
            repaint();
        }
    }

    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.fillOval(x, y, 10, 10);
    }

    void cfunc1(JFrame f1)//passing JFrame as parameter 
    {
        child1 c1 = new child1();
        f1.add(c1);
        c1.timer1.start(); //timer started at the end of class1
    }
}  

class child2 extends JPanel implements ActionListener
{
    int x1,y1;
    int delay2;
    Timer timer2;

    child2()
    {
        x1 = 500;
        y1 = 100;
        timer2 = new Timer(50,this);//timer for 2nd class constructor
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        if(x1 <= 500)
        {
            x1 -= 1;
            y1 = 100;
            repaint();
        }
    }

    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.fillOval(x1, y1, 10, 10);
    }

    void cfunc2(JFrame f1)//passing JFrame as parameter
    {
        child2 c2 = new child2();
        f1.add(c2);
        c2.timer2.start();//timer started for 2nd class
    }
} 

推荐答案

将两个组件添加到BorderLayout(JFrame的默认布局)的单个约束中时,仅显示一个.

When two components are added to a single constraint of a BorderLayout (the default layout for a JFrame), only one is displayed.

执行此操作的典型方法不是在JComponent中设计自定义绘画(如JPanel),而是将它们作为简单的类,可以在需要时将其作为paint(Graphics)draw(Graphics).

The typical way to do this is not to design the custom painting in a JComponent (like JPanel) but instead to have them as simple classes which can paint(Graphics) or draw(Graphics) when requested to do so.

然后扩展一个JComponent,该JComponent迭代可绘制组件的列表并绘制每个组件.

Then extend a single JComponent that iterates a list of the drawable components and paints each one.

这篇关于如何在JFrame中将来自两个不同类的两个圆一起移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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