运行 单击按钮时又是一个程序?

Running a program again on button click?

所以我需要程序做的是在dialogResult为0(Zahl)时重新生成一个新的随机数(是的选项)。我认为重新启动程序将是实现这一目标的一种方式。我尝试使用 System.exit(0) 关闭 JFrame,之后将可见性设置为 true (f.setVisible(true)).那没有用,框架没有重新出现。我也试过 SwingUtilities.updateComponentTreeUI(f);, f.revalidate();f.repaint(), none 有效。

有没有其他方法可以“重新加载”JFrame,或者只生成一个新的随机数会更好吗?

提前致谢!

import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.util.Random;
    
    public class Zahlenraten{
        static int i = 0;
        public static void main(String[] args) {
            
            JFrame f = new JFrame("Zahlenraten");
            f.setSize(500,500);
            //f.setLayout(null);
            
            JTextField t = new JTextField("");
            t.setBounds(95,10,150,30);
            f.addWindowFocusListener(new WindowAdapter() {
             
                public void windowGainedFocus(WindowEvent e) {
                    t.requestFocusInWindow();
                }
            });
            
            
            JButton e = new JButton("Exit");
            e.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                System.exit(0);
                }
            });
            e.setBounds(300, 100, 100, 30);
            
            JLabel txt = new JLabel("Zahl eingeben:");
            txt.setBounds(10,10,150,30);
            
            Random r = new Random();
            int Zahl = r.nextInt(100-0+1);
            
            
            
            JButton b = new JButton("Prüfen");
            f.getRootPane().setDefaultButton(b);//Enter Key = button b
            b.addActionListener(evt -> { //statt evt könnte man natürlich auch actionPerformed... nehmen.
                 i++;
                 
                 String str = t.getText();
                 int wert = Integer.parseInt(str);
                 t.setText("");
                 
                 if(wert > Zahl) {
                     System.out.println("Der gesuchte Wert ist kleiner.");
                 }
                 if(wert < Zahl) {
                     System.out.println("Der gesuchte Wert ist größer.");
                 }
                 if(wert == Zahl) {
                     System.out.println("Der gesuchte Wert wurde gefunden! Du hast "+i+" Versuche gebraucht.");
                     int dialogResult = JOptionPane.showConfirmDialog(null, "Wollen Sie noch eine Runde spielen?","Frage",JOptionPane.YES_NO_OPTION);
                     if(dialogResult==0) {
                         
                     }else {
                         System.exit(0);
                     }
                 }
            });
            b.setBounds(10,100,100,30);
            
            JPanel p = new JPanel();
            p.setLayout(null);
            p.add(b);
            p.add(e);
            p.add(t);
            p.add(txt);
            f.add(p);
            f.setVisible(true);

            
        }
    }

目前,你的做法就像是你想倒车就关掉发动机——这种做法显然是不必要的,因为你可以在不重新启动汽车的情况下换档。

您需要将逻辑和 UI 分成两个单独的 class。将您的 UI 视为遥控器,将包含逻辑的 class 视为执行从 UI 接收到的命令的引擎。您的 UI class 创建了您的引擎实例 class.

引擎 class 运行 是一个游戏循环,一直持续到 运行 直到你告诉它停止,所以在编程术语中,你的程序有 状态 : 游戏进行中,或游戏不在进行中。

boolean imGange = true;
boolean ratenFalsch = true;

while(imGange)
{
    // generate random number
    
    while(ratenFalsch)
    {
        // ask for a guess
        // if the guess is correct
            // print message: right
            ratenFalsch = false;
        // if the guess is incorrect
            // print message: wrong
    }
    // ask user to play again
    // if no
        imGange = false; // game will end, but the program will still be running
}

确保您的引擎 运行 正常并且您可以在构建 UI 之前通过控制台玩游戏。当游戏 运行 正确时,您必须将 UI 中的按钮映射到引擎中的方法。例如,您的 UI 有一个按钮“Neu Spiel”,它从引擎调用相应的方法,例如 mainLoop(),其中我上面概述的所有逻辑都包含在名为 [=12 的方法中=].

以这种方式组织代码意味着只要您的 UI 是 运行ning,您的引擎就是 运行ning。使用主循环意味着您可以随时开始新游戏,无需停止和开始 UI,并且每局游戏都会获得一个新的随机数。