鼠标悬停之前,JButton不可见 [英] JButton not visible until mouseover

查看:61
本文介绍了鼠标悬停之前,JButton不可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的项目创建GUI.第一次加载gui时,仅可见背景,因此按钮不可见,但是将鼠标悬停在它们上时,它们是可见的.解决这个问题有什么办法?

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


}   

}

主类

public class MainClass {
public static Home pencere;

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

}

推荐答案

  1. 请勿在顶级容器(例如JFrame)上绘画,因为它们已经承担了绘画其所有组件的负担.

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

代替在JPanelJComponentOverride上绘画,这是paintComponent方法.

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

在覆盖paintComponent(或您的情况下为paint)的顶部,您还需要在方法内部调用super.paintComponent(在您的情况下为super.paint)(方法签名下的第一次调用) ),以免破坏油漆链.否则,可能会留下不希望的油漆瑕疵.

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.

出于多种原因,请避免使用空布局.不同的平台将对它们进行不同的处理.除其他许多原因外,它们很难维护.而是使用布局管理器,让他们像设计用于Swing应用程序一样对组件进行布局和调整大小.在在容器内布置组件

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

Home pancere设置为MainClassstatic类成员是完全没有意义的.只需在main方法中进行声明和实例化.

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

Swing应用程序应在事件调度线程(EDT)上运行.您可以通过使用SwingUtilities.invokeLater...将代码包装在main方法中来实现.有关更多信息,请参见初始线程

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

与其尝试使面板不可见或不可见或添加可移除面板,不如考虑使用CardLayout,它将对面板进行层化" ,而您可以使用CardLayout的方法,例如show()next()previous().有关更多信息,请参见如何使用CardLayout

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

在部署之前,正在使用的映像将需要成为嵌入式资源,并且应该从类路径而不是从文件系统加载.当将String传递给ImageIcon时,就是在告诉程序在文件系统中查找,这可能在您的开发环境中可用,仅此而已.请参阅 embedded-resource 上的Wiki标签,请密切注意最后一个链接,该链接将为您提供一些资源.有关信息未提供足够详细信息的情况下如何使用和加载嵌入式资源的说明.

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.

这篇关于鼠标悬停之前,JButton不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆