简体   繁体   中英

How to restart a program if a button is pressed in java?

I'm programing a game using JFrame, JPanel, JButton and actionListener and I'd like to have a button that when pressed restarts the game. Basically I want the the current Jframe to close and to reopen a new different one. I want it to be like a button you press to start a new game. Here is my code:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Start {
public static int a;
public static JButton[][] gumbi = new JButton[15][15];
public static JFrame okno = new JFrame("Nonogram");
public static void main(String[] args) {

    okno.setVisible(true);
    okno.setSize(800, 800);
    okno.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel(new BorderLayout());
    okno.add(panel);

    JPanel polje = new JPanel(new GridLayout(15, 15));
    panel.add(polje, BorderLayout.CENTER);

    a = 0;
    int b = 1;
    // String I = I;
    for (int i = 0; i < 15; i++) {
        for (int j = 0; j < 15; j++) {
            if (i < 5 && j < 5) {
                gumbi[i][j] = new JButton();
                gumbi[i][j].setBackground(Color.GREEN);
                // gumbi[i][j].addActionListener(new Listener(gumbi));
                polje.add(gumbi[i][j]);
            } else if (i < 5 || j < 5) {
                gumbi[i][j] = new JButton();
                gumbi[i][j].setBackground(Color.YELLOW);

                // gumbi[i][j].addActionListener(new Listener(gumbi));
                polje.add(gumbi[i][j]);
                gumbi[i][j].setEnabled(false);

            } else {
                if (Math.random() <= 0.5) {
                    a += 1;
                    gumbi[i][j] = new JButton();
                    gumbi[i][j].setBackground(Color.WHITE);
                    gumbi[i][j].setForeground(Color.RED);
                    // gumbi[i][j].addActionListener(new Listener(gumbi));
                    gumbi[i][j].setText("3");
                    polje.add(gumbi[i][j]);

                } else {
                    gumbi[i][j] = new JButton();
                    gumbi[i][j].setBackground(Color.WHITE);
                    gumbi[i][j].setForeground(Color.WHITE);
                    // gumbi[i][j].addActionListener(new Listener(gumbi));
                    gumbi[i][j].setText("4");
                    polje.add(gumbi[i][j]);
                }
            }
        }
    }

    for (int i = 0; i < 15; i++) {
        for (int j = 0; j < 15; j++) {
            gumbi[i][j].addActionListener(new Listener(gumbi));
        }
    }

    int[] array = new int[105];
    for (int i = 5; i < 15; i++) {
        for (int j = 5; j < 15; j++) {
            int num = Integer.parseInt(gumbi[i][j].getText());
            array[j + ((i - 5) * 10) - 5] = num;
        }
    }

    int[] array2 = new int[105];
    for (int i = 0; i < 100; i++) {
        array2[i] = -2;
    }

    for (int i = 0; i < 100; i++) {
        if (array[i] == array[i + 1] && array[i] == 3 && (i + 1) % 10 != 0) {
            b += 1;
        } else if ((array[i] == 3 && array[i] != array[i + 1] && i < 99)
                || ((i + 1) % 10 == 0 && array[i] == 3)) {
            array2[i] = b;
            b = 1;
        }
        if ((i + 1) % 10 == 0) {
            b = 1;
        }
    }
    int x = 0;
    int y = 0;
    for (int i = 0; i <= 100; i++) {
        // if(array2[(i-4) + (10*(j - 5))] != -2){
        if (array2[i] != -2 && array[i] != 0) {
            gumbi[x + 5][y].setText("" + array2[i]);
            y++;
        }
        if ((i + 1) % 10 == 0) {
            x++;
            y = 0;
        }

    }

    for (int i = 0; i < 101; i++) {
        // System.out.println(array[i]);
        if (array2[i] != -2)
            System.out.print(array2[i] + " ");
    }
    for (int i = 5; i < 15; i++) {
        for (int j = 5; j < 15; j++) {
            int num = Integer.parseInt(gumbi[j][i].getText());
            array[j + ((i - 5) * 10) - 5] = num;
        }
    }
    for (int i = 0; i < 100; i++) {
        array2[i] = -2;
    }

    b = 1;
    for (int i = 0; i < 100; i++) {
        if (array[i] == array[i + 1] && array[i] == 3 && (i + 1) % 10 != 0) {
            b += 1;
        } else if ((array[i] == 3 && array[i] != array[i + 1] && i < 99)
                || ((i + 1) % 10 == 0 && array[i] == 3)) {
            array2[i] = b;
            b = 1;
        }
        if ((i + 1) % 10 == 0) {
            b = 1;
        }

    }
    x = 0;
    y = 0;
    for (int i = 0; i <= 100; i++) {
        if (array2[i] != -2 && array[i] != 0) {
            gumbi[y][x + 5].setText("" + array2[i]);
            y++;
        }
        if ((i + 1) % 10 == 0) {
            x++;
            y = 0;
        }

    }

    System.out.println();
    for (int i = 0; i < 105; i++) {
        // System.out.println(array[i]);
        if (array2[i] != -2)
            System.out.print(array2[i] + " ");
    }
    System.out.println();
    for (int i = 0; i < 105; i++) {
        // System.out.println(array[i]);

        System.out.print(array[i] + " ");
    }
    gumbi[1][1].setText("?");
    gumbi[1][2].setText("C");
    for (int i = 5; i < 15; i++) {
        for (int j = 5; j < 15; j++) {
            // gumbi[i][j].setText(null);
        }
    }

}
}

and the actionListener:

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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



public class Listener implements ActionListener {
JButton[][] gumbi = Start.gumbi;
JFrame cestitke = new JFrame("Cestitke!");
JLabel bravo = new JLabel("Bravo, zmagali ste!");
JFrame vprasaj = new JFrame("Navodila");
JPanel navodila = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JLabel restart = new JLabel("Za zaceti novo igro pritisnite tipko R(restart)");
JLabel pregled = new JLabel("Za pregled veljavnosti trenutne pozicije pritisnite tipko C(check)");
public Listener(JButton[][] gumbi) {
    this.gumbi = gumbi;

}

public void actionPerformed(ActionEvent e) {

    JButton gumb = (JButton) e.getSource();

    if (gumb.getBackground() == Color.WHITE) {
        gumb.setBackground(Color.BLACK);

    } else if (gumb.getBackground() == Color.BLACK) {
        gumb.setBackground(Color.WHITE);


    }
    if(e.getSource() == gumbi[1][1]){
        vprasaj.setVisible(true);
        vprasaj.setSize(450, 200);
        vprasaj.add(navodila);
        c.gridx = 0;
        c.gridy = 0;
        navodila.add(restart, c);
        c.gridx = 0;
        c.gridy = 1;
        navodila.add(pregled, c);;

    }
    if(e.getSource() == gumbi[1][2]){
        for(int i = 5; i < 15; i++){
            for(int j = 5; j < 15; j++){
                if(gumbi[i][j].getBackground() == Color.BLACK && gumbi[i][j].getForeground() ==     Color.WHITE){
                    gumbi[i][j].setBackground(Color.RED);
                }
            }
        }   
    }
    if(gumb.getBackground()  == Color.RED && gumb.getForeground() == Color.WHITE){
        gumb.setBackground(Color.WHITE);
    }

    if (gumb.getBackground() == Color.WHITE && gumb.getForeground() == Color.RED) {
        Start.a += 1;
        gumbi[0][0].setText("" + Start.a);
    } else if (gumb.getBackground() == Color.BLACK && gumb.getForeground() == Color.RED) {
        Start.a -= 1;
        gumbi[0][0].setText("" + Start.a);
        if(Start.a == 0){
            for(int i = 5; i < 15; i++){
                for (int j = 5; j < 15; j++){
                    gumbi[i][j].setEnabled(false);
                }
            }
            cestitke.add(bravo);
            cestitke.setVisible(true);
            cestitke.setSize(100, 100);
        }
    }
    if (gumb.getBackground() == Color.BLACK && gumb.getForeground() == Color.WHITE) {
        Start.a += 100;
        gumbi[0][0].setText("" + Start.a);
    } else if (gumb.getBackground() == Color.WHITE && gumb.getForeground() == Color.WHITE) {
        Start.a -= 100;
        gumbi[0][0].setText("" + Start.a);
        if(Start.a == 0){
            for(int i = 5; i < 15; i++){
                for (int j = 5; j < 15; j++){
                    gumbi[i][j].setEnabled(false);
                }
            }
            cestitke.add(bravo);
            cestitke.setVisible(true);
            cestitke.setSize(100, 100);
        }
    }
}

}

thanks for your time.

Something like this?

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

import java.awt.event.WindowEvent;

public class Start
{
    public static int a;
    public static JButton[][] gumbi = new JButton[15][15];
    public static JFrame okno = null;

    public static class Listener 
        implements ActionListener
    {
        JButton[][] gumbi = Start.gumbi;
        JFrame cestitke = new JFrame("Cestitke!");
        JLabel bravo = new JLabel("Bravo, zmagali ste!");
        JFrame vprasaj = new JFrame("Navodila");
        JPanel navodila = new JPanel(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        JLabel restart = new JLabel("Za zaceti novo igro pritisnite tipko R(restart)");
        JLabel pregled = new JLabel("Za pregled veljavnosti trenutne pozicije pritisnite tipko C(check)");

        public Listener(JButton[][] gumbi)
        {
            this.gumbi = gumbi;
        }

        public void actionPerformed(ActionEvent e)
        {
            JButton gumb = (JButton) e.getSource();

            if (gumb.getBackground() == Color.WHITE)
            {
                gumb.setBackground(Color.BLACK);

            }
            else if (gumb.getBackground() == Color.BLACK)
            {
                gumb.setBackground(Color.WHITE);

            }
            if (e.getSource() == gumbi[1][1])
            {
                vprasaj.setVisible(true);
                vprasaj.setSize(450, 200);
                vprasaj.add(navodila);
                c.gridx = 0;
                c.gridy = 0;
                navodila.add(restart, c);
                c.gridx = 0;
                c.gridy = 1;
                navodila.add(pregled, c);;

            }
            if (e.getSource() == gumbi[1][2])
            {
                for (int i = 5; i < 15; i++)
                {
                    for (int j = 5; j < 15; j++)
                    {
                        if (gumbi[i][j].getBackground() == Color.BLACK && gumbi[i][j].getForeground() == Color.WHITE)
                        {
                            gumbi[i][j].setBackground(Color.RED);
                        }
                    }
                }
            }
            if (e.getSource() == gumbi[1][3])
            { 
                restartFrame();
            }

            if (gumb.getBackground() == Color.RED && gumb.getForeground() == Color.WHITE)
            {
                gumb.setBackground(Color.WHITE);
            }

            if (gumb.getBackground() == Color.WHITE && gumb.getForeground() == Color.RED)
            {
                Start.a += 1;
                gumbi[0][0].setText("" + Start.a);
            }
            else if (gumb.getBackground() == Color.BLACK && gumb.getForeground() == Color.RED)
            {
                Start.a -= 1;
                gumbi[0][0].setText("" + Start.a);
                if (Start.a == 0)
                {
                    for (int i = 5; i < 15; i++)
                    {
                        for (int j = 5; j < 15; j++)
                        {
                            gumbi[i][j].setEnabled(false);
                        }
                    }
                    cestitke.add(bravo);
                    cestitke.setVisible(true);
                    cestitke.setSize(100, 100);
                }
            }
            if (gumb.getBackground() == Color.BLACK && gumb.getForeground() == Color.WHITE)
            {
                Start.a += 100;
                gumbi[0][0].setText("" + Start.a);
            }
            else if (gumb.getBackground() == Color.WHITE && gumb.getForeground() == Color.WHITE)
            {
                Start.a -= 100;
                gumbi[0][0].setText("" + Start.a);
                if (Start.a == 0)
                {
                    for (int i = 5; i < 15; i++)
                    {
                        for (int j = 5; j < 15; j++)
                        {
                            gumbi[i][j].setEnabled(false);
                        }
                    }
                    cestitke.add(bravo);
                    cestitke.setVisible(true);
                    cestitke.setSize(100, 100);
                }
            }
        }
    }

    public static void main(String[] args)
    {
        restartFrame();
    }

    public static void restartFrame()
    {
        if (null != okno)
        {
            okno.dispatchEvent(new WindowEvent(okno, WindowEvent.WINDOW_CLOSING));
            okno = null;
        }
        okno = new JFrame("Nonogram");
        okno.setVisible(true);
        okno.setSize(800, 800);
        okno.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        JPanel panel = new JPanel(new BorderLayout());
        okno.add(panel);

        JPanel polje = new JPanel(new GridLayout(15, 15));
        panel.add(polje, BorderLayout.CENTER);

        a = 0;
        int b = 1;
        // String I = I;
        for (int i = 0; i < 15; i++)
        {
            for (int j = 0; j < 15; j++)
            {
                if (i < 5 && j < 5)
                {
                    gumbi[i][j] = new JButton();
                    gumbi[i][j].setBackground(Color.GREEN);
                    // gumbi[i][j].addActionListener(new Listener(gumbi));
                    polje.add(gumbi[i][j]);
                }
                else if (i < 5 || j < 5)
                {
                    gumbi[i][j] = new JButton();
                    gumbi[i][j].setBackground(Color.YELLOW);

                    // gumbi[i][j].addActionListener(new Listener(gumbi));
                    polje.add(gumbi[i][j]);
                    gumbi[i][j].setEnabled(false);

                }
                else
                {
                    if (Math.random() <= 0.5)
                    {
                        a += 1;
                        gumbi[i][j] = new JButton();
                        gumbi[i][j].setBackground(Color.WHITE);
                        gumbi[i][j].setForeground(Color.RED);
                        // gumbi[i][j].addActionListener(new Listener(gumbi));
                        gumbi[i][j].setText("3");
                        polje.add(gumbi[i][j]);

                    }
                    else
                    {
                        gumbi[i][j] = new JButton();
                        gumbi[i][j].setBackground(Color.WHITE);
                        gumbi[i][j].setForeground(Color.WHITE);
                        // gumbi[i][j].addActionListener(new Listener(gumbi));
                        gumbi[i][j].setText("4");
                        polje.add(gumbi[i][j]);
                    }
                }
            }
        }

        for (int i = 0; i < 15; i++)
        {
            for (int j = 0; j < 15; j++)
            {
                gumbi[i][j].addActionListener(new Listener(gumbi));
            }
        }

        int[] array = new int[105];
        for (int i = 5; i < 15; i++)
        {
            for (int j = 5; j < 15; j++)
            {
                int num = Integer.parseInt(gumbi[i][j].getText());
                array[j + ((i - 5) * 10) - 5] = num;
            }
        }

        int[] array2 = new int[105];
        for (int i = 0; i < 100; i++)
        {
            array2[i] = -2;
        }

        for (int i = 0; i < 100; i++)
        {
            if (array[i] == array[i + 1] && array[i] == 3 && (i + 1) % 10 != 0)
            {
                b += 1;
            }
            else if ((array[i] == 3 && array[i] != array[i + 1] && i < 99)
                    || ((i + 1) % 10 == 0 && array[i] == 3))
            {
                array2[i] = b;
                b = 1;
            }
            if ((i + 1) % 10 == 0)
            {
                b = 1;
            }
        }
        int x = 0;
        int y = 0;
        for (int i = 0; i <= 100; i++)
        {
            if (array2[i] != -2 && array[i] != 0)
            {
                gumbi[x + 5][y].setText("" + array2[i]);
                y++;
            }
            if ((i + 1) % 10 == 0)
            {
                x++;
                y = 0;
            }

        }

        for (int i = 0; i < 101; i++)
        {
            // System.out.println(array[i]);
            if (array2[i] != -2)
            {
                System.out.print(array2[i] + " ");
            }
        }
        for (int i = 5; i < 15; i++)
        {
            for (int j = 5; j < 15; j++)
            {
                int num = Integer.parseInt(gumbi[j][i].getText());
                array[j + ((i - 5) * 10) - 5] = num;
            }
        }
        for (int i = 0; i < 100; i++)
        {
            array2[i] = -2;
        }

        b = 1;
        for (int i = 0; i < 100; i++)
        {
            if (array[i] == array[i + 1] && array[i] == 3 && (i + 1) % 10 != 0)
            {
                b += 1;
            }
            else if ((array[i] == 3 && array[i] != array[i + 1] && i < 99)
                    || ((i + 1) % 10 == 0 && array[i] == 3))
            {
                array2[i] = b;
                b = 1;
            }
            if ((i + 1) % 10 == 0)
            {
                b = 1;
            }

        }
        x = 0;
        y = 0;
        for (int i = 0; i <= 100; i++)
        {
            if (array2[i] != -2 && array[i] != 0)
            {
                gumbi[y][x + 5].setText("" + array2[i]);
                y++;
            }
            if ((i + 1) % 10 == 0)
            {
                x++;
                y = 0;
            }

        }

        System.out.println();
        for (int i = 0; i < 105; i++)
        {
            // System.out.println(array[i]);
            if (array2[i] != -2)
            {
                System.out.print(array2[i] + " ");
            }
        }
        System.out.println();
        for (int i = 0; i < 105; i++)
        {
            // System.out.println(array[i]);

            System.out.print(array[i] + " ");
        }
        gumbi[1][1].setText("?");
        gumbi[1][2].setText("C");
        gumbi[1][3].setText("R");
    }
}

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