在 JTabbedPane 内的 JPanel 上绘图时,我很难理解和解决问题 基本上我有一个绘制统计图形的小应用程序,它是一个简单的 JFrame,其中包含 JTabbedPane。现在,JTabbedPane 有 2 个选项卡,每个选项卡都包含一个 JPanel(扩展),一个 javax.swing.Timer 在按下开始按钮后启动,并在一个 JPanel 上绘制图形,每秒换行,到目前为止一切顺利。如果在计时器运行并在面板上绘图时,我选择了另一个选项卡(它仍然是空的),我看到 drawString 方法开始在所选面板上绘图,其中不包含对 drawString 方法的任何调用,我将粘贴相关代码:
public class Monitor extends JPanel{
**private Timer timer=new Timer(1000,new PerformanceEvent(this));**
public Monitor(){
this.setBackground(Color.BLACK);
this.setPreferredSize(new Dimension(400,211));
this.setBounds(new Rectangle(16,44,400,211));
this.setVisible(true);
}
/**
* This method contains the Graphoc tool to draw on the panel
* @param g
*/
public void analize(Graphics g){
if((ammountOfTimesAnalizeCalled / 10)==1){
g.setColor(Color.red);
g.drawLine(left1, high1, left2, high2);
//print high2 variable on file
Performance.report(high2);
prepareForNext();
ammountOfTimesAnalizeCalled++;
System.out.println(ammountOfTimesAnalizeCalled);
}
/**
* This method is used by the GUI to start the timer<br/>
* The timer starts and will calls analize
*/
public void start(){
timer.start();
}
/**
* This method stops the TimerTask
*/
public void stop(){
timer.stop();
}
}
我没有报告 analize 方法的功能,因为它很长而且不相关,所有都由 drawString 完成 这里是空面板,它设置在 jtabbed 窗格的第二个选项卡中,你可以看到它什么也没做
public class Informations extends JPanel{
/**
* The constructor initializes the panel's appearence
*/
public Informations(){
this.setBackground(Color.BLACK);
this.setPreferredSize(new Dimension(400,211));
this.setBounds(new Rectangle(16,44,400,211));
this.setVisible(true);
}
}
JTabbedPane
public class MyPane extends JTabbedPane{
private Monitor monitor=new Monitor();
private Informations informations=new Informations();
public MyPane(){
monitor.setBounds(new Rectangle(5, 5, 400, 211));
this.setPreferredSize(new Dimension(420,250));
this.setBounds(new Rectangle(16,44,420,250));
this.setVisible(true);
this.addTab("Performance", monitor);
this.addTab("Informations", informations);
}
}
这是计时器中的事件处理程序:
public class PerformanceEvent implements ActionListener{
private Monitor monitor=null;
public PerformanceEvent(Monitor monitor){
this.monitor=monitor;
}
/**
* Perform the drawing action
*/
public void actionPerformed(ActionEvent event) {
monitor.analize(monitor.getGraphics());
}
}
希望大家给点建议,thx