简体   繁体   English

JButton 在鼠标悬停之前不可见

[英]JButton not visible until mouseover

I'm creating a gui for my project.我正在为我的项目创建一个 gui。 When the gui is first loaded only background is visible, so buttons are not visible, but when mouse over them, they are visible.当 gui 第一次加载时,只有背景是可见的,所以按钮是不可见的,但是当鼠标悬停在它们上时,它们是可见的。 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);


}   

}

Main Class主Class

public class MainClass {
public static Home pencere;

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

} }

  1. Don't paint on top-level containers like JFrame as they already carry the burden of painting all of it's components. 不要在像JFrame这样的顶级容器上绘画,因为它们已经承担了绘制所有组件的负担。

  2. Instead paint on JPanel or JComponent and Override it's paintComponent method. 而是在JPanelJComponentJComponentOverride它的paintComponent方法。

  3. 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. 除了重写paintComponent (或者在你的情况下是paint )之外,你还需要在方法内调用super.paintComponent (在你的情况下是super.paint )(在方法签名下首次调用),以免破坏绘制链。 Failing to do so may and probably will leave you with undesired paint artifacts. 如果不这样做可能会并且可能会给你留下不希望的油漆文物。

  4. 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. 而是使用布局管理器,让他们进行组件的布局和大小调整,因为它们是为Swing应用程序设计的。 Learn more at Laying out components Within a Container 了解有关在Container布置组件的更多信息

  5. Setting Home pancere as a static class member of MainClass is completely pointless. 设置Home pancere作为一个static的类成员MainClass是完全没有意义的。 Just declare and instantiate both in the main method. 只需在main方法中声明并实例化两者。

  6. Swing apps should be run on the Event Dispatch Thread (EDT). Swing应用程序应该在Event Dispatch Thread(EDT)上运行。 You can do so by wrapping your the code inside your main method with a SwingUtilities.invokeLater... . 您可以通过使用SwingUtilities.invokeLater...将代码包装在main方法中来实现。 See more at Initial Threads 初始线程中查看更多信息

  7. 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() . 不要试图使面板可见且不可见或添加删除面板,而是考虑使用将“分层”面板的CardLayout ,并且可以使用CardLayout的方法(如show()next()previous() See more at How to Use CardLayout 有关如何使用CardLayout的更多信息,请参阅

  8. 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. 当您将String传递给ImageIcon ,您告诉程序查看文件系统,这可能在您的开发环境中有效,但就是这样。 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. 请参阅嵌入式资源上的wiki标记,密切关注最后一个链接,如果信息没有提供足够的详细信息,将提供有关如何使用和加载嵌入资源的一些资源。

Problem is with 问题在于

 getContentPane().setLayout(null);

remove it as you have already set the layout to a Border Layout and you will see all these buttons. 删除它,因为您已经将布局设置为边框布局,您将看到所有这些按钮。

只要确保除了你想要显示的面板之外的所有其他面板的可设置性设置为假。我也有类似的问题,但我忘了将10个面板中的一个面板的可见性设置为false。问题解决了一旦我设置它是假的。

I don't know how this worked for me I just typed jf.setVisible(true);我不知道这对我有什么用,我只是输入jf.setVisible(true); at the end after adding all the GUI codes... `在添加所有 GUI 代码后的最后...`

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.setVisible(true); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

I dont know how to type a perfect reply 'cause I am replying for the first time in stackoverflow.. SORRY!!我不知道如何输入完美的回复,因为我是第一次在 stackoverflow 中回复.. 抱歉!!

Try putting validate(); 尝试使用validate(); method on your main frame. 主框架上的方法。 I think it would help you. 我认为这会对你有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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