【问题标题】:Swing issue, drawing on a JTabbedPaneSwing 问题,在 JTabbedPane 上绘图
【发布时间】:2011-11-27 20:18:40
【问题描述】:

在 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());
}

}

希望你能给点建议,谢谢

【问题讨论】:

    标签: swing jpanel jtabbedpane drawstring


    【解决方案1】:

    你的问题确实很奇怪。从上面的代码来看,一切似乎都很好。不过,你能测试一下吗?由于绘图只发生在扩展 JPanel 的 Monitor 类内部,因此不要将 Graphics 对象扔掉,而是尝试使用 Graphics g = getGraphics();在“analize()”方法中获取 Graphics 对象。 我不确定它是否能解决任何问题,但是让一个班级向另一个班级提供一些已经拥有的东西似乎很奇怪。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-21
      • 2021-09-10
      • 1970-01-01
      • 2011-11-25
      • 2019-04-04
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多