Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:

Java
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) {
		                // TODO Auto-generated method stub
		                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) {
						// TODO Auto-generated method stub
						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) {
						// TODO Auto-generated method stub
						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) {
						// TODO Auto-generated method stub
						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) {
						// TODO Auto-generated method stub
						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) {
		                // TODO Auto-generated method stub
		                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) {
				// TODO Auto-generated method stub
				
			}
		    
		    
		}
		
	}
		
		
}
Posted
Updated 18-Apr-21 1:19am
v2
Comments
Richard MacCutchan 18-Apr-21 7:22am    
You do not have a main method in your code, so there is nothing that the Java VM can find to run the class.
Jon McKee 18-Apr-21 7:40am    
It also has multiple null pointer exceptions once you add a main. I'm reminded of why you never nest class definitions in Java as well...

gamelevel1.Pictureslevel1.PicturesLevel1 game = new gamelevel1().new Pictureslevel1().new PicturesLevel1();
Richard MacCutchan 18-Apr-21 8:02am    
I do not think your comment was meant for me.
Richard MacCutchan 18-Apr-21 12:16pm    
public class gamelevel1 {

public static void main(String[] args){

}

Yes, and your main method does nothing except closing the program. I cannot work out how you managed to write all that GUI code, without understanding how to create the basic framework of a Java program. I also suspect that the first line above should not be there. Why do you need one class inside the other?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900