JLabel 不显示,除非与 mouselistener 一起使用

JLabel doesn't show up except when used with mouselistener

我正在开发 java 游戏,当我将带有 JLabel 按钮的 JPanel 添加到 JFrame 时,按钮不会显示,直到我将鼠标悬停在它们上面。我尝试删除 mouselistener,但它只会让按钮不可见,即使我将鼠标悬停在按钮上也是如此。 我也试过:validate(),revalidate(), changing order of adding components to Jframe.

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:

    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) {}
}

应在框架可见之前将组件添加到框架。

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

您创建一个框架并将其打包,但没有向框架添加任何组件。在将组件添加到框架后调用 pack() 方法,这样所有组件都可以按其首选大小显示。

public class Menu extends JLabel implements MouseListener {  

为什么要扩展 JLabel?您没有向标签添加任何新功能。

我建议您 class 应该:

  1. 扩展 JPanel,以便您可以将所有组件添加到面板
  2. ZbudujMenu(...) 方法中的所有代码都将移至 Menu() 的构造函数中 class。
  3. 从菜单中删除所有 JFrame 逻辑 class。

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

阅读Swing tutorial。有大量演示程序将向您展示如何使用此基本结构创建 GUI。这些演示还将向您展示如何在 Event Dispatch Thread (EDT) 上创建 GUI。所有 Swing 组件都应在 EDT.

上创建