2012-02-04 45 views
3

因此,我有一個JPanel對象作爲JFrame的組件,並且我定期用Timer對象重新繪製JPanel的內容。除了在JFrame菜單頂部重繪JPanel時,一切都工作正常,因此導致菜單項無法讀取。有沒有辦法解決這個問題,而不必每次用戶訪問菜單時都暫停計時器?Janel內的JFrame在JFrame的菜單頂部繪製

控制框架類

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

public class ControlFrame extends JFrame implements ActionListener{ 
    /*======Public Constants======*/ 
    public static int DEFAULT_HEIGHT = 400; 
    public static int DEFAULT_WIDTH = 400; 

    /*======Private Instance Variables======*/ 
    private AnimationPanel animPane; 
    private JMenu menu; 
    private JMenuItem menuExit; 
    private JMenuBar menuBar; 

    /*======Constructors======*/ 
    public ControlFrame(){ 
     initialize(); 
    } 

    /*======Public Instance Methods======*/ 
    public void actionPerformed(ActionEvent ae) { 
     if(ae.getActionCommand().equals("exit")){ 
       System.exit(0); 
     } 
    } 

    /*======Private Instance Methods======*/ 
    private void initialize(){ 
     this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); 

     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     this.setLayout(new GridLayout(0,2)); 

     this.animPane = new AnimationPanel(this.getWidth(), this.getHeight()); 

     this.add(animPane); 

     createCFMenu(); 

     this.setVisible(true); 
    } 

    private void createCFMenu(){ 
     this.menuBar = new JMenuBar(); 
     this.menu = new JMenu("File"); 
     this.menu.setMnemonic(KeyEvent.VK_F); 
     this.menuBar.add(this.menu); 

     this.menuExit = new JMenuItem("Exit", KeyEvent.VK_X); 
     this.menuExit.addActionListener(this); 
     this.menuExit.setActionCommand("exit"); 
     this.menu.add(menuExit); 

     this.setJMenuBar(this.menuBar); 
    } 

    /*======Main Method======*/ 
    public static void main(String[] args){ 
     ControlFrame cf = new ControlFrame(); 


    } 

} 

AnimationPanel類

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.awt.image.BufferedImage; 

public class AnimationPanel extends JPanel implements ActionListener{ 



    /*======Private Instance Variables======*/ 
    private int timeInterval; 
    private Timer animTimer; 

    /*======Constructor======*/ 
    public AnimationPanel(int width, int height){ 
     timeInterval = 50; 

     this.setSize(width, height); 

     this.animTimer = new Timer(timeInterval, this); 

     animTimer.start(); 
    } 


    public void actionPerformed(ActionEvent arg0) { 

     paint(); 
    } 

    /*======Private Instance Variables======*/ 
    private void paint(){ 
     BufferedImage bImage = new BufferedImage(this.getWidth(), 
      this.getHeight(), BufferedImage.TYPE_INT_RGB); 
     Graphics bg = bImage.getGraphics(); 

     bg.setColor(Color.WHITE); 
     bg.fillRect(0, 0, bImage.getWidth(), bImage.getHeight()); 

     this.getGraphics().drawImage(bImage, 0, 0, this); 
    } 
} 

問題是動畫面板是在ControlFrames菜單

+1

解決方案是修復您的代碼中的錯誤。如果您需要我們的幫助,您需要顯示該代碼,或者其中的一部分。要發佈的最佳代碼是創建一個[SSCCE](http://sscce.org),它是一個最小的可編譯和可運行程序,可以顯示您的問題,並且沒有與手頭問題無關的代碼。我不太清楚你如何在沒有這些重要信息的情況下猜測錯誤,但也許是因爲你是新來的。順便提一下,歡迎使用stackoverflow。 – 2012-02-04 04:30:32

+1

添加了代碼。 – pmurph 2012-02-04 04:53:06

+0

發佈代碼的好處。 +1 – 2012-02-04 05:05:53

回答

4

的頂部繪製不要調用Java代碼getGraphics()。 Java GUI必須重新畫圖,並且應該使用paint(Graphics)paintComponent(Graphics)這樣做。這就是菜單消失的原因。

該版本的AnimationPanel解決了該錯誤。

class AnimationPanel extends JPanel implements ActionListener{ 
    /*======Private Instance Variables======*/ 
    private int timeInterval; 
    private Timer animTimer; 

    /*======Constructor======*/ 
    public AnimationPanel(int width, int height){ 
     timeInterval = 50; 
     this.setSize(width, height); 
     this.animTimer = new Timer(timeInterval, this); 
     animTimer.start(); 
    } 

    public void actionPerformed(ActionEvent arg0) { 
     repaint(); 
    } 

    /*======Private Instance Variables======*/ 
    public void paintComponent(Graphics g){ 
     // important to get the component to paint itself & borders etc. 
     super.paintComponent(g); 
     BufferedImage bImage = new BufferedImage(this.getWidth(), 
      this.getHeight(), BufferedImage.TYPE_INT_RGB); 
     Graphics bg = bImage.getGraphics(); 

     bg.setColor(Color.WHITE); 
     bg.fillRect(0, 0, bImage.getWidth(), bImage.getHeight()); 
     bg.dispose(); // Assist the garbage collector! 

     g.drawImage(bImage, 0, 0, this); 
    } 
} 
+0

打敗我吧! 1+ – 2012-02-04 05:04:53

+0

修復了這個問題,感謝幫助 – pmurph 2012-02-04 05:12:07

4

您的代碼的一個問題是您繪製的所有錯誤。幾乎從不使用組件的getGraphics來獲取其Graphics對象,因爲如果有任何重繪,此對象將不會保留。相反,繪圖應該在您的JPanel的paintComponent方法中被動地執行。

編輯:安德魯在他的更快的帖子顯示! 1+給他!

但是,如果您從本練習中得到任何東西,那就需要通過Java Swing圖形教程來學習如何繪製Swing,因爲您需要拋出一些錯誤的假設,即您(以及所有我們)已經開始做這種編碼了。

+0

對本教程的建議+1,但主要是爲了提示'郵政編碼'。 :) – 2012-02-04 05:08:28