简体   繁体   中英

Problem While Connecting Two Classes(or Projects) in java

So here's the thing i have two projects and wanna connect both of them. I'm able to connect them. But when i open the first project and press the button that is linked to the other project, it does open the other project but the other project simply shows a blank screen and doesn't work. I've tried running the project directly and it is working but when i do the same through another project it doesn't work for some reason.

Now, I don't just want answers but also possible extensions and upgrades i can do to advance my project further. Moreover to tell ya' this is a game i am making ;D. Please reply. Here is my first code for the project with the linking button-:`

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

And here's the second code:

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

By the way sorry for the and here text coming inside the codes:D.

If you want classes to be used in other projects and if you are using an IDE like IntelliJ, Netbeans or Eclipse then you will need to add the project as a Library or Dependency. Normally this is under "Project Structure" or "Project Settings".

Other ways you can do it is to have different modules under the same project.

If you are not using an IDE then you'll need to compile the project and include the .class files or ideally .jar file in your build.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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