Java 为什么我的JMenuBar没有激活?(单击时闪烁,但不会完全显示)
我正在构建一个JFrame,它在顶部实现一个JMenuBar。帧的其余部分是一个JComponent,其中包含一个通过双缓冲光栅图像在屏幕上移动的动画球。每当我单击菜单项时,它们都会闪烁,但不会完全显示。我的JComponent使用paintComponent绘图,它是否出于某种原因覆盖了我的菜单?我创建单独JComponent的全部原因是为了避免元素之间的冲突。下面是我的JFrame和JComponent的代码 我的JComponent使用paintComponent绘图,它是否出于某种原因覆盖了我的菜单 您没有使用paintComponent方法,这可能就是问题所在。您使用G而不是C创建了自己的方法,然后尝试使用图形,而不是通常传递到paintComponent方法中的图形 不要喜欢你的代码。只需将绘制代码放在paintComponent方法中,并使用其图形对象 在绘制代码中没有while循环,只需使用Swing计时器来安排动画 有关更多信息和示例,请阅读上Swing教程的部分Java 为什么我的JMenuBar没有激活?(单击时闪烁,但不会完全显示),java,swing,jcomponent,jmenubar,Java,Swing,Jcomponent,Jmenubar,我正在构建一个JFrame,它在顶部实现一个JMenuBar。帧的其余部分是一个JComponent,其中包含一个通过双缓冲光栅图像在屏幕上移动的动画球。每当我单击菜单项时,它们都会闪烁,但不会完全显示。我的JComponent使用paintComponent绘图,它是否出于某种原因覆盖了我的菜单?我创建单独JComponent的全部原因是为了避免元素之间的冲突。下面是我的JFrame和JComponent的代码 我的JComponent使用paintComponent绘图,它是否出于某种原因覆
public class Driver
{
public static void main(String[] args)
{
// Creates Game window.
MyFrame myFrame = new MyFrame();
DrawingSurface drawingSurface = new DrawingSurface();
myFrame.add(drawingSurface);
drawingSurface.setup();
myFrame.makeMenu();
drawingSurface.paintGomponent();
}
}
class MyFrame extends JFrame
{
/**
* Default serial version of long defined to suppress serial warning.
*/
private static final long serialVersionUID = 1L;
public MyFrame()
{
setTitle("Breakout");
setSize(800,600);
setResizable(false);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setLocationRelativeTo(null); // Sets location to center of screen (stackoverflow.com)
setVisible(true);
}
public void makeMenu()
{
JMenuBar menuBar = new JMenuBar();
// Adds menu "Game" with sub-menu "New", "Pause" and "Exit".
JMenu game = new JMenu("Game");
JMenuItem newGame = new JMenuItem("New");
newGame.setToolTipText("Starts a new game");
JMenuItem pauseGame = new JMenuItem("Pause");
pauseGame.setToolTipText("Pauses the game");
JMenuItem exitGame = new JMenuItem("Exit");
exitGame.setToolTipText("Exits the game");
// Adds menu "High Score".
JMenu highScores = new JMenu("High Scores");
highScores.setToolTipText("Displays high scores");
// Adds menus to menu bar and sets menu bar to frame.
game.add(newGame);
game.add(pauseGame);
game.add(exitGame);
menuBar.add(game);
menuBar.add(highScores);
setJMenuBar(menuBar);
}
}
class DrawingSurface extends JComponent
{
private int XResolution = 800;
private int YResolution = 600;
private Image raster;
private Graphics rasterGraphics;
public void paintGomponent()
{
super.paintComponent(rasterGraphics);
// Player ball used in game.
Ball ball = new Ball(400, 300, 2.0f, 1.0f);
while(true)
{
// Time for use with sleep, to make game run more smoothly.
long time = System.currentTimeMillis();
drawBackground();
ball.moveBall(ball);
ball.drawBall(rasterGraphics);
// Draws buffered raster graphics to frame.
getGraphics().drawImage(raster, 0, 0, XResolution, YResolution, null);
long changeInTime = System.currentTimeMillis() - time;
try{Thread.sleep(10-changeInTime);}catch(Exception e){}
}
}
private void drawBackground()
{
rasterGraphics.setColor(Color.black);
rasterGraphics.fillRect(0, 0, XResolution, YResolution);
}
public void setup()
{
raster = createImage(XResolution, YResolution);
rasterGraphics = raster.getGraphics();
}
}