Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JButton not visible until mouseover

I'm creating a gui for my project. When the gui is first loaded only background is visible, so buttons are not visible, but when mouse over them, they are visible. What is the solve this problem?

public class Home extends JFrame{
//New JPanel 
private JPanel home;

//Creating image url. You must be change url
ImageIcon icon = new ImageIcon("img//home1.jpeg");

//Home Class
public Home(){

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 960, 640);
    setTitle("LoneyTunes Crush");
    home = new JPanel();
    home.setBorder(new EmptyBorder(5, 5, 5, 5));
    home.setLayout(new BorderLayout(0, 0));
    setContentPane(home);

    getContentPane().setLayout(null);
    JLabel background = new JLabel(new ImageIcon("img//giphy."));
    getContentPane().add(background);
    background.setLayout(new FlowLayout());

        //Creating Buttons
    JButton play = new JButton("Play");
    play.setBounds(20, 20, 200, 30);
    JButton setting = new JButton("Settings");
    setting.setBounds(20, 60, 200, 30);
    JButton exit = new JButton("Exit");
    exit.setBounds(20, 100, 200, 30);
       //Adding Buttons
    home.add(play);
    home.add(setting);
    home.add(exit);

            //ActionListeners
    play.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
               home.setVisible(false);
               difficulty.setVisible(true);

            }

          });

    exit.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            System.exit(1);   

            }

          });


    validate();


}

//Background paint method
public void paint(Graphics g){

    g.drawImage(icon.getImage(), 0, 0, getWidth(), getHeight(), null);


}   

}

Main Class

public class MainClass {
public static Home pencere;

public static void main(String args[]){
    pencere=new Home();
    pencere.setVisible(true);
}

}

like image 741
rdmzcn Avatar asked Mar 11 '14 10:03

rdmzcn


1 Answers

  1. Don't paint on top-level containers like JFrame as they already carry the burden of painting all of it's components.

  2. Instead paint on JPanel or JComponent and Override it's paintComponent method.

  3. On top of overriding paintComponent (or in your case paint), you need to also call super.paintComponent (in your case super.paint) inside the the method (first call under the method signature), as to not break the paint chain. Failing to do so may and probably will leave you with undesired paint artifacts.

  4. Avoid using null layouts for a number of reason. Different platform will treat them differently. They are difficult to maintain, among many other reasons. Instead use layout managers, and let them do the laying out and sizing of the components, as they were designed to do with Swing apps. Learn more at Laying out components Within a Container

  5. Setting Home pancere as a static class member of MainClass is completely pointless. Just declare and instantiate both in the main method.

  6. Swing apps should be run on the Event Dispatch Thread (EDT). You can do so by wrapping your the code inside your main method with a SwingUtilities.invokeLater.... See more at Initial Threads

  7. Instead of trying to make panels visible and not visible or adding an removing panel, consider using a CardLayout which will "layer" panels, and you can navigate through them with CardLayout's methods like show(), next(), previous(). See more at How to Use CardLayout

  8. By time of deployments, the images you are using will need to become embedded resources, and should be loaded from the class path, and not from the file system. When you pass a String to ImageIcon, you are telling the program to look in the file system, which may work in your development environment, but that's it. See the wiki tag on embedded-resource an pay close attention to the very last link that will provide you will some resources on how to use and load embedded resources if the info doesn't provide enough detail.

like image 86
Paul Samsotha Avatar answered Oct 20 '22 01:10

Paul Samsotha