[英]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.