首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >布尔值更改监听器Java

布尔值更改监听器Java
EN

Stack Overflow用户
提问于 2012-02-28 05:42:37
回答 3查看 7.3K关注 0票数 2

我需要一个侦听器,它会不断地检查静态布尔值是否发生了变化,这样我就可以在框架上重新绘制组件。有没有人能帮帮我?我真的不太了解听众,也没怎么和他们合作过。我们将非常感谢您的帮助。

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

代码语言:javascript
复制
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;
}

这是“可点击的照片”

代码语言:javascript
复制
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是应该在单击时弹出的东西

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-02-28 05:48:01

在Java中,不是在字段上设置侦听器,而是在属性上设置侦听器。虽然属性(根据JavaBeans规范)可以是字段,但它们通常是成对的方法(一个getter,一个setter;后者对于只读字段不是必需的),因为它允许您在访问属性时将额外的逻辑挂接到其中。例如,触发侦听器回调以声明该值已更改。(您可以使用线程来监视这类事情,但这真的很麻烦且容易出错。也很浪费。)

但是有一件事需要注意:你不知道这个值是从哪个线程修改的。在调用回Swing…时要小心

票数 1
EN

Stack Overflow用户

发布于 2012-02-28 05:44:56

最好将布尔值设为私有,并且只允许通过setter方法对其进行更改。当调用setter方法时,应该重新绘制组件。

票数 5
EN

Stack Overflow用户

发布于 2012-02-28 05:45:25

侦听器的意义在于颠倒逻辑。您不会经常检查某个值是否发生了更改。当您更改该值时,您将通知侦听器。

因此,您可以调用Foo.setBar(5)而不是Foo.bar = 5,其中除了赋值之外,您还可以调用barListener.valueChanged(value)

作为附注-避免将状态存储在静态变量中。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9472753

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档