My problem is when I run this code it should normally pop up a window, but that's not the case.
can anyone help me with that please.
Thank you!
What I have tried:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.awt.Color;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.JTextField;
public class gamelevel1 {
public class Pictureslevel1 {
public class PicturesLevel1 extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
public JFrame frame;
public JPanel panel;
public JLabel label1;
public JLabel label2;
public JLabel label3;
public JButton next;
public JButton tryagain;
public JButton replay;
public JButton buttonA;
public JButton buttonB;
public JButton buttonC;
public ImageIcon tomatocolor;
public ImageIcon walvis;
public ImageIcon zon;
public JTextField vraag;
public JTextField felicitatie;
public JTextField foutmelding;
public Timer hidePicture;
public int counter;
int index;
char[] answers = {
'A',
'B',
'C'
};
public PicturesLevel1() {
this.getContentPane().setLayout(new FlowLayout());
frame = new JFrame("colourgame");
frame.setSize(420, 420);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
String[] questions = {
"Guess the color of the tomato",
"Guess the color of the whale",
"Guess the color of the sun"
};
foutmelding = new JTextField("Wrong, try again.");
felicitatie = new JTextField("Good job!");
foutmelding.setVisible(false);
felicitatie.setVisible(false);
foutmelding.setBounds(200, 390, 50, 20);
felicitatie.setBounds(200, 390, 50, 20);
tomatocolor = new ImageIcon("src/tomato.colour.jpg");
walvis = new ImageIcon("src/walvis.colour.jpg");
zon = new ImageIcon("src/zon.colour.jpg");
label1 = new JLabel(tomatocolor, JLabel.CENTER);
label2 = new JLabel(walvis, JLabel.CENTER);
label3 = new JLabel(zon, JLabel.CENTER);
vraag.setBounds(0,0,200,50);
vraag.setBackground(new Color(51,153,255));
vraag.setForeground(new Color(0,0,0));
vraag.setFont(new Font("Monospaced",Font.PLAIN,12));
vraag.setBorder(BorderFactory.createBevelBorder(1));
vraag.setHorizontalAlignment(JTextField.CENTER);
vraag.setText(questions[index]);
vraag.setVisible(true);
panel = new JPanel();
panel.setBounds(180,180, 100, 50);
panel.add(label1);
panel.add(label2);
label2.setVisible(false);
panel.add(label3);
label3.setVisible(false);
replay = new JButton();
replay.setBounds(100, 300, 100, 50);
replay.setText("Show again");
replay.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (counter == 0) {
label1.setVisible(true);
} else if (counter == 1){
label2.setVisible(true);
}
else{
label3.setVisible(true);
}
}
});
panel.add(replay);
tryagain = new JButton();
tryagain.setBounds(150, 300, 100, 50);
tryagain.setText("Try again");
tryagain.setVisible(false);
tryagain.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
foutmelding.setVisible(false);
tryagain.setVisible(false);
}
});
next = new JButton();
next.setBounds(300, 300, 100, 50);
next.setText("Next picture");
next.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (counter == 0) {
label1.setVisible(false);
label2.setVisible(true);
counter++;
felicitatie.setVisible(false);
} else {
counter++;
label2.setVisible(false);
label3.setVisible(true);
felicitatie.setVisible(false);
}
}
});
panel.add(next);
next.setVisible(false);
panel.add(vraag);
buttonA.setBounds(100,0,100,100);
buttonA.setFont(new Font("San serif",Font.BOLD,35));
buttonA.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(answers[index] == 'A') {
next.setVisible(true);
felicitatie.setVisible(true);
index++;
vraag.setText(questions[index]);
}
else {
tryagain.setVisible(true);
foutmelding.setVisible(true);
}
}
});
buttonA.setFocusable(false);
buttonA.setBackground(new Color(255, 0, 0));
panel.add(buttonA);
buttonB.setBounds(0,100,100,100);
buttonB.setFont(new Font("San serif",Font.BOLD,35));
buttonB.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(answers[index] == 'B') {
next.setVisible(true);
felicitatie.setVisible(true);
index++;
vraag.setText(questions[index]);
}
else {
tryagain.setVisible(true);
foutmelding.setVisible(true);
}
}
});
buttonB.setFocusable(false);
buttonB.setBackground(new Color(0, 0, 255));
panel.add(buttonB);
buttonC.setBounds(0,100,100,100);
buttonC.setFont(new Font("San serif",Font.BOLD,35));
buttonC.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(answers[index] == 'C') {
next.setVisible(true);
felicitatie.setVisible(true);
index++;
vraag.setText(questions[index]);
}
else {
tryagain.setVisible(true);
foutmelding.setVisible(true);
}
}
});
buttonC.setFocusable(false);
buttonC.setBackground(new Color (255, 255, 0));
panel.add(buttonC);
hidePicture = new Timer(7000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(counter == 0){
label1.setVisible(false);
} else if (counter == 1){
label2.setVisible(false);
} else{
label3.setVisible(false);
}
}
});
hidePicture.start();
frame.add(panel);
}
@Override
public void actionPerformed(ActionEvent e) {
}
}
}
}