当前位置: 首页 > 知识库问答 >
问题:

在java中,如果按下按钮,如何重新启动程序?

施慈
2023-03-14

我正在使用JFrame、JPanel、JButton和actionListener编写一个游戏,我希望有一个按钮,按下后重新启动游戏。基本上,我希望当前Jframe关闭并重新打开一个新的Jframe。我希望它像一个按钮,你按下开始一个新的游戏。这是我的密码:

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

}
}

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

}

谢谢你抽出时间。

共有1个答案

商茂勋
2023-03-14

像这样的?

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");
    }
}
 类似资料:
  • 问题内容: 关于此事,我几乎遍历了每篇文章,但大多数都没有解释如何正确执行。问题: 我创建了一个javaFX应用程序,一个骰子游戏,人类玩家与计算机,但是在玩游戏的任何时候,人类玩家都应该能够单击“新游戏”按钮,并且应该从头开始重新启动游戏。 我尝试再次启动该阶段,但是在javafx中,我们不能两次调用launch方法。 1)有没有一种方法可以实现此功能而无需重新启动整个应用程序? 2)如果没有,

  • 我正在制作一个应用程序,其中有一个“MainActivity.class”,它有一个按钮“Edit profile”,可以引导另一个活动,称为“Editprofile.class”。它(MainActivity)还有一个文本视图,显示一个字符串(名称)。 Editprofile.class接受来自用户的字符串,按下添加按钮后,新的字符串会更新到数据库中,替换以前的字符串。现在,当更新后,用户通过后

  • 嘿,伙计们,我目前正在为我的最终项目开发一个应用程序,这个概念是一个保持联系的应用程序,但有用户登录和注册。当用户输入他们的凭据并按下登录按钮时,它会启动一个意图移动到另一个页面,在此 Intent 上,我用 .finish() 方法结束它,这样用户就无法返回,但是当执行该操作时,用户意外按下设备上的物理后退按钮,应用程序将关闭,如果您尝试通过执行多任务物理按钮再次打开它在设备上,你选择它,它从头

  • 按下后退按钮,我试图从活动中退出应用程序。但问题是在按下返回键后,刷新相同的活动2到3次,进入登录活动,不退出应用程序。有人可以帮助解决这个问题。 以下是我必须尝试退出应用程序的方式。

  • 在我的应用程序中,我有一个注销功能。如果用户单击注销,它将进入主屏幕。现在,我按下后退按钮退出我的应用程序。但我想要的是,我需要自动退出(即以编程方式退出),就像返回按钮功能一样。我知道,通过调用finish()可以实现该功能。但问题是,它会转到上一个活动。

  • 问题内容: 我很难找到一种方法来启动,停止和重新启动Java中的线程。 具体来说,我在中有一个类Task(当前实现)。我的主应用程序需要能够在线程上启动此任务,在需要时停止(杀死)该线程,有时还可以杀死并重新启动该线程… 我的第一次尝试是与,但我似乎找不到办法重新启动任务。当我使用任何将来的呼叫失败时,因为是“关机” … 那么,我该怎么做呢? 问题答案: 一旦线程停止,你将无法重新启动它。但是,没