当我添加paint方法时JMenuBar消失

发布于 2024-08-15 20:24:35 字数 4594 浏览 9 评论 0原文

我尝试使用以下代码最终制作游戏。代码如下所示,可以运行。

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

我什至没有开玩笑。请帮忙

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

海螺姑娘 2024-08-22 20:24:35

组件.paint 负责绘制组件。在本例中,是整个 JFrame。

通过重写 paint(Graphics g),但不从其中调用 super.paint(g),您已经停止了 JFrame 中的所有渲染。

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

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.

三生路 2024-08-22 20:24:35

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文