繁体   English   中英

将JMenuBar和JPanel添加到JFrame

[英]Adding JMenuBar and JPanel to a JFrame

我在向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);
}

字段“ gui”是我的JPanel类:

@Override
public void paintComponent(Graphics g) {


}

这是我的重绘计时器:

public static java.awt.event.ActionListener repaint = new java.awt.event.ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        GUI.gui.repaint();
    }
};

这是输出:

您可以看到它重复了我的JMenuBar

看到它如何复制JMenuBar了吗? 它可以为绘制在该JPanel上的任何内容执行此操作。 有什么办法吗?

有什么办法吗?

是。 不要在paintComponent方法中调用Thread.sleep 实际上,不要在Swing应用程序中的任何地方调用Thread.sleep 对于周期性延迟,请使用Swing计时器

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM