简体   繁体   English

除了与mouselistener一起使用外,JLabel不会显示

[英]JLabel doesn't show up except when used with mouselistener

I'm working on java game and when i add JPanel with JLabel buttons to JFrame, buttons don't show up until i hover over them. 我正在开发Java游戏,当我将带有JLabel按钮的JPanel添加到JFrame时,直到我将鼠标悬停在它们上方时,按钮才会显示。 I tried removing mouselistener, but it just makes buttons invisible even if i hover over them. 我尝试删除mouselistener,但是即使我将鼠标悬停在它们上面,按钮也会变得不可见。 I also tried: validate(),revalidate(), changing order of adding components to Jframe. 我也尝试过:validate(),revalidate(),更改将组件添加到Jframe的顺序。

Game.java: Game.java:

    import javax.swing.*;
    public class Game {
    public static void main(String[] args) {
        JFrame ramka = new JFrame();
        ramka.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ramka.setTitle("Super Boxxy");
        ramka.setResizable(false);
        ramka.pack();
        ramka.setLocationRelativeTo(null);
        Menu window = new Menu(ramka);
        window.ZbudujMenu(ramka);
     }
    }

Menu.java: Menu.java:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import static java.lang.Thread.sleep;

public class Menu extends JLabel implements MouseListener {
public Menu(JFrame ramka){
   this.ramka=ramka;
}
//Title Screen
JFrame ramka;
JLabel startButton;
JLabel exitButton;
ImageIcon titleIcon;
JLabel backgroundImg;
JPanel menu;

public static final int WIDTH = 1024;
public static final int HEIGHT = 640;

ImageIcon exitIcon =new ImageIcon("resources/exit.png");
ImageIcon exitIconHover =new ImageIcon("resources/exit_hover.png");

ImageIcon startIcon =new ImageIcon("resources/start.png");
ImageIcon startIconHover =new ImageIcon("resources/start_hover.png");

public void ZbudujMenu(JFrame ramka) {
    //Start button
    startButton = new JLabel();
    startButton.setIcon(startIcon);
    startButton.addMouseListener(this);


    //Exit button
    exitButton=new JLabel();
    exitButton.setIcon(exitIcon);
    exitButton.addMouseListener(this);


    //Background
    titleIcon =new ImageIcon("resources/background.png");
    backgroundImg = new JLabel(titleIcon);
    backgroundImg.setBounds(0,0,WIDTH,HEIGHT);
    backgroundImg.setLayout(new FlowLayout());


    //Frame
    ramka.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ramka.setSize (WIDTH,HEIGHT);
    ramka.setTitle("Super Boxxy");

    menu = new JPanel(new GridLayout(2, 1, 8, 8));
    menu.add(startButton);
    menu.add(exitButton);
    menu.setBorder(BorderFactory.createEmptyBorder(353,370,54,370));

    ramka.add(backgroundImg);
    ramka.add(menu);
    ramka.setVisible(true);
    ramka.setLocationRelativeTo(null);


}
public void mouseClicked(MouseEvent akcja)
{
}
public void mouseEntered(MouseEvent akcja)
{
    if (akcja.getSource() == startButton)
    {
       startButton.setIcon(startIconHover);
    }
    else if (akcja.getSource() == exitButton)
    {
        exitButton.setIcon(exitIconHover);
    }
}
    public void mouseExited(MouseEvent akcja)
    {
       if (akcja.getSource() == startButton)
        {
            startButton.setIcon(startIcon);
        }
        else if (akcja.getSource() == exitButton)
        {
            exitButton.setIcon(exitIcon);
        }
    }
    public void mousePressed(MouseEvent akcja) {}
    public void mouseReleased(MouseEvent akcja) {}
}

Component should be added to the frame before the frame is made visible. 在使框架可见之前,应将组件添加到框架。

JFrame ramka = new JFrame();
ramka.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ramka.setTitle("Super Boxxy");
ramka.setResizable(false);
ramka.pack();

You create a frame and pack it without adding any components to the frame. 您创建框架并将其打包,而无需在框架中添加任何组件。 You invoke the pack() method AFTER adding components to the frame so all the components can be displayed at their preferred size. 在将组件添加到框架之后,您将调用pack()方法,以便所有组件都可以按其首选大小显示。

public class Menu extends JLabel implements MouseListener {  

Why are you extending JLabel? 为什么要扩展JLabel? You are not adding any new functionality to the label. 您没有在标签上添加任何新功能。

I would suggest your class should: 我建议您的班级应该:

  1. Extend JPanel so you can add all your components to the panel 扩展JPanel,以便可以将所有组件添加到面板中
  2. all the code in the ZbudujMenu(...) method would be moved to the constructor of the Menu() class. ZbudujMenu(...)方法中的所有代码都将移至Menu()类的构造函数。
  3. get rid of all the JFrame logic from the Menu class. 摆脱掉Menu类中的所有JFrame逻辑。

Then the code in main() method would look something like: 然后main()方法中的代码如下所示:

    JFrame ramka = new JFrame();
    ramka.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ramka.setTitle("Super Boxxy");
    ramka.add( new Menu() ):
    ramka.setResizable(false);
    ramka.pack();
    ramka.setLocationRelativeTo(null);
    ramka.setVisible( true );
    //Menu window = new Menu(ramka);
    //window.ZbudujMenu(ramka);

Read the Swing tutorial . 阅读Swing教程 There a plenty of demo programs that will show you how to create a GUI using this basic structure. 有很多演示程序,将向您展示如何使用此基本结构创建GUI。 The demos will also show you how to create your GUI on the Event Dispatch Thread (EDT) . 演示还将向您展示如何在Event Dispatch Thread (EDT)上创建GUI。 All Swing components should be created on the EDT . 所有Swing组件都应在EDT上创建。

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

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