简体   繁体   中英

My buttons wont appear until i hover over it with my mouse

i have this problem and yes i saw that other people had the problem to but i not realy able to compare there code to mine and see the problem that way so i hope u can help me.

i use intellij to write my code and use there gui desinger to make gui's but when i added a button i didnt get it to display untill i hover it with a mouse and the possitions are wrong and i cant realy get it to work. here are the classes // this is the jpanel class public class paintMenu extends JPanel{

public JPanel menuPanel;
public JButton newGameButt;
public JButton loadGameButt;
public JButton helpbutt;
public JButton optionsButt;
public JButton info;
public JButton quitButt;

public paintMenu(){

    add(newGameButt);
    add(loadGameButt);
    add(helpbutt);
    add(info);
    add(optionsButt);
    add(quitButt);
    setVisible(true);


}

//this is de jframe class 
public class jframepainter extends JFrame {

paintMenu menupaint = new paintMenu();

public jframepainter(){


    //main frame settings
    setTitle("Kingdom V " + Reference.version);
    setSize(Reference.width, Reference.height);
    setResizable(false);
    setLocationRelativeTo(null);
    setVisible(Kingdom.vissible);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //draw jpnael
    getContentPane().add(menupaint);

}

I don't know what the Kingdom class is, but i can assume that vissible is a typo and may be causing a compile-time error. You didn't clearly describe your problem.

Try setting the JFrame to visible after you add your JPanel to it. Also you may want to call this.pack() after you add your JPanel.

//main frame settings
setTitle("Kingdom V " + Reference.version);
setSize(Reference.width, Reference.height);
setResizable(false);
setLocationRelativeTo(null);
//draw jpnael
getContentPane().add(menupaint);  //Moved this before setting Visible
this.pack();                      // call pack before setting visible
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(Kingdom.vissible);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM