Как открыть JFrame одним нажатием кнопки - PullRequest
0 голосов
/ 11 апреля 2020

Я создаю игру, но у меня есть главное и слишком внешнее меню, и я хочу открыть одно из них, нажав соответствующую кнопку в JFrame.

код main. java

package main;

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

import game.client.mainClient;
import game.server.mainServer;

public class main extends JPanel {
    /**
     * 
     */
    private static final long serialVersionUID = 6590770928148744094L;

    private JLabel jcomp1;
    private JButton jcomp5;
    private JButton jcomp6;
    private JLabel jcomp8;
    private JLabel jcomp9;
    private JLabel jcomp10;



    public main() {
        //construct components
        jcomp1 = new JLabel ("test game");
        jcomp5 = new JButton ("singel player");
        jcomp6 = new JButton ("multiplayer");
        jcomp8 = new JLabel ("this game is made by kebe_");
        jcomp9 = new JLabel ("gui is made in guigenie");
        jcomp10 = new JLabel ("game verision dev 1.0");

        //adjust size and set layout
        setPreferredSize (new Dimension (681, 466));
        setLayout (null);

        //add components
        add (jcomp1);
        add (jcomp5);
        add (jcomp6);
        add (jcomp8);
        add (jcomp9);
        add (jcomp10);

        //set component bounds (only needed by Absolute Positioning)
        jcomp1.setBounds (295, 5, 70, 25);
        jcomp5.setBounds (265, 30, 115, 30);
        jcomp6.setBounds (265, 65, 115, 30);
        jcomp8.setBounds (0, 430, 180, 25);
        jcomp9.setBounds (0, 415, 155, 20);
        jcomp10.setBounds (0, 445, 140, 25);

        // close and open
        // singleplayer
        jcomp5.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                System.exit(0);
                mainClient mc = new mainClient();
                mc.setVisible(true);
            }
        });
        // multiplayer
        jcomp6.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                System.exit(0);
                mainServer ms = new mainServer();
                ms.setVisible(true);
            }
        });
    }
    public static void main (String[] args) {
        JFrame frame = new JFrame ("Test game");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add (new main());
        frame.pack();
        frame.setVisible (true);
    }
}

код mainClient. java

package game.client;

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

public class mainClient extends JPanel
{
    /**
     * 
    */
    private static final long serialVersionUID = -1271816540338950462L;

    private JLabel jcomp1;
    private JComboBox jcomp2;
    private JButton jcomp3;

    public mainClient() {
        //construct preComponents
        String[] jcomp2Items = {"save 1", "save 2", "save 3", "save 4", "save 5"};

        //construct components
        jcomp1 = new JLabel ("singel player");
        jcomp2 = new JComboBox (jcomp2Items);
        jcomp3 = new JButton ("play selected save");

        //adjust size and set layout
        setPreferredSize (new Dimension (681, 466));
        setLayout (null);

        //add components
        add (jcomp1);
        add (jcomp2);
        add (jcomp3);

        //set component bounds (only needed by Absolute Positioning)
        jcomp1.setBounds (290, 5, 85, 25);
        jcomp2.setBounds (280, 30, 100, 25);
        jcomp3.setBounds (255, 70, 150, 25);

        jcomp3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                System.exit(0);
            }
        });
    }


    public static void main (String[] args) {
        JFrame frame = new JFrame ("Singel player");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add (new mainClient());
        frame.pack();
        frame.setVisible (true);
    }
}

код mainServer

package game.server;


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

public class mainServer extends JPanel
{
    /**
     * 
     */
    private static final long serialVersionUID = 2726545572728204122L;

    private JLabel jcomp1;
    private JButton jcomp2;
    private JButton jcomp3;

    public mainServer() {
        //construct components
        jcomp1 = new JLabel ("multiplayer");
        jcomp2 = new JButton ("host game");
        jcomp3 = new JButton ("join game");

        //adjust size and set layout
        setPreferredSize (new Dimension (681, 466));
        setLayout (null);

        //add components
        add (jcomp1);
        add (jcomp2);
        add (jcomp3);

        //set component bounds (only needed by Absolute Positioning)
        jcomp1.setBounds (300, 0, 75, 25);
        jcomp2.setBounds (285, 25, 100, 25);
        jcomp3.setBounds (285, 50, 100, 25);
    }


    public static void main (String[] args) {
        JFrame frame = new JFrame ("Multiplayer");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add (new mainServer());
        frame.pack();
        frame.setVisible (true);
    }
}

как я могу открыть JFrame в mainClient. java когда я нажмите кнопку одиночной игры и JFrame в mainServer. java когда я нажимаю кнопку многопользовательской игры?

1 Ответ

0 голосов
/ 11 апреля 2020

Создание ActionListeners для ваших кнопок. Определите ваши фреймы, и вы можете получить к ним доступ с вашим именем класса. Например,

class mainClient extends JPanel {
    JFrame mainClientFrame = new JFrame();
}

, а затем;

mainClient mc = new mainClient();
mc.mainClientFrame.setVisible(true);

, если вы хотите использовать эти кадры в своем основном пустоте, определите их stati c.

class mainClient extends JPanel {
    static JFrame mainClientFrame = new JFrame();
}
...