简体   繁体   English

在我点击 JOptionPane 上的确定按钮后,我不知道如何重置第二帧中的所有 function。 该怎么办?

[英]I don't know how to reset all the function in the 2nd frame after i hit the okay button on the JOptionPane. What to do?

I am using JCreator as my IDE and this program has two frames 1st is the startup frame you will just press start and my problem is on the 2nd frame which is I want to reset the all the function in that frame as I click OK on the JOptionPane or what would you suggest on doing.我使用 JCreator 作为我的 IDE 并且该程序有两个帧第一个是启动帧,您只需按开始,我的问题是在第二帧,我想重置该帧中的所有 function,因为我在JOptionPane或者你会建议做什么。 It is on line 135.它在第 135 行。

import javax.swing.border.Border;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class OddEven extends JFrame implements ActionListener
{
    public static void main (String args[])
    {
        new OddEven();
    }
        JButton b1;
        JProgressBar p;
        int i = 0;
        
        public OddEven() 
        {
            Border border = BorderFactory.createLineBorder(new Color(0xFFFFFF),5);
            
            JLabel l = new JLabel("ODD or EVEN");     
            l.setForeground(new Color(0x143D59));
            l.setFont(new Font("Garamond",Font.BOLD,25));
            l.setBackground(new Color(0xF4B41A));
            l.setOpaque(true);  
            l.setBorder(border);
            l.setVerticalAlignment(l.CENTER);
            l.setHorizontalAlignment(l.CENTER);     
            l.setBounds(100,65,300,50);   
                
            JPanel buttonpanel = new JPanel();
            buttonpanel.setBackground(new Color(0xF4B41A));
            buttonpanel.setBounds(100,125,300,60);
            buttonpanel.setLayout(null);
                        
            b1 = new JButton ("Start Game");
            
            b1.setBounds(100,10,100,25);
            b1.setBackground(new Color(0xFFFFFF));
            b1.setFocusable(false);
            b1.setFont(new Font("Garamond",Font.BOLD,15));
            b1.setBorder(BorderFactory.createEtchedBorder());
            
            p = new JProgressBar(0, 200);
            p.setBackground(new Color(0x143D59));
            p.setForeground(new Color(0xF49F1C));
            p.setBounds(50,40,200,20);
            p.setStringPainted(true);
            
            Timer t = new Timer(1, this);
            
            b1.addActionListener(new ActionListener ()
            {
                public void actionPerformed(ActionEvent ae)
                {
                    t.start();
                }
            });

            add(l);  
            add(buttonpanel);   
            buttonpanel.add(p);
            buttonpanel.add(b1);
                                                                                               
            setTitle("RON BALID");
            setLayout(null);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(500,300);  
            setResizable(false);                              
            setLocationRelativeTo(null);                     
            setVisible(true);
            getContentPane().setBackground(new Color(0xF4B41A));
        }
        int score = 0;
        int count = 0; 
        int lifecounter = 3;
        
        Random r = new Random();
        int randomNum;
            
        JFrame f;
        JPanel rNumpanel, aPanel, scorePanel;
        JButton l1,l2,time;
        JLabel rScore,nScore,rNum2;
        JProgressBar timeBar;
        int num;
        int timerCounter;
        Timer t1;   
        public void actionPerformed(ActionEvent ae)
        {   
            if(i==200)
            {
            //-------------------JLABEL--------------------------   
                
                Border border3 = BorderFactory.createLineBorder(new Color(0xFFFFFF),5);
                rNum2 = new JLabel();
                randomNum = r.nextInt(100);
                rNum2.setText(String.valueOf(randomNum));
                rNum2.setBounds(190,25,100,50);
                rNum2.setFont(new Font("Garamond", Font.BOLD,50));
                rNum2.setForeground(Color.red);
                rNum2.setBorder(border3);
                rNum2.setVerticalAlignment(rNum2.CENTER);
                rNum2.setHorizontalAlignment(rNum2.CENTER);
                
                rScore = new JLabel("");
                rScore.setForeground(new Color(0xFFFFFF));
                rScore.setBounds(380,10,50,50);
                rScore.setFont(new Font("Garamond", Font.BOLD,20));
                
                nScore = new JLabel("SCORE:"); 
                nScore.setForeground(new Color(0xFFFFFF));
                nScore.setBounds(300,-15,100,100);
                nScore.setFont(new Font("Garamond", Font.BOLD,20));
    
                
            //-------------------JLABEL--------------------------
                
            //-----------------CONDITION-------------------------           
            
                timeBar = new JProgressBar(0, 200);
                timeBar.setBackground(new Color(0x143D59));
                timeBar.setForeground(new Color(0xF49F1C));
                timeBar.setBounds(140,0,200,20);
                timeBar.setStringPainted(true);
                timeBar.setString("TIMER"); 
                
                time = new JButton ("10s");
                time.setForeground(new Color(0x000000));
                time.setBackground(new Color(0xFFFFFF));
                time.setBounds(55,15,50,20);
                time.setFocusable(false);
                time.setFont(new Font("Garamond",Font.BOLD,12));
                    t1 = new Timer(25, timerEvent ->
                        {
                            if (timerCounter == 200 ) 
                            {       
                                JOptionPane.showMessageDialog(this, "Your score is: "+ score +" / "+ count);
                                count = 0;
                                score = 0;              
                                rScore.setText("");
                                randomNum = r.nextInt(1000);
                                rNum2.setText(String.valueOf(randomNum));   
                                                        
                            }
                            timerCounter += 1;
                            timeBar.setValue(timerCounter);
                        });
                time.addActionListener(timeEvent ->
                    {
                    t1.start();                                         
                    }
                    );
                    
                l1 = new JButton("ODD");
                l1.setForeground(new Color(0xFFFFFF));
                l1.setBackground(new Color(0xF4B41A));
                l1.setBounds(55,13,100,50);
                l1.setFocusable(false);
                l1.setFont(new Font("Garamond",Font.BOLD,20));
                l1.setBorder(BorderFactory.createEtchedBorder()); 
                l1.addActionListener(event -> {
            count = count+1;
            num = Integer.parseInt(rNum2.getText());
            
            if (timerCounter == 200)
            {
                if (num%2!=0)
                {       
                    score = score+1;
                    rScore.setText(String.valueOf(score));
                    randomNum = r.nextInt(1000);    
                }
                else
                {
                    score -=1;
                    rScore.setText(String.valueOf(score));
                    randomNum = r.nextInt(1000);
                    rNum2.setText(String.valueOf(randomNum));   
                }
            }
            else
            {
                if (num%2!=0)
                {
                    score = score+1;
                    rScore.setText(String.valueOf(score));
                    randomNum = r.nextInt(1000);
                    rNum2.setText(String.valueOf(randomNum));                       
                }
                else
                {   

                    score -=1;
                    rScore.setText(String.valueOf(score));
                    randomNum = r.nextInt(1000);
                    rNum2.setText(String.valueOf(randomNum));
                }
            }
        });         
                l2 = new JButton("EVEN");
                l2.setForeground(new Color(0xFFFFFF));
                l2.setBackground(new Color(0xF4B41A));
                l2.setBounds(315,13,100,50);
                l2.setFocusable(false);
                l2.setFont(new Font("Garamond",Font.BOLD,20));
                l2.setBorder(BorderFactory.createEtchedBorder());
                l2.addActionListener(act -> {
            count = count+1;
            num = Integer.parseInt(rNum2.getText());
            if (timerCounter == 200)
            {
                if (num%2==0)
                {
                    score = score+1;
                    rScore.setText(String.valueOf(score));
                    randomNum = r.nextInt(1000);
                }
                else
                {                       

                    score -=1;
                    rScore.setText(String.valueOf(score));
                    randomNum = r.nextInt(1000);    
                    rNum2.setText(String.valueOf(randomNum));               
                }

            }
            else
            {
                if (num%2==0)
                {                   
                    score = score+1;
                    rScore.setText(String.valueOf(score));
                    randomNum = r.nextInt(1000);
                    rNum2.setText(String.valueOf(randomNum));
                    
                }
                else
                {                                                                               
                    score -=1;
                    rScore.setText(String.valueOf(score));
                    randomNum = r.nextInt(1000);
                    rNum2.setText(String.valueOf(randomNum));
                }
            }
            });
            
            //-----------------CONDITION-------------------------
            
            //-----------------PANELS----------------------------           
                aPanel = new JPanel ();
                aPanel.setLayout(null);
                aPanel.setBackground(new Color(0x143D59));
                aPanel.setBounds(10,110,475,75);
                
                rNumpanel = new JPanel();
                rNumpanel.setLayout(null);
                rNumpanel.setBackground(new Color(0x143D59));
                rNumpanel.setBounds(10,20,475,75);
                
                scorePanel = new JPanel ();
                scorePanel.setLayout(null);
                scorePanel.setBackground(new Color(0x143D59));
                scorePanel.setBounds(10,180,475,75);
            //-----------------PANELS----------------------------   
            //------------------FRAME----------------------------           
                f = new JFrame("ODD or EVEN Game");
                f.setLayout(null);
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setSize(500,300);  
                f.setResizable(false);                              
                f.setLocationRelativeTo(null);                     
                f.setVisible(true);
                f.getContentPane().setBackground(new Color(0x143D59));
                
                        
                f.add(aPanel);
                f.add(rNumpanel);
                f.add(scorePanel);
                aPanel.add(l1);
                aPanel.add(l2); 
                
                
        
                rNumpanel.add(rNum2);   
                scorePanel.add(rScore);
                scorePanel.add(nScore);
                scorePanel.add(time);
                rNumpanel.add(timeBar);
                
         //------------------FRAME----------------------------  
                                        
                dispose();
            }
            i++;
            p.setValue(i);      
        }
}
import javax.swing.border.Border;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class OddEven extends JFrame implements ActionListener
{
    public static void main (String args[])
    {
        new OddEven();
    }
        JButton b1;
        JProgressBar p;
        int i = 0;
        
        public OddEven() 
        {
            Border border = BorderFactory.createLineBorder(new Color(0xFFFFFF),5);
            
            JLabel l = new JLabel("ODD or EVEN");     
            l.setForeground(new Color(0x143D59));
            l.setFont(new Font("Garamond",Font.BOLD,25));
            l.setBackground(new Color(0xF4B41A));
            l.setOpaque(true);  
            l.setBorder(border);
            l.setVerticalAlignment(l.CENTER);
            l.setHorizontalAlignment(l.CENTER);     
            l.setBounds(100,65,300,50);   
                
            JPanel buttonpanel = new JPanel();
            buttonpanel.setBackground(new Color(0xF4B41A));
            buttonpanel.setBounds(100,125,300,60);
            buttonpanel.setLayout(null);
                        
            b1 = new JButton ("Start Game");
            
            b1.setBounds(100,10,100,25);
            b1.setBackground(new Color(0xFFFFFF));
            b1.setFocusable(false);
            b1.setFont(new Font("Garamond",Font.BOLD,15));
            b1.setBorder(BorderFactory.createEtchedBorder());
            
            p = new JProgressBar(0, 200);
            p.setBackground(new Color(0x143D59));
            p.setForeground(new Color(0xF49F1C));
            p.setBounds(50,40,200,20);
            p.setStringPainted(true);
            
            Timer t = new Timer(1, this);
            
            b1.addActionListener(new ActionListener ()
            {
                public void actionPerformed(ActionEvent ae)
                {
                    t.start();
                }
            });

            add(l);  
            add(buttonpanel);   
            buttonpanel.add(p);
            buttonpanel.add(b1);
                                                                                               
            setTitle("RON BALID");
            setLayout(null);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(500,300);  
            setResizable(false);                              
            setLocationRelativeTo(null);                     
            setVisible(true);
            getContentPane().setBackground(new Color(0xF4B41A));
        }
        int score = 0;
        int count = 0; 
        int lifecounter = 3;
        
        Random r = new Random();
        int randomNum;
            
        JFrame f;
        JPanel rNumpanel, aPanel, scorePanel;
        JButton l1,l2,time;
        JLabel rScore,nScore,rNum2;
        JProgressBar timeBar;
        int num;
        int timerCounter;
        Timer t1;   
        public void actionPerformed(ActionEvent ae)
        {   
            if(i==200)
            {
            //-------------------JLABEL--------------------------   
                
                Border border3 = BorderFactory.createLineBorder(new Color(0xFFFFFF),5);
                rNum2 = new JLabel();
                randomNum = r.nextInt(100);
                rNum2.setText(String.valueOf(randomNum));
                rNum2.setBounds(190,25,100,50);
                rNum2.setFont(new Font("Garamond", Font.BOLD,50));
                rNum2.setForeground(Color.red);
                rNum2.setBorder(border3);
                rNum2.setVerticalAlignment(rNum2.CENTER);
                rNum2.setHorizontalAlignment(rNum2.CENTER);
                
                rScore = new JLabel("");
                rScore.setForeground(new Color(0xFFFFFF));
                rScore.setBounds(380,10,50,50);
                rScore.setFont(new Font("Garamond", Font.BOLD,20));
                
                nScore = new JLabel("SCORE:"); 
                nScore.setForeground(new Color(0xFFFFFF));
                nScore.setBounds(300,-15,100,100);
                nScore.setFont(new Font("Garamond", Font.BOLD,20));
    
                
            //-------------------JLABEL--------------------------
                
            //-----------------CONDITION-------------------------           
            
                timeBar = new JProgressBar(0, 200);
                timeBar.setBackground(new Color(0x143D59));
                timeBar.setForeground(new Color(0xF49F1C));
                timeBar.setBounds(140,0,200,20);
                timeBar.setStringPainted(true);
                timeBar.setString("TIMER"); 
                
                time = new JButton ("10s");
                time.setForeground(new Color(0x000000));
                time.setBackground(new Color(0xFFFFFF));
                time.setBounds(55,15,50,20);
                time.setFocusable(false);
                time.setFont(new Font("Garamond",Font.BOLD,12));
                    t1 = new Timer(25, timerEvent ->
                        {
                            if (timerCounter == 200 ) 
                            {
                                
                                JOptionPane.showMessageDialog(this, "Your score is: "+ score +" / "+ count);
                                count = 0;
                                score = 0;              
                                rScore.setText("");
                                randomNum = r.nextInt(1000);
                                rNum2.setText(String.valueOf(randomNum));   
                                                        
                            }
                            timerCounter += 1;
                            timeBar.setValue(timerCounter);
                        });
                time.addActionListener(timeEvent ->
                    {
                    t1.start();                                         
                    }
                    );
                    
                l1 = new JButton("ODD");
                l1.setForeground(new Color(0xFFFFFF));
                l1.setBackground(new Color(0xF4B41A));
                l1.setBounds(55,13,100,50);
                l1.setFocusable(false);
                l1.setFont(new Font("Garamond",Font.BOLD,20));
                l1.setBorder(BorderFactory.createEtchedBorder()); 
                l1.addActionListener(event -> {
            count = count+1;
            num = Integer.parseInt(rNum2.getText());
            
            if (timerCounter == 200)
            {
                if (num%2!=0)
                {       
                    score = score+1;
                    rScore.setText(String.valueOf(score));
                    randomNum = r.nextInt(1000);    
                }
                else
                {
                    score -=1;
                    rScore.setText(String.valueOf(score));
                    randomNum = r.nextInt(1000);
                    rNum2.setText(String.valueOf(randomNum));   
                }
            }
            else
            {
                if (num%2!=0)
                {
                    score = score+1;
                    rScore.setText(String.valueOf(score));
                    randomNum = r.nextInt(1000);
                    rNum2.setText(String.valueOf(randomNum));                       
                }
                else
                {   

                    score -=1;
                    rScore.setText(String.valueOf(score));
                    randomNum = r.nextInt(1000);
                    rNum2.setText(String.valueOf(randomNum));
                }
            }
        });         
                l2 = new JButton("EVEN");
                l2.setForeground(new Color(0xFFFFFF));
                l2.setBackground(new Color(0xF4B41A));
                l2.setBounds(315,13,100,50);
                l2.setFocusable(false);
                l2.setFont(new Font("Garamond",Font.BOLD,20));
                l2.setBorder(BorderFactory.createEtchedBorder());
                l2.addActionListener(act -> {
            count = count+1;
            num = Integer.parseInt(rNum2.getText());
            if (timerCounter == 200)
            {
                if (num%2==0)
                {
                    score = score+1;
                    rScore.setText(String.valueOf(score));
                    randomNum = r.nextInt(1000);
                }
                else
                {                       

                    score -=1;
                    rScore.setText(String.valueOf(score));
                    randomNum = r.nextInt(1000);    
                    rNum2.setText(String.valueOf(randomNum));               
                }

            }
            else
            {
                if (num%2==0)
                {                   
                    score = score+1;
                    rScore.setText(String.valueOf(score));
                    randomNum = r.nextInt(1000);
                    rNum2.setText(String.valueOf(randomNum));
                    
                }
                else
                {                                                                               
                    score -=1;
                    rScore.setText(String.valueOf(score));
                    randomNum = r.nextInt(1000);
                    rNum2.setText(String.valueOf(randomNum));
                }
            }
            });
            
            //-----------------CONDITION-------------------------
            
            //-----------------PANELS----------------------------           
                aPanel = new JPanel ();
                aPanel.setLayout(null);
                aPanel.setBackground(new Color(0x143D59));
                aPanel.setBounds(10,110,475,75);
                
                rNumpanel = new JPanel();
                rNumpanel.setLayout(null);
                rNumpanel.setBackground(new Color(0x143D59));
                rNumpanel.setBounds(10,20,475,75);
                
                scorePanel = new JPanel ();
                scorePanel.setLayout(null);
                scorePanel.setBackground(new Color(0x143D59));
                scorePanel.setBounds(10,180,475,75);
            //-----------------PANELS----------------------------   
            //------------------FRAME----------------------------           
                f = new JFrame("ODD or EVEN Game");
                f.setLayout(null);
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setSize(500,300);  
                f.setResizable(false);                              
                f.setLocationRelativeTo(null);                     
                f.setVisible(true);
                f.getContentPane().setBackground(new Color(0x143D59));
                
                        
                f.add(aPanel);
                f.add(rNumpanel);
                f.add(scorePanel);
                aPanel.add(l1);
                aPanel.add(l2); 
                
                
        
                rNumpanel.add(rNum2);   
                scorePanel.add(rScore);
                scorePanel.add(nScore);
                scorePanel.add(time);
                rNumpanel.add(timeBar);
                
         //------------------FRAME----------------------------  
                                        
                dispose();
            }
            i++;
            p.setValue(i);      
        }
}

I completed the code and it works well now, what I did was reset the counter of my timer and then stop it.我完成了代码,它现在运行良好,我所做的是重置计时器的计数器,然后停止它。

if (timerCounter == 200 ){ 
    JOptionPane.showMessageDialog(this, "Your score is: "+ score +" / "+ count); 
    count = 0; 
    score = 0; 
    rScore.setText(""); 
    randomNum = r.nextInt(1000); 
    rNum2.setText(String.valueOf(randomNum)); 
    timerCounter = 0; 
    t1.stop();
} 

It's on line 135 in the end.它最后在第 135 行。 I don't need to reset the JFrame , just the timer.我不需要重置JFrame ,只需重置计时器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JOptionPane 的输入验证。 我如何确保输入是双精度的? - Input validation for JOptionPane. How would I make sure the input is a double? 第二个JOptionPane不会出现 - 2nd JOptionPane won't come up 我在模拟中有一个NullPointerException,但我不知道该怎么办 - I have a NullPointerException in a mock, and I don't know what to do 它说:“错误:不是声明”,我不知道该怎么办 - It says: "error: not a statement", I don't know what to do 变量可能未初始化错误(研究了 20 分钟后我不知道该怎么办) - Variable might not have been initialized error (I don't know what to do after 20 minutes of research) 如何显示起始值的所有倍数与第二个参数状态一样多? - How do I display all multiples of the starting value for as many multiples as the 2nd parameter states? 如果我不知道变量是正/负,如何在不调用函数或不使用if的情况下使其变为正数? - If I don't know if a variable is positive/negative, how do I make it positive without calling a function or using if? 我不知道如何验证电话号码是否已经存在。 我只是新手,现在不知道该怎么办 - I don't know how to verify if the phone number already exist. I'm just a newbie and i don't know what to do now 如何从另一个框架到第二个框架在文本字段中获取输入的文本? - How can i get the inputted text in a textfield from another frame to the 2nd frame? toString方法我不知道如何使用它和目的是什么 - toString method I don't know how to use it and what the purpose
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM