当我添加paint方法时,JMenuBar消失了

时间:2009-12-18 03:57:28

标签: jframe paint jmenubar

我试图使用以下代码最终制作游戏。如下所示,代码可以正常工作。

import java.awt.event.*;
import javax.swing.*;



public class GUI extends JFrame implements ActionListener
{
 public static void main(String[] args)
 {
  GUI g = new GUI();
 }
 public GUI()
    {
  try
  {
   UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  } catch (Exception e)
  {
   e.printStackTrace();
  } 
  setSize(500,500);
  setDefaultCloseOperation(EXIT_ON_CLOSE);
  setJMenuBar(createMenuBar());
  setVisible(true);
    }
 public JMenuBar createMenuBar()
    {
  JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("File");
        fileMenu.setMnemonic(KeyEvent.VK_F);
        JMenuItem save = new JMenuItem("Save");
        save.setMnemonic(KeyEvent.VK_S);
        save.addActionListener(this);
        JMenuItem load = new JMenuItem("Load");
        load.setMnemonic(KeyEvent.VK_L);
        load.addActionListener(this);
        JMenuItem quit = new JMenuItem("Quit");
        quit.setMnemonic(KeyEvent.VK_Q);
        quit.addActionListener(this);
        fileMenu.add(save);
        fileMenu.add(load);
        fileMenu.addSeparator();
        fileMenu.add(quit);
        JMenu editMenu = new JMenu("Edit");
        editMenu.setMnemonic(KeyEvent.VK_E);
        JMenuItem undo = new JMenuItem("Undo");
        undo.setMnemonic(KeyEvent.VK_U);
        undo.addActionListener(this);
        JMenuItem redo = new JMenuItem("Redo");
        redo.setMnemonic(KeyEvent.VK_R);
        redo.addActionListener(this);
        editMenu.add(undo);
        editMenu.add(redo);
        JMenu helpMenu = new JMenu("Help");
        helpMenu.setMnemonic(KeyEvent.VK_H);
        JMenuItem controls = new JMenuItem("Controls");
        controls.setMnemonic(KeyEvent.VK_C);
        controls.addActionListener(this);
        JMenuItem about = new JMenuItem("About");
        about.setMnemonic(KeyEvent.VK_A);
        about.addActionListener(this);
        helpMenu.add(controls);
        helpMenu.addSeparator();
        helpMenu.add(about);
        menuBar.add(fileMenu);
        menuBar.add(editMenu);
        menuBar.add(helpMenu);
        return menuBar;
    }
 public void actionPerformed(ActionEvent e)
 {
  System.out.println(e.getActionCommand());
 }
}

但是,如果您要在其中放置绘画方法,如下所示,它将停止显示JMenuBar。

import java.awt.event.*;
import javax.swing.*;



public class GUI extends JFrame implements ActionListener
{
 public static void main(String[] args)
 {
  GUI g = new GUI();
 }
 public GUI()
    {
  try
  {
   UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  } catch (Exception e)
  {
   e.printStackTrace();
  } 
  setSize(500,500);
  setDefaultCloseOperation(EXIT_ON_CLOSE);
  setJMenuBar(createMenuBar());
  setVisible(true);
    }
 public JMenuBar createMenuBar()
    {
  JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("File");
        fileMenu.setMnemonic(KeyEvent.VK_F);
        JMenuItem save = new JMenuItem("Save");
        save.setMnemonic(KeyEvent.VK_S);
        save.addActionListener(this);
        JMenuItem load = new JMenuItem("Load");
        load.setMnemonic(KeyEvent.VK_L);
        load.addActionListener(this);
        JMenuItem quit = new JMenuItem("Quit");
        quit.setMnemonic(KeyEvent.VK_Q);
        quit.addActionListener(this);
        fileMenu.add(save);
        fileMenu.add(load);
        fileMenu.addSeparator();
        fileMenu.add(quit);
        JMenu editMenu = new JMenu("Edit");
        editMenu.setMnemonic(KeyEvent.VK_E);
        JMenuItem undo = new JMenuItem("Undo");
        undo.setMnemonic(KeyEvent.VK_U);
        undo.addActionListener(this);
        JMenuItem redo = new JMenuItem("Redo");
        redo.setMnemonic(KeyEvent.VK_R);
        redo.addActionListener(this);
        editMenu.add(undo);
        editMenu.add(redo);
        JMenu helpMenu = new JMenu("Help");
        helpMenu.setMnemonic(KeyEvent.VK_H);
        JMenuItem controls = new JMenuItem("Controls");
        controls.setMnemonic(KeyEvent.VK_C);
        controls.addActionListener(this);
        JMenuItem about = new JMenuItem("About");
        about.setMnemonic(KeyEvent.VK_A);
        about.addActionListener(this);
        helpMenu.add(controls);
        helpMenu.addSeparator();
        helpMenu.add(about);
        menuBar.add(fileMenu);
        menuBar.add(editMenu);
        menuBar.add(helpMenu);
        return menuBar;
    }
    public void paint(Graphics g)
    {
    }
 public void actionPerformed(ActionEvent e)
 {
  System.out.println(e.getActionCommand());
 }
}
我甚至不开玩笑。请帮忙

2 个答案:

答案 0 :(得分:3)

Component.paint负责绘制组件。在这种情况下,整个JFrame。

通过覆盖paint(Graphics g)但未在其中调用super.paint(g),您已停止所有渲染到JFrame中。

只需将super.paint(g)添加到您的paint方法中即可修复它。

答案 1 :(得分:0)

您添加到JFrame的组件本身是在JFrame的绘制方法中绘制的。

在进行任何自己的绘画之前,您想要从您自己的绘制方法中调用基类的绘制方法。