-1

I'm trying this code in Java to start creating a game.I have a problem with the background color which stays by default even though I've used setBackgroundColor into the code.Can someone show me how to fix this?This is my code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.*;
import java.util.Random;


public class Background extends JFrame{ 

    private JButton b;
     public Background () {

         super("the title");
            setLayout(new FlowLayout());

         b=new JButton("ROLL THE DICES");

         b.setForeground(Color.WHITE);//ndryshon ngjyren e shkrimit
         b.setBackground(Color.YELLOW);
         b.setBounds(20, 30, 20, 70);
         add(b);

         thehandler hand=new thehandler();
         b.addActionListener(hand);
          }
     private class thehandler implements ActionListener{
         public void actionPerformed(ActionEvent event) {
                JOptionPane.showMessageDialog(null, "Ju shtypet butonin");

     }

     }


     public static void main(String[] args) {



            Background f=new Background();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setBackground(Color.GREEN);
            f.setSize(400,300);
            f.setVisible(true);
     }}
Gerald Schneider
  • 17,416
  • 9
  • 60
  • 78
Doggy
  • 63
  • 2
  • 10
  • @A. Di Matteo what's the problem with the title?That's how I want to name it. – Doggy Jan 18 '16 at 21:25
  • That's weird, I only added tags, I didn't change the title, you can revert it back, definitely. The history however points at the change as part of my edit, really weird behavior – A_Di-Matteo Jan 18 '16 at 21:27
  • Ok! Just thought that was a problem with naming the title like I did.Thought that was a coding problem.Ok thanks – Doggy Jan 18 '16 at 21:31

1 Answers1

3

Change

f.setBackground(Color.GREEN); 

to

f.getContentPane().setBackground(Color.GREEN);

You need to change the content pane background.

You can read more about it in here: JFrame.setBackground() not working -- why?

Community
  • 1
  • 1
wonryu
  • 46
  • 2