0

I've been following this tutorial on youtube on how to create a text-based adventure game. I've been following the tutorial step-by-step but unfortunately, I cannot seem to find a solution to my issue.

Here is my block of code (I've narrowed it down to the JPanel/JButton portion):

public Game() {
    window = new JFrame();
    window.setSize(800, 600);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.getContentPane().setBackground(Color.black);
    window.setLayout(null);

    con = window.getContentPane();

    // button panel
    buttonPanel = new JPanel();
    buttonPanel.setBounds(300, 400, 200, 100);
    buttonPanel.setBackground(Color.black);

    //start button 
    pushButton = new JButton("START");
    pushButton.setBackground(Color.black);
    pushButton.setForeground(Color.white);
    pushButton.setFont(normalFont);
    pushButton.setFocusPainted(false);

    buttonPanel.add(pushButton);    

    con.add(buttonPanel);

    window.setVisible(true);
}

Here's a photo of what my button looks like: Unclicked Clicked

Some of the solutions I've tried include adding the following, both in combination and different orders:

    pushButton.setContentAreaFilled(false);
    pushButton.setOpaque(true);
    pushButton.setOpaque(false);
    pushButton.setFocusable(true);

And together:

    pushButton.getBackground();
    pushButton.setBackground(Color.BLACK);

But so far, none of them work or get the results I want -- which is to have a button with a BLACK background and WHITE text.

*I have been searching through Stackoverflow and other websites for an answer but so far, there seems to be no one else having similar issues as the one I am having. **I am not sure if the OS that I am using Eclipse on makes a difference or not.

Please let me know if more information needs to be provided! Thank you all in advance.

  • After conducting extensive research on other forums, it would appear that using “.setBackground” doesn’t necessarily show up or work BASED ON THE OS YOU ARE USING. Hence the reason why none of these “solutions” on Stackoverflow are working for me. – IntrovertTechie Nov 30 '17 at 15:09

1 Answers1

0

Use setOpaque(true) first.

 btn = new JButton();
 btn.setOpaque(true);
 btn.setBackground(backgroundColor);
Stanley Ko
  • 3,383
  • 3
  • 34
  • 60
  • While looking for a solution (before submitting this question), I have already came across this suggest answer and unfortunately it does not change anything about the background color of the button. The Button background is still defaulting to white. – IntrovertTechie Nov 30 '17 at 05:40