簡體   English   中英

使用ActionListener清除JPanel中的圖形

[英]Clear graphics from JPanel with ActionListener

我的問題是,如何通過再次運行OtherPanel來使用我的動作偵聽器清除圖形並創建一組新的圖形?

public class MainFrame extends JFrame
  {

   private OtherPanel panel;

       public MainFrame()
   {

        panel = new OtherPanel();
       }

   class OtherPanel extends JPanel 
   {
      private OtherPanel()
      {
    ...

      }

      public void paintComponent(Graphics g) {
          super.paintComponent(g);

          Graphics2D g2d = (Graphics2D) g;            
              ....

          }

      private class ReloadListener implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {
           }
        }

    }
class OtherPanel extends JPanel 
{
    private boolean isReset;

    private OtherPanel()
    {
    ...
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        if(!isReset){
            //your painting code here
        }
    }

    public void setReset(boolean reset){
        isReset = reset;
    }

    private class ReloadListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            setReset(true);
            repaint();
        }

    }
}

一切都取決於“重置”面板應該如何。 我只是將super.paintComponent()作為默認外觀,你可能想要改變它。 當你想在面板上繪制某些東西時,不要忘記在你的代碼中添加setReset(false)

private class ReloadListener implements ActionListener
 {
    public void actionPerformed(ActionEvent e)
     {
       newPic();
       panel.updateUI();            

  }

   public MainFrame newPic()
   {

      return new MainFrame();
   }
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM