簡體   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