Napisałem program, okienkowy, losujący dwie liczby z przedziału i mający sprawdzić odpowiedź użytkownika odnośnie sumy tych liczb. Jednak w momencie gdy chciałbym aby wyświetlił jedną z opcji ( Dobrze lub Źle ) nie wyświetla wcale. Wielkie dzięki za pomoc oraz za krytykę (chociaż się czegoś dowiem)
PS: Wydaje mi się że brakuje tam linijki która miałaby odświeżyć zawartość okna, jednak nie potrafiłem takowej znaleźć, a jeśli nie to niepotrzebnie wprowadzam w błąd za co przepraszam.
Poniżej kod :
Nazwa klasy : Calculate
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Calculate extends JFrame implements ActionListener
{
Random randomGenerator = new Random();
public int randomNumber1;
public int randomNumber2;
public int bottomNumber = 2;
public int topNumber = 100;
public int answer;
JButton exitButton= new JButton("Wyjście");
JButton ranNumButton= new JButton("Wylosuj Liczby");
JLabel num1Label = new JLabel("x");
JLabel num2Label = new JLabel("y");
JTextField answerField = new JTextField("");
JButton checkAnswer = new JButton("Sprawdz odpowiedz!");
JLabel goodAnswer = new JLabel("Dobrze !");
public boolean goodAnswerVisibility = false;
JLabel badAnswer = new JLabel("Źle !");
public boolean badAnswerVisibility = false;
public Calculate()
{
setSize(500,500);
setTitle("Calculate");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setLayout(null);
ranNumButton.setBounds(100,50,300,50);
ranNumButton.setForeground(new Color(0,0,0));
ranNumButton.setFont(new Font("Impact",Font.HANGING_BASELINE,20));
add(ranNumButton);
ranNumButton.addActionListener(this);
exitButton.setBounds(350,15,100,15);
add(exitButton);
exitButton.addActionListener(this);
num1Label.setBounds(125, 125, 25, 25);
add(num1Label);
num2Label.setBounds(225, 125, 25, 25);
add(num2Label);
answerField.setBounds(300, 125, 50, 25);
add(answerField);
checkAnswer.setBounds(210, 175, 200, 50);
add(checkAnswer);
JLabel plusSign = new JLabel("+");
plusSign.setBounds(175, 125, 25, 25);
add(plusSign);
JLabel equalitySign = new JLabel("=");
equalitySign.setBounds(275, 125, 25, 25);
add(equalitySign);
goodAnswer.setBounds(275, 425, 100, 25);
goodAnswer.setForeground(Color.GREEN);
badAnswer.setBounds(275, 425, 100, 25);
badAnswer.setForeground(Color.RED);
if( goodAnswerVisibility == true)
{
add(goodAnswer);
remove(badAnswer);
}
else if (badAnswerVisibility == true)
{
add(badAnswer);
remove(goodAnswer);
}
}
public static void main(String[] args)
{
Calculate Window = new Calculate();
}
public void actionPerformed(ActionEvent e) {
Object source =e.getSource();
if(source==ranNumButton)
{
randomNumber1=randomGenerator.nextInt(topNumber - bottomNumber +1 )+ bottomNumber;
num1Label.setText(String.valueOf(randomNumber1));
randomNumber2=randomGenerator.nextInt(topNumber - bottomNumber +1 )+ bottomNumber;
num2Label.setText(String.valueOf(randomNumber2));
}
if(source==exitButton)
{
dispose();
}
if(source==checkAnswer)
{
answer=Integer.valueOf(answerField.getText());
if(answer==randomNumber1+randomNumber2)
{
badAnswerVisibility = false;
goodAnswerVisibility = true;
}
else
{
badAnswerVisibility = true;
goodAnswerVisibility = false;
}
}
}
}