JButton在没有鼠标悬停时不可见

5

我正在为我的项目创建一个GUI界面。当GUI首次加载时,只有背景是可见的,因此按钮是不可见的,但当鼠标悬停在它们上面时,它们就会变得可见。如何解决这个问题?

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);


}   

}

主类
public class MainClass {
public static Home pencere;

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

}


1
不要覆盖像JFrame这样的顶级容器的paint方法,而是使用自定义组件如JPanel并使用它的paintComponent方法。确保在进行任何自定义绘制之前调用super.paintComponent,然后将其应用为框架的contentPane - MadProgrammer
5个回答

9
  1. 不要在顶级容器(如JFrame)上绘制,因为它们已经承担了绘制所有组件的负担。

  2. 相反,在JPanelJComponent上绘制,并覆盖它的paintComponent方法。

  3. 除了重写paintComponent(或在您的情况下是paint)之外,您还需要在方法内部调用super.paintComponent(在方法签名下的第一个调用),以避免破坏绘制链。如果未能这样做,可能会留下不希望的绘制图案。

  4. 避免使用null布局,原因有很多。不同的平台将以不同的方式处理它们。它们难以维护,等等。相反,使用布局管理器,让它们对Swing应用程序进行布局和组件大小调整。详见Laying out components Within a Container

  5. Home pancere设置为MainClassstatic类成员完全没有意义。只需在main方法中声明和实例化即可。

  6. Swing应用程序应该在事件调度线程(EDT)上运行。您可以通过将代码包装在SwingUtilities.invokeLater...中来实现。详见Initial Threads

  7. 不要尝试使面板可见或不可见,也不要添加或删除面板,而是考虑使用CardLayout,它将"层叠"面板,并且您可以使用CardLayout的方法(如show()next()previous())导航它们。详见How to Use CardLayout

  8. 在部署时,您使用的图像将需要成为嵌入式资源,并且应从类路径加载,而不是从文件系统加载。当您向ImageIcon传递一个字符串时,您告诉程序在文件系统中查找,这可能在开发环境中有效,但仅此而已。请参阅关于embedded-resource的维基标签,并密切关注最后一个链接,如果信息不提供足够的详细信息,它将为您提供有关如何使用和加载嵌入式资源的一些资源。


0

我不知道为什么这对我起作用,我只是在添加所有GUI代码后,在结尾处键入了jf.setVisible(true);

public Calculator(){
    
    jf = new JFrame("Basic Calculator");
    jf.setLayout(GridBagLayout);
    jf.setSize(306, 550);
    jf.setLocation(530, 109);
    //all the GUI things like JButton, JLabel, etc...
    jf.setVisible(true);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

0

问题出在

 getContentPane().setLayout(null);

将其删除,因为您已经将布局设置为边框布局,这样您就可以看到所有这些按钮。


我试过了,它是可以工作的。你还改了其他什么东西吗? - Sanjeev

0

请确保除了您想要显示的面板之外,所有其他面板的setvisibility都设置为false。我也遇到过类似的问题,但我忘记将其中一个10个面板的可见性设置为false。一旦我将其设置为false,问题就得到解决。


-2
尝试在您的主框架上放置validate();方法。我认为这会对您有所帮助。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接