[英]JButtons not showing in JApplet before mouse contact
在將鼠標移到按鈕上之前,我的小程序沒有顯示按鈕,我遇到了這個問題。 我用谷歌搜索,發現“ validate();” 可能有幫助。 它沒。 也許我做錯了,但是找不到其他解決方案。 這是我的代碼:
public void paintMainMenu(){
Container conMain = getContentPane();
play = new JButton("Play");
credits = new JButton("Credits");
controls = new JButton("Controls");
FlowLayout lay1 = new FlowLayout();
JPanel mainPanel = new JPanel();
mainPanel.setSize(SW, SH);
mainPanel.setLayout(lay1);
mainPanel.add(play);
mainPanel.add(credits);
mainPanel.add(controls);
play.addActionListener(this);
credits.addActionListener(this);
controls.addActionListener(this);
conMain.add(mainPanel);
conMain.validate(); // Validation...
setContentPane(conMain);
}
我認為這里存在問題,但是如果您想要完整的代碼,請告訴。
謝謝!
您需要revalidate(); AND repaint();
按鈕應該出現
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.