[英]Adding JMenuBar and JPanel to a JFrame
I am having some trouble with adding a JMenuBar and a JPanel to a JFrame, here is my Main Method: 我在向JFrame添加JMenuBar和JPanel时遇到了一些麻烦,这是我的主要方法:
public static Timer timer = new Timer(100, ActionListener.repaint);
public static void main(String[] args) {
Insets in;
frame.setSize(600, 500);
frame.pack();
in = frame.getInsets();
frame.setSize(600 + (in.left + in.right) - 10, 500 + (in.top + in.bottom) - 10);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
add.addActionListener(new ActionListener());
frame.setJMenuBar(menuBar);
menuBar.add(file);
file.add(add);
file.add(sub);
frame.add(gui);
timer.start();
frame.setVisible(true);
}
The field 'gui' is my JPanel class: 字段“ gui”是我的JPanel类:
@Override
public void paintComponent(Graphics g) {
}
Here is my repaint Timer: 这是我的重绘计时器:
public static java.awt.event.ActionListener repaint = new java.awt.event.ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
GUI.gui.repaint();
}
};
here is the output: 这是输出:
See how it duplicated the JMenuBar? 看到它如何复制JMenuBar了吗? It does that for anything painted onto that JPanel.
它可以为绘制在该JPanel上的任何内容执行此操作。 Any solutions?
有什么办法吗?
Any solutions?
有什么办法吗?
Yes. 是。 Dont call
Thread.sleep
in the paintComponent
method. 不要在
paintComponent
方法中调用Thread.sleep
。 In fact dont call Thread.sleep
anywhere in a Swing application. 实际上,不要在Swing应用程序中的任何地方调用
Thread.sleep
。 For periodic delays use a Swing Timer 对于周期性延迟,请使用Swing计时器
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.