为什么我的JButton不显示?

时间:2018-01-11 14:43:54

标签: java jframe jbutton jlabel

public class StartMenu extends JFrame {
public static void StartMenu() {
   JFrame StartMenu = new JFrame("SamBallPool");  //New Windwow With Title

   StartMenu.add(new JLabel(new ImageIcon("Data/StartMenu.png")));  
   StartMenu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   //Set Close Operation
   StartMenu.setSize(600, 330);
   StartMenu.setVisible(true);

   JPanel StartPanel = new JPanel();
   StartPanel.setLayout(null);  
   StartPanel.setVisible(true);
   ImageIcon Start = new ImageIcon("Data/StartButton.png");  //Play Button in Center
   JButton StartButton = new JButton(Start);  
   StartButton.setBounds(263,101,70,53);  //Positioned Button Over Image of Button

   StartButton.setVisible(true);
   StartButton.setEnabled(true);

   StartButton.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent StartButtonClicked){
         JOptionPane.showMessageDialog(null,"Test");
         MainMenu.MainMenu();
         StartMenu.dispose();
      }
   });
   StartPanel.add(StartButton);
   StartMenu.add(StartPanel);
       }
}

按钮未显示,文件路径正确,即使没有显示的图像也没有。它过去常常出现但由于某种原因停止工作,提前感谢您的帮助:(

1 个答案:

答案 0 :(得分:0)

您过早致电StartPanel.setVisible(true);。完成表单配置后,它必须是 last 语句。