import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.ArrayList; public class GUI extends JFrame implements ActionListener { JMenuItem resetItem, startItem; JTextField ansTextField; JLabel resultLabel, score
please build up the java code
complete the code and fill up missing codes
// = needs input for the code
thank you
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;
public class GUI extends JFrame implements ActionListener {
JMenuItem resetItem, startItem;
JTextField ansTextField;
JLabel resultLabel, scoreLabel, timeLabel, operationLabel;
Timer timer;
DoubleDigitOp op;
final int maxTime = 6;
final int runTime = 100;
double score = 0;
int newTime = maxTime*runTime;
String[] status = new String[]{"Score: ","Timer: "};
String[] reset = new String[]{"Score: 0.00","Timer: "+((double)newTime/100),
"00 + 00 = ","Game Message"};
public GUI(String title){
setTitle(title);
//Create a Menu names "Game" (1)
//Add two menus on "Game": the menus named "Start" and "Reset" (6) -- both execute an action
JMenuBar menuBar = new JMenuBar();
menuBar.add(gameMenu);
setJMenuBar(menuBar);
JPanel content = new JPanel();
content.setLayout(new BorderLayout());
JPanel statusPanel = new JPanel();
statusPanel.setLayout(new FlowLayout());
scoreLabel = new JLabel(reset[0]); //contains the current score
statusPanel.add(scoreLabel);
timeLabel = new JLabel(reset[1]); //contains the remaining time
statusPanel.add(timeLabel);
content.add(statusPanel,BorderLayout.NORTH);
JPanel opsPanel = new JPanel();
opsPanel.setLayout(new FlowLayout());
operationLabel = new JLabel(reset[2]); //contains the operation, e.g. x + y
opsPanel.add(operationLabel);
ansTextField = new JTextField(10); //a text field where thew user orplayer enter the answer
ansTextField.addActionListener(this);
ansTextField.requestFocus();
ansTextField.setBackground(Color.LIGHT_GRAY);
opsPanel.add(ansTextField);
content.add(opsPanel,BorderLayout.CENTER);
//the resulLabel contains the current situation in the game
resultLabel = new JLabel(reset[3],SwingConstants.CENTER);
content.add(resultLabel,BorderLayout.SOUTH);
setContentPane(content);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocation(30,10);
setVisible(true);
}
public static void main(String[] args){
//write the appropriate command here (1)
}
public void actionPerformed(ActionEvent e){
if(){ //chooses to reset the game, write the correct condition (1)
//resets the answer, score, timer, operations, and result (1)
} else if(){ //chooses to start the game, write the correct condition (1)
start();
} else if(e.getSource()==ansTextField){ //when the user presses enter on the answer text field
if(){ //this is where the answer is correct, write the correct condition (1)
//print "Correct" on the result (1)
score += (double)newTime/runTime;
scoreLabel.setText(status[0]+score);
start();
} else {
resetMany(1,5);
//print "Incorrect" on the result (1)
}
} else if(e.getSource()==timer){
newTime--;
timeLabel.setText(status[1]+((double)newTime/runTime));
if(newTime==0){
resetMany(1,5);
resultLabel.setText("Time's Up!");
}
} else;
}
public void resetMany(int ... choices){
for(int choice: choices){
resetOne(choice);
}
}
public void resetOne(int choice){
if(choice == 4){
ansTextField.setText("");
} else if(choice == 0){
scoreLabel.setText(reset[0]);
score = 0;
} else if(choice == 1){
timeLabel.setText(reset[1]);
newTime = maxTime*runTime;
} else if(choice == 2){
operationLabel.setText(reset[2]);
} else if(choice == 3){
resultLabel.setText(reset[3]);
} else {
if(timer!=null){
timer.stop();
}
}
}
public void resetAll(){
ansTextField.setText("");
scoreLabel.setText(reset[0]);
score = 0;
timeLabel.setText(reset[1]);
newTime = maxTime*runTime;
operationLabel.setText(reset[2]);
resultLabel.setText(reset[3]);
if(timer!=null){
timer.stop();
}
}
public void start(){
//reset time and answer textfield (1)
//print the operation to the operationLLabel (1) -- e.g. x + y =
timer = new Timer(10,this);
timer.start();
}
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps