我在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