2012-02-27 164 views
1

我需要一个听众,如果静态布尔值已经改变,这样我可以重绘一帧上的组件,将不断检查。有人能帮助我我真的不太了解听众,并且没有和他们一起工作太多吗?帮助将不胜感激。布尔值变化监听的Java

编辑(更清晰):我有两个单独的类,其中在类是“主帧”第二类是一个JLabel的扩展并实现MouseListner用于“可点击照片”。 “主帧”创建照片并当光被点击“主帧”应该画在面板上的照片的描述的实例。这是“主框架”

MenuBar menuBar; 
static AnnotationVisual a; 
Picture pic; 
Picture pic2; 

GalleryScreen(int rows, int columns){ 
    this.setBorder(BorderFactory.createEmptyBorder(500,500,0,0)); 
    pic = new Picture("pic1", "Z:/My Documents/Downloads/Ball.jpg", new Coordinate(0,0)); 
    pic2 = new Picture("pic2", "Z:/My Documents/Downloads/hoop.jpg" , new Coordinate(1,0)); 

    this.add(pic); 
    this.add(pic2); 
    a = new AnnotationVisual(); 
} 

public void paintComponent(Graphics g){ 
    super.paintComponent(g); 
    if(a.shouldAnnotate()){ 
     FontMetrics size= g.getFontMetrics(); 
     if(getWidth()>=(a.dispX()+size.stringWidth(a.annotationText()))){ 
      g.setColor(Color.white); 
      g.fillRect(a.dispX()-3,a.dispY()-12,size.stringWidth(a.annotationText())+5,15); 
      g.setColor(Color.black); 
      g.drawRect(a.dispX()-3,a.dispY()-12,size.stringWidth(a.annotationText())+5,15); 
      g.drawString(a.annotationText(), a.dispX(), a.dispY()); 
     }else{ 
      String sub=""; 
      int letters=0; 
      g.setColor(Color.white); 
      g.fillRect(a.dispX()-3,a.dispY()-12,getWidth(),15); 
      g.setColor(Color.black); 

      for(int i=0;i<a.annotationText().length();i++){ 
       if(a.dispX()+letters+16<=getWidth()){ 
        sub+=a.annotationText().substring(i,i+1); 
        letters=size.stringWidth(sub); 
       }else{ 
        sub=sub+"..."; 
        i=a.annotationText().length(); 
       } 
      } 
      g.drawRect(a.dispX()-3,a.dispY()-12,size.stringWidth(sub)+3,15); 
      g.drawString(sub,a.dispX(),a.dispY()); 
     } 
    } 
} 

public static AnnotationVisual getA() 
{ 
    return a; 
} 

这是“可点击图片”

import java.awt.Dimension; 
import java.awt.Toolkit; 
import java.awt.event.*; 
import javax.swing.*; 

public class Picture extends JLabel implements MouseListener 
{ 
    String myAnnotation; 
    String filePath; 
    Coordinate imageCoord; 
    private boolean wasDoubleClick; 
    private Timer timer; 
    EditAnnotation newEdit; 
    AnnotationVisual newVisual; 

    public Picture(String annotation, String filePath, Coordinate coord) 
    { 
     super(new ImageIcon(filePath)); 
     this.addMouseListener(this); 
     myAnnotation=annotation; 
     this.filePath = filePath; 
     imageCoord = coord; 
     newEdit = new EditAnnotation(annotation); 
     newVisual = new AnnotationVisual(); 
    } 
    public Picture(String filePath) 
    { 
     super(new ImageIcon(filePath)); 
     this.addMouseListener(this); 
     this.filePath = filePath; 
     newEdit = new EditAnnotation(); 
     newVisual = new AnnotationVisual(); 
    } 
    public String getAnnotation() 
    { 
     return myAnnotation; 
    } 
    public AnnotationVisual getAnnotationVisual() 
    { 
     return newVisual; 
    } 
    public void setAnnotation(String annotation) 
    { 
     myAnnotation=annotation; 
    } 
    public Coordinate getCoordinate() 
    { 
     return imageCoord; 
    } 
    public void setCoordinate(Coordinate coord) 
    { 
     imageCoord = coord; 
    } 
    public Dimension getSize() 
    { 
     return new Dimension(super.getIcon().getIconWidth(), super.getIcon().getIconHeight()); 
    } 
    public void mouseClicked(MouseEvent e) 
    { 
     final int scrLocX = (int)e.getLocationOnScreen().getX(); 
     final int scrLocY = (int)e.getLocationOnScreen().getY(); 

     if (e.getClickCount() == 2) 
     { 
      wasDoubleClick = true; 
     } 
     else if(e.getClickCount() == 1) 
     { 
      Integer timerinterval = (Integer) Toolkit.getDefaultToolkit().getDesktopProperty("awt.multiClickInterval"); 

      timer = new Timer(timerinterval.intValue(), new ActionListener() 
      { 
       public void actionPerformed(ActionEvent evt) 
       { 
        if (wasDoubleClick) 
        { 
         GalleryScreen.getA().deleteAnnotation(); 
         myAnnotation = newEdit.getAnnotation(); 
         newEdit.show(myAnnotation); 
         wasDoubleClick = false; 
        } 
        else 
        { 
         GalleryScreen.getA().deleteAnnotation(); 
         GalleryScreen.getA().showAnnotation(scrLocX, scrLocY , myAnnotation); 
        } 
       } 
      }); 
      timer.setRepeats(false); 
      timer.start(); 
     } 
    } 
    public void mouseEntered(MouseEvent e) 
    { 

    } 
    public void mouseExited(MouseEvent e) 
    { 

    } 
    public void mousePressed(MouseEvent e) 
    { 

    } 
    public void mouseReleased(MouseEvent e) 
    { 

    } 
} 

AnnotationVisual是应该弹出一个点击时

+0

肿块。需要帮助,请! – 2012-02-27 22:07:26

回答

1

您不会在Java中的字段上设置侦听器,而是将其设置为属性。虽然性能(根据JavaBeans规范)即可领域,他们通常做的方法对(一个消气,一个二传手;后者不需要只读域)的,可以让你在钩额外的逻辑是在访问属性时调用。如触发一个监听器回调来表示值已经改变。 (你可以使用一个线程来监视这种事情,但这真的很糟糕,而且很容易出错。)浪费太多了。

但有一点需要注意:你不知道该线程的值是多少被修改了。调用回摆时要小心......

+0

我想到了线程,但这太复杂了为了我。你可以看看附加的代码,因为setter和getters不使用我的代码。 – 2012-02-27 22:06:30

4

你可能会更好过制造的东西boolean private,并且只允许通过setter方法更改它。 setter方法在调用时应该重新绘制组件。

+0

对不起,我只是添加了代码,也许它使它更清晰。因为我是第一次在一个组中进行编程,因此值的发送和接收不太好,所以我不能使用你的方法来做到这一点,但这很有意义 – 2012-02-27 22:02:03

3

听众的点是反转的逻辑。你不会经常检查一个值是否改变。您在更改值时通知监听者。

所以,与其Foo.bar = 5,你调用Foo.setBar(5),其中除分配,你叫barListener.valueChanged(value)

一点题外话 - 避免在静态变量存储状态。

+0

对不起,我只是添加了代码,也许它使它更清晰。因为我是第一次编程在一个组中,所以值的发送和接收都不是很好,所以我不能用你的方法去做,但这很有意义 – 2012-02-27 22:03:41