1

I am trying to make a button that is entirely black (with grey words). However, my button.setBackground(color.black) just gives the button a black outline. Here is my code:

public class Main{

 protected static JFrame window;

 public static void main(String args[]){
  window = new JFrame();
  window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  window.setSize(1200,800); 
  window.setVisible(true);  
  window.setResizable(false);
  window.setBackground(Color.gray);

  BottomButtons seeProgress = new BottomButtons(window, "See Progress", 200, 710);
  }
}


public class BottomButtons {

 public static JButton button;

 bottomButtons(JFrame window, String name, int x, int y){
  button = new JButton(name);
  button.setForeground(Color.LIGHT_GRAY);
  button.setBackground(Color.BLACK);
  window.add(button);
  button.setBounds(x, y, 250, 60);
  button.setOpaque(true);
 }
}
hnor4
  • 11
  • 2
  • I don't think that your posted code and text is adequate to allow us to be able to understand your problem enough to answer it. If you don't get a decent answer soon, consider creating and posting a Minimal, Complete, and Verifiable Example Program. – Hovercraft Full Of Eels Feb 11 '18 at 02:10
  • As an aside, you will want to learn and use [Java naming conventions](http://en.wikipedia.org/wiki/Naming_convention_(programming)#Java). Variable names should all begin with a lower letter while class names with an upper case letter. Learning this and following this will allow us to better understand your code, and would allow you to better understand the code of others. – Hovercraft Full Of Eels Feb 11 '18 at 02:10
  • I added more code, so hopefully that makes my problem a little more easy to understand. – hnor4 Feb 11 '18 at 02:16
  • Move `window.setVisible(true);` to the bottom of the main method. Don't display the GUI until adding all components to it. – Hovercraft Full Of Eels Feb 11 '18 at 02:20
  • I tried that, but it did not seem to make a difference – hnor4 Feb 11 '18 at 02:23
  • Also the BottomButtons are different. Your class is declared as `BottomButtons`, but you're using `bottomButtons` in the Main class??? – Hovercraft Full Of Eels Feb 11 '18 at 02:30
  • I just missed that when I was changing the code from this question to fit the Java naming conventions link. – hnor4 Feb 11 '18 at 02:32
  • `button.setBounds(x, y, 250, 60);` demonstrates a lack of understanding of how the layout management API works – MadProgrammer Feb 11 '18 at 02:51
  • `public static JButton button;` is an indication of a bad design. Either `BottomButtons` needs to extend from `JButton` or you need to set up a factory method which creates them – MadProgrammer Feb 11 '18 at 02:56

0 Answers0