简体   繁体   English

Java如何调用另一个类?

[英]Java How to call another class?

I am having trouble calling a java class from another class when a button is pressed. 按下按钮时,我无法从另一个类调用Java类。

When I press Play the other frame comes up with the screen is white and I can't even exit it. 当我按“播放”时,出现另一帧,屏幕为白色,我什至无法退出。 other than shutting the whole eclipse program off. 除了关闭整个eclipse程序。 The new frame that pop's up is just a white screen nothing else from Main Class is coming up like the character. 弹出的新帧只是一个白屏,Main Class中没有其他字符出现。

This is my Menu class code which contains the button Play which the user will press. 这是我的菜单类代码,其中包含用户将按的播放按钮。

        import java.awt.BorderLayout;
    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.LayoutManager;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;







    import javax.swing.BoxLayout;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;

    public class Menu implements ActionListener {

        JButton Play, Scoreboard;

        public static void main(String[] args) throws InterruptedException 
        {

            Menu myWindow = new Menu();

        }

        public Menu() {     

            JFrame frame = new JFrame("Fruit Catcher");
            JPanel panel = new JPanel();
            panel.setLayout(new BorderLayout());

            // Filler panel to fill in the empty space to get button panel centered.

            JPanel filler = new JPanel();
            filler.setPreferredSize(new Dimension(180,180));

            ImageIcon junglebackground = new ImageIcon("junglebackground.jpg");
            JLabel backgroundimage = new JLabel(junglebackground);

            frame.add(backgroundimage);
            frame.setSize(700,470);
            frame.setResizable(false);
            frame.setVisible(true);

            JPanel buttonPanel = new JPanel();
            buttonPanel.setLayout((LayoutManager) new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));

            Play = new JButton("Play");
            Scoreboard = new JButton("Scoreboard");
            Play.setAlignmentX(Component.CENTER_ALIGNMENT);
            Scoreboard.setAlignmentX(Component.CENTER_ALIGNMENT);
            JLabel gap = new JLabel("\n");

            Play.addActionListener(this);
            Scoreboard.addActionListener(this);

            buttonPanel.add(Play);
            buttonPanel.add(gap);
            buttonPanel.add(Scoreboard);


            panel.add(buttonPanel, BorderLayout.CENTER);
            panel.add(filler, BorderLayout.NORTH);
            frame.add(panel);

        }

        public void actionPerformed(ActionEvent e) { 

            if(e.getSource().equals(Play))
            {   
                String[] args = {};
                try {
                    Main.main(args);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }

            }
            if(e.getSource().equals(Scoreboard))
            {   

                System.out.println("test");
            }

        }
    }

This is Main java class which is the Level . 这是Main Java类,即Level。 it contains character and fruit. 它包含个性和水果。 the character can move. 角色可以移动。

    import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.*;

import javax.swing.*;

public class Main extends JPanel 
{
    Character player = new Character(this);
    movement fruit = new movement(this);

    public Main() 
    {
        addKeyListener(new KeyListener() //KeyListener for the main
        {
            public void keyTyped(KeyEvent e) 
            {

            }

            public void keyReleased(KeyEvent e) 
            {
                player.keyReleased(e);
            }

            public void keyPressed(KeyEvent e) 
            {
                player.keyPressed(e);
            }
        });
        setFocusable(true); //Allows the focus to be on the main and enables movement
    }

    private void move() 
    {
        player.move();
    }

    public void paint(Graphics g) 
    {
        super.paint(g);
        Graphics2D Player = (Graphics2D) g;
        player.paint(Player);   

        Graphics2D Fruit = (Graphics2D) g;
        fruit.paintComponent(Fruit);
    }

    public static void main(String[] args) throws InterruptedException 
    {

        JFrame frame = new JFrame("Fruit Catcher");
        Main game = new Main();
        frame.add(game);
        frame.setSize(700, 450);
        frame.setVisible(true);
        while (true) 
        {
            game.move();
            game.repaint();
            Thread.sleep(5); //Control speed of player
        }
    }
}

Quick question, do you really sleep for only 5 ms? 快速提问,您真的只睡了5毫秒吗? Perhaps 40-50 ms would be better (it is anyway 25 pfs!) 也许40到50毫秒会更好(无论如何都是25 pfs!)

Thread.sleep(5); //Control speed of player

The public static void main(String[] args) is a special method in Java. 公共静态void main(String [] args)是Java中的一种特殊方法。 This is always the first method that gets called when the program start. 这始终是程序启动时首先被调用的方法。 You seem to be calling it from your click on Play. 您似乎是通过单击“播放”来调用它的。

Actually, the fact that you have 2 public static void main(String[] args) makes thing quite confusing. 实际上,您拥有2个公共静态void main(String [] args)的事实使事情变得很混乱。 Which one is going to get called when the program starts? 程序启动时将调用哪一个? This is determined by the telling java which one to call from the .jar manifest file (a text file that tells java different things about your program). 这由告诉Java从.jar清单文件(一个告诉Java有关程序的不同内容的文本文件)中调用哪个文件来确定。

I would personally revisit the design of the program to avoid having 2 main methods. 我将亲自回顾该程序的设计,以避免使用2种主要方法。

void main() is the method where your execution starts. main()是执行开始的方法。 A program can have only one main method. 一个程序只能有一个主要方法。

The button etc. to start the game should be in the main method, and the other classes' methods should be changed to a different name, for example, game. 开始游戏的按钮等应位于main方法中,其他类的方法应更改为其他名称,例如game。

You can call class methods with(syntax): object.method(), or for static classes, class.method() 您可以使用(syntax):object.method()调用类方法,对于静态类,可以使用class.method()调用类方法

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

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