如何在图形顶部绘制 JMenuBar

发布于 2024-11-15 20:42:45 字数 1169 浏览 4 评论 0原文

我正在用 Java 编写一个基本的计时器程序,在该程序中我希望在顶部有一个菜单栏。我现在拥有的代码是:

public Main() {
    JMenuBar menubar = new JMenuBar();

    setJMenuBar(menubar);

    JMenu menu = new JMenu("Test");
    menubar.add(menu);

    JMenuItem menuitem = new JMenuItem("Item");
    menu.add(menuitem);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(500, 500);
    setTitle(Namer.name);
    new Timer(delay, timer).start();
    new Timer(1, new Refresher()).start();
    setResizable(false);
    setVisible(true);
}

public void paint(Graphics g) {
    Graphics buffer = unscreen.getGraphics();
    buffer.setColor(Color.white);
    buffer.fillRect(0, 0, 500, 500);
    buffer.setColor(Color.black);
    buffer.setFont(new Font("Times New Roman", Font.PLAIN, 25));
    // buffer.drawString("hours:minutes:seconds: ", 25, 100);
    buffer.drawString(hourss + numhours + ":" + minutess + numminutes + ":"
            + secondss + numseconds, 100, 100);
    g.drawImage(unscreen, 0, 0, null);
}

当我运行此代码时,我得到了我所期望的一切,其中是一些显示程序运行了多长时间的数字以及屏幕顶部的菜单栏(菜单栏除外)。我尝试过注释掉绘制方法,当我这样做时它就起作用了。有没有更好的方法来完成我正在做的事情,或者有不同的解决方案来解决我的问题?另外,如果有更好的方法在窗口上打印内容,我不需要在那里使用绘制方法。

I am writing a basic timer program with Java, and in the program I would like to have a menu bar at the top. The code I have as of now is:

public Main() {
    JMenuBar menubar = new JMenuBar();

    setJMenuBar(menubar);

    JMenu menu = new JMenu("Test");
    menubar.add(menu);

    JMenuItem menuitem = new JMenuItem("Item");
    menu.add(menuitem);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(500, 500);
    setTitle(Namer.name);
    new Timer(delay, timer).start();
    new Timer(1, new Refresher()).start();
    setResizable(false);
    setVisible(true);
}

public void paint(Graphics g) {
    Graphics buffer = unscreen.getGraphics();
    buffer.setColor(Color.white);
    buffer.fillRect(0, 0, 500, 500);
    buffer.setColor(Color.black);
    buffer.setFont(new Font("Times New Roman", Font.PLAIN, 25));
    // buffer.drawString("hours:minutes:seconds: ", 25, 100);
    buffer.drawString(hourss + numhours + ":" + minutess + numminutes + ":"
            + secondss + numseconds, 100, 100);
    g.drawImage(unscreen, 0, 0, null);
}

When I run this code, I get everything I would expect, which is some numbers showing how long the program has been up and a menu bar at the top of the screen, except the menu bar. I have tried commenting out the paint method, and when i do that it works. Is there a better way to do what I am doing or a different solution to my problem? Also, I don't need to have the paint method there if there is a better way to print stuff on the window.

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

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

发布评论

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

评论(1

狂之美人 2024-11-22 20:42:45

直接绘制 JFrame 并不是一个好主意。扩展 JComponent、重写其 PaintComponent() 来执行自定义绘制并将其添加到 JFrame 可能会更简单。这样 JMenuBar 就可以在 JFrame 上运行,一切正常。

   public class DrawingSurface extends JComponent{

          public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics buffer = unscreen.getGraphics();
            buffer.setColor(Color.white);
            buffer.fillRect(0, 0, 500, 500);
            buffer.setColor(Color.black);
            buffer.setFont(new Font("Times New Roman", Font.PLAIN, 25));
            // buffer.drawString("hours:minutes:seconds: ", 25, 100);
            buffer.drawString(hourss + numhours + ":" + minutess + numminutes + ":"
             + secondss + numseconds, 100, 100);
            g.drawImage(unscreen, 0, 0, null);
         }
   }

现在,您将 DrawingSurface 的实例添加到 JFrame 中。

It is not a good idea to paint the JFrame directly. It may be less troublesome to extend a JComponent, override its paintComponent() to do your custom painting, and add it to the JFrame. This way the JMenuBar goes on the JFrame and everything works fine.

   public class DrawingSurface extends JComponent{

          public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics buffer = unscreen.getGraphics();
            buffer.setColor(Color.white);
            buffer.fillRect(0, 0, 500, 500);
            buffer.setColor(Color.black);
            buffer.setFont(new Font("Times New Roman", Font.PLAIN, 25));
            // buffer.drawString("hours:minutes:seconds: ", 25, 100);
            buffer.drawString(hourss + numhours + ":" + minutess + numminutes + ":"
             + secondss + numseconds, 100, 100);
            g.drawImage(unscreen, 0, 0, null);
         }
   }

Now you add an instance of DrawingSurface to the JFrame.

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