我在JTabbedPane中绘制JPanel时很难理解和解决问题 基本上我有这个小应用程序绘制统计图形,它带有JTabbedPane的简单JFrame在里面。 现在,JTabbedPane有2个选项卡,每个选项卡包含一个JPanel(扩展),一个javax.swing.Timer在按下启动按钮后启动,并在一个JPanel上绘制图形,每隔一个新行,到目前为止这么好。 如果当计时器正在运行并在面板上绘图时,我选择另一个选项卡(它仍然是空的),我看到drawString方法开始在选定的面板上绘图,该面板不包含任何调用drawString方法, 粘贴相关代码:Swing问题,在JTabbedPane上绘图
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方法的功能,因为它的长和不相关的,都是由束带 这里空面板,该面板在设置好的完成标签窗格的第二个标签,因为您可以看到它没有任何内容
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