我有一个JPanel对象作为JFrame组件,并使用Timer对象定期重新绘制JPanel的内容。除了JPanel在重绘时会覆盖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();
}
}
动画面板类
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);
}
}
问题是动画面板正在覆盖控制框架菜单。