简体   繁体   English

如何使用来自不同类中的类的方法?

[英]How can I use a Method from a class in a different class?

I've been trying this for hours now with no avail.我已经尝试了几个小时,但无济于事。

I'm trying to use the paintComponent method in my Game.java class, but I'm not sure exactly how to do this.我正在尝试在我的 Game.java 类中使用paintComponent 方法,但我不确定如何做到这一点。

I've tried calling the function directly but of course it does not work as it needs to return something.我试过直接调用该函数,但当然它不起作用,因为它需要返回一些东西。

The method I need to use is in this "Circles.java" class:我需要使用的方法在这个“Circles.java”类中:


package testgame;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import javax.swing.JPanel;
import javax.swing.JPanel;

public class Circles extends JPanel {

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Bubbles(g); }

    private void Bubbles(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        RenderingHints rh
                = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        rh.put(RenderingHints.KEY_RENDERING, 
                RenderingHints.VALUE_RENDER_QUALITY);
        g2d.setRenderingHints(rh);
        int x, y, size;
        x = (int) (Math.random() * 500) + 15;
        y = (int) (Math.random() * 450) + 15;
        size = (int) (Math.random() * 30) + 10;
        g2d.setColor(Color.green);
        g2d.drawOval(x, y, size, size); } 
}

This is the class that needs the paintComponent method (Game.java):这是需要paintComponent方法(Game.java)的类:


package testgame;

import java.awt.EventQueue;
import javax.swing.JFrame;

public class Game extends JFrame {

    public static void LoadUI() {
        JFrame frame = new JFrame("Just a test!");
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
        frame.setSize(550, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true); }

public static void main(String[] args) {
     frame.add(new (Circles()));  }
}

The error I get is at the:我得到的错误是:

frame.add(new (Circles()));

The error being:错误是:

identifier expected

cannot find symbol
symbol:  method Circles()
location: class Game

cannot find symbol
symbol:  variable frame
location: class Game

First of all, It should be new Circle () instead of new (Circle ())首先,应该是new Circle ()而不是new(Circle())

Second thing as new and Circle() both are in different bracket ,java treat Circle() as method of Class Game hence you are getting error like that.第二件事newCircle()都在不同的括号中,java将Circle()视为 Class Game 的方法,因此您会收到这样的错误。

What you are trying to do here frame.add(new (Circles()));你想在这里做什么frame.add(new (Circles())); inside your main method is accessing a variable that is only available in your LoadUI() method.您的 main 方法内部正在访问一个仅在您的LoadUI()方法中可用的变量。

To be able to access that variable you would need to declare it outside your LoadUI() method, something like this:为了能够访问该变量,您需要在LoadUI()方法之外声明它,如下所示:

    ...
    public class Game extends JFrame {

    JFrame frame;

    public static void LoadUI()....

Secondly, as Manish Jaiswal mentioned above, your placement of brackets is wrong, meaning your way of initializing the Circles object is wrong.其次,正如Manish Jaiswal上面提到的,您放置的括号是错误的,这意味着您初始化Circles对象的方式是错误的。

To make this code work, you could do something like this in your main method:要使此代码工作,您可以在 main 方法中执行以下操作:

LoadGUI();
frame.add(new Circles());

Though I would recommend using a separate class / object for your GUI / JFrame and not making the LoadUI() method static.虽然我建议为您的 GUI/JFrame 使用单独的类/对象,而不是将LoadUI()方法LoadUI()静态。 You could also indent your code in an easier to read way, just to make it easier for yourself as well :)您还可以以更易于阅读的方式缩进您的代码,只是为了让您自己也更轻松:)

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

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