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

问题描述 投票:0回答:1

我正在研究java游戏,当我将带有JLabel按钮的JPanel添加到JFrame时,按钮不会显示,直到我将鼠标悬停在它们上面。我尝试删除mouselistener,但它只是让按钮隐形,即使我将鼠标悬停在它们上面。我还尝试过:validate(),revalidate(),更改向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) {}
}
java jframe jlabel
1个回答
0
投票

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

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?您没有为标签添加任何新功能。

我建议你的班级应该:

  1. 扩展JPanel,以便您可以将所有组件添加到面板中
  2. ZbudujMenu(...)方法中的所有代码都将被移动到Menu()类的构造函数中。
  3. 摆脱Menu类中的所有JFrame逻辑。

然后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。应在EDT上创建所有Swing组件。

© www.soinside.com 2019 - 2024. All rights reserved.