如何在图形顶部绘制 JMenuBar
我正在用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
直接绘制 JFrame 并不是一个好主意。扩展 JComponent、重写其 PaintComponent() 来执行自定义绘制并将其添加到 JFrame 可能会更简单。这样 JMenuBar 就可以在 JFrame 上运行,一切正常。
现在,您将 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.
Now you add an instance of DrawingSurface to the JFrame.