java中连接两个类(或项目)时出现问题

时间:2018-10-02 14:29:32

标签: java

这是我有两个项目并想将两个项目都连接起来的东西。我可以连接它们。但是,当我打开第一个项目并按链接到另一个项目的按钮时,它确实打开了另一个项目,但另一个项目只是显示一个空白屏幕而无法正常工作。我试过直接运行该项目,并且它正在运行,但是当我通过另一个项目执行相同操作时,由于某种原因,它无法正常工作。

现在,我不仅需要答案,而且还可能进行扩展和升级,以进一步推进我的项目。而且要告诉你,这是我正在制作的游戏; D。请回复。这是我的第一个带有链接按钮-的项目代码:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class gameteststart implements ActionListener {
    JFrame f1;
    JPanel p1;
    JLabel title;
    JButton b1;

    gameteststart() {
        f1 = new JFrame("Game");
        p1 = new JPanel();
        title = new JLabel("Scratchophobia");
        b1 = new JButton("Play");
        Font fnt = new Font("", Font.BOLD, 18);
        Font fnt1 = new Font("", Font.PLAIN, 11);

        p1.setLayout(null);

        f1.add(p1);
        p1.add(title);
        p1.add(b1);

        title.setBounds(230, 75, 200, 100);
        b1.setBounds(250, 245, 100, 25);

        f1.setSize(600, 500);
        f1.setVisible(true);
        p1.setBackground(Color.YELLOW);
        title.setBackground(Color.RED);
        b1.setBackground(Color.RED);
        b1.setForeground(Color.YELLOW);
        f1.setLocationRelativeTo(null);
        f1.setResizable(false);

        title.setFont(fnt);
        b1.setFont(fnt1);

        b1.addActionListener(this);
    }

    public void actionPerformed(ActionEvent ae) {
        if (ae.getSource() == b1) {
            gametest obj = new gametest();
            f1.setVisible(false);
            System.out.println("Game-Started");
        }
    }

    public static void main(String args[]) {
        gameteststart obj = new gameteststart();
    }
}

这是第二个代码:

//keylisteners

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.*;

class gametest implements KeyListener {
    JFrame f1;
    JPanel p1;
    JLabel right, down, up, left, chat;
    JTextField chatmessage;
    ImageIcon im, im2, im3, im4;
    int keys = 0, size = 600, playerWidth = 10, playerHeight = 20, speed = 6, cycletime, g, e, tempe, chatvar = 0;
    int x = (size / 2) - (playerWidth / 2);
    int y = (size / 2) - (playerHeight / 2);

    gametest() {
        f1 = new JFrame("Game");
        p1 = new JPanel();
        im = new ImageIcon("right.png");
        im2 = new ImageIcon("down.png");
        im3 = new ImageIcon("up.png");
        im4 = new ImageIcon("left.png");
        right = new JLabel(im);
        down = new JLabel(im2);
        up = new JLabel(im3);
        left = new JLabel(im4);
        chat = new JLabel("   ");
        chatmessage = new JTextField();

        p1.setLayout(null);

        f1.add(p1);
        p1.add(new JLabel(new ImageIcon("DayBackground.png")));
        p1.add(right);
        p1.add(down);
        p1.add(up);
        p1.add(left);
        p1.add(chat);
        p1.add(chatmessage);

        right.setBounds(x, y, playerWidth, playerHeight);
        down.setBounds(x, y, playerWidth, playerHeight);
        up.setBounds(x, y, playerWidth, playerHeight);
        left.setBounds(x, y, playerWidth, playerHeight);
        chat.setBounds(50, (size - 150), 100, 25);
        chatmessage.setBounds(50, (size - 100), 100, 25);

        f1.setSize(size, size);
        f1.setLocationRelativeTo(null);
        f1.setVisible(true);
        p1.setVisible(true);
        f1.setResizable(false);

        right.setVisible(false);
        down.setVisible(true);
        up.setVisible(false);
        left.setVisible(false);
        chat.setVisible(false);
        chatmessage.setVisible(false);
        f1.setFocusable(true);
        p1.setBackground(new Color(113, 204, 0));//Green and Day
        g = 1;
        cycletime = 6000;
        f1.addKeyListener(this);

        for (int j = 0;;j++) {
            for (e = 0;e <= cycletime;e++) {
                System.out.println(e);
                f1.setFocusable(true);
                try {
                    Thread.sleep(100);
                }
                catch (InterruptedException ie) {
                    System.out.println(ie);
                }
            }
            f1.addKeyListener(this);
            if (g == 1) {//Night
                p1.setBackground(new Color(1, 50, 32));//Gray
                g = 0;
                cycletime = 9000;
            }
            else if (g == 0) {//Day
                p1.setBackground(new Color(113, 204, 0));//Green
                g = 1;
                cycletime = 6000;
            }
        }
    }

    public void keyPressed(KeyEvent ke) {
        keys = keys + 1;

        if (ke.getKeyCode() == KeyEvent.VK_UP && keys == 1 || ke.getKeyCode() == KeyEvent.VK_W && keys == 1) {
            y = y - speed;
            right.setVisible(false);
            down.setVisible(false);
            up.setVisible(true);
            left.setVisible(false);
            up.setBounds(x, y, playerWidth, playerHeight);
            try {
                Thread.sleep(75);
            }
            catch (Exception e) {
                return;
            }
            if (ke.getKeyCode() == KeyEvent.VK_UP) {
                System.out.println("Up-Arrow-Key Pressed.");
            }
            else if (ke.getKeyCode() == KeyEvent.VK_W) {
                System.out.println("W-Key Pressed.");
            }
            System.out.println("X-Position: " + x);
            System.out.println("Y-Position: " + y);
            System.out.println("Speed: " + speed);
            System.out.println("Size: " + size);
            System.out.println("playerWidth: " + playerWidth);
            System.out.println("playerHeight: " + playerHeight);
        }
        else if (ke.getKeyCode() == KeyEvent.VK_DOWN && keys == 1 || ke.getKeyCode() == KeyEvent.VK_S && keys == 1) {
            y = y + speed;
            right.setVisible(false);
            down.setVisible(true);
            up.setVisible(false);
            left.setVisible(false);
            down.setBounds(x, y, playerWidth, playerHeight);
            try {
                Thread.sleep(75);
            }
            catch (Exception e) {
                System.out.println(e);
            }
            if (ke.getKeyCode() == KeyEvent.VK_DOWN) {
                System.out.println("Down-Arrow-Key Pressed.");
            }
            else if (ke.getKeyCode() == KeyEvent.VK_S) {
                System.out.println("S-Key Pressed.");
            }
            System.out.println("X-Position: " + x);
            System.out.println("Y-Position: " + y);
            System.out.println("Speed: " + speed);
            System.out.println("Size: " + size);
            System.out.println("playerWidth: " + playerWidth);
            System.out.println("playerHeight: " + playerHeight);
        }
        else if (ke.getKeyCode() == KeyEvent.VK_LEFT && keys == 1 || ke.getKeyCode() == KeyEvent.VK_A && keys == 1) {
            x = x - speed;
            right.setVisible(false);
            down.setVisible(false);
            up.setVisible(false);
            left.setVisible(true);
            left.setBounds(x, y, playerWidth, playerHeight);
            try {
                Thread.sleep(75);
            }
            catch (Exception e) {
                System.out.println(e);
            }
            if (ke.getKeyCode() == KeyEvent.VK_LEFT) {
                System.out.println("Left-Arrow-Key Pressed.");
            }
            else if (ke.getKeyCode() == KeyEvent.VK_D) {
                System.out.println("A-Key Pressed.");
            }
            System.out.println("X-Position: " + x);
            System.out.println("Y-Position: " + y);
            System.out.println("Speed: " + speed);
            System.out.println("Size: " + size);
            System.out.println("playerWidth: " + playerWidth);
            System.out.println("playerHeight: " + playerHeight);
        }
        else if (ke.getKeyCode() == KeyEvent.VK_RIGHT && keys == 1 || ke.getKeyCode() == KeyEvent.VK_D && keys == 1) {
            x = x + speed;
            right.setVisible(true);
            down.setVisible(false);
            up.setVisible(false);
            left.setVisible(false);
            right.setBounds(x, y, playerWidth, playerHeight);
            try {
                Thread.sleep(75);
            }
            catch (Exception e) {
                System.out.println(e);
            }
            if (ke.getKeyCode() == KeyEvent.VK_RIGHT) {
                System.out.println("Right-Arrow-Key Pressed.");
            }
            else if (ke.getKeyCode() == KeyEvent.VK_D) {
                System.out.println("D-Key Pressed.");
            }
            System.out.println("X-Position: " + x);
            System.out.println("Y-Position: " + y);
            System.out.println("Speed: " + speed);
            System.out.println("Size: " + size);
            System.out.println("playerWidth: " + playerWidth);
            System.out.println("playerHeight: " + playerHeight);
        }
        else if (ke.getKeyCode() == KeyEvent.VK_C && keys == 1) {
            if (chatvar == 1) {
                chatmessage.setVisible(false);
                chatvar = 0;
            }
            else {
                chatmessage.setVisible(true);
                chatvar = 1;
                try {
                    Thread.sleep(30000);
                }
                catch (InterruptedException e) {
                    System.out.println(e);
                }
                chatmessage.setVisible(false);
                chatvar = 0;
            }
        }
        else if (ke.getKeyCode() == KeyEvent.VK_ENTER && keys == 1 && chatvar == 1) {
            String str2 = chatmessage.getText();
            chat.setText(str2);
            chat.setVisible(true);
            try {
                Thread.sleep(30000);
            }
            catch (InterruptedException e) {
                System.out.println(e);
            }
            chat.setVisible(false);
        }
        else if (ke.getKeyCode() == KeyEvent.VK_N && keys == 1) {//Turn it night.
            if (g != 0) {
                e = cycletime;
                g = 1;
            }
            else {
                //nothing...
            }
        }
        else if (ke.getKeyCode() == KeyEvent.VK_M && keys == 1) {//Turn it daytime.
            if (g != 1) {
                e = cycletime;
                g = 0;
            }
            else {
                //nothing...
            }
        }
        else {
            if ((keys - 1) > -1) {
                keys = keys - 1;
            }
        }
    }

    public void keyReleased(KeyEvent arg0) {
        keys = 0;
        System.out.println("Keys:" + keys);
        try {
            Thread.sleep(100);
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }

    public void keyTyped(KeyEvent arg0) {
            //Blank...
    }

    public static void main(String args[]) {    
        gametest obj = new gametest();
    }
}

很抱歉,代码中包含and文本:D。

1 个答案:

答案 0 :(得分:0)

如果要在其他项目中使用类,并且正在使用IntelliJ,Netbeans或Eclipse之类的IDE,则需要将项目添加为库或依赖项。通常在“项目结构”或“项目设置”下。

您可以通过其他方法在同一个项目下使用不同的模块。

如果不使用IDE,则需要编译项目,并在构建中包含.class文件或理想的.jar文件。