English 中文(简体)
JMenuBar disappears when I add paint method
原标题:

Im trying to use the following code to eventually make a game. The code, as shown below, works.

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());
 }
}

However, if you were to put a paint method in it, as shown below, it stops showing the 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());
 }
}

Im not even kidding. Please help

最佳回答

Component.paint is responsible for drawing the component. In this case, the entire JFrame.

By overriding paint(Graphics g), but not called super.paint(g) from within it, you ve stopped all rendering into the JFrame.

Just add super.paint(g) to your paint method and that should fix it.

问题回答

The components you ve added to the JFrame are themselves painted in the JFrame s paint method.

You want to call the base class s paint method from your own one, before doing any of your own painting.





相关问题
Java Updating Small Circles

I need to display a large number (500+) of small circles on a form to simulate LEDs. However, these circles need to be quite small, around 8 or 9 pixels diameter. So far, in my testing, I ve put ...

JMenuBar disappears when I add paint method

Im trying to use the following code to eventually make a game. The code, as shown below, works. import java.awt.event.*; import javax.swing.*; public class GUI extends JFrame implements ...

Mixing two RGB color vectors to get resultant

I am trying to mix two source RGB vectors to create a third "resultant vector" that is an intuitive mix of the first two. Ideally, I would be able to emulate "real paint mixing characteristics", but ...

Paint like Feature in Web.Application?

i want to implement Ms Paint like feature in my web application in c# asp.net. like cropping coloring zooming ,color picker etc . Please tell me is there any pre build tools or application for it.Any ...

DataGridView CellFormatting event preventing form painting

I am using C#, Winforms, and .Net 3.5 My form has a custom DataGridView (double-buffered to prevent flickering during my cellformatting events, as seen here). When I perform a database search, I bind ...

Delphi Form Painting Black Flash When Restoring

When I minimize and restore my Delphi application, the window contents flash a horrible black before (re)painting on Vista/Win7. This can also be seen with the Delphi 2007 IDE - the Object Inspector, ...

热门标签