【问题标题】:How to restart a program if a button is pressed in java?如果在java中按下按钮,如何重新启动程序?
【发布时间】:2015-01-11 12:21:19
【问题描述】:

我正在使用 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);
        }
    }
}

}

感谢您的宝贵时间。

【问题讨论】:

    标签: java jframe


    【解决方案1】:

    这样的?

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-22
      • 2017-09-24
      • 1970-01-01
      • 2010-12-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多