English 中文(简体)
How to draw a JMenuBar on top of a graphics
原标题:

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.

最佳回答

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.

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签