[英]JLabel doesn't show up except when used with mouselistener
我正在開發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) {}
}
在使框架可見之前,應將組件添加到框架。
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? 您沒有在標簽上添加任何新功能。
我建議您的班級應該:
ZbudujMenu(...)
方法中的所有代碼都將移至Menu()類的構造函數。 然后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教程 。 有很多演示程序,將向您展示如何使用此基本結構創建GUI。 演示還將向您展示如何在Event Dispatch Thread (EDT)
上創建GUI。 所有Swing組件都應在EDT
上創建。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.