简体   繁体   English

将JButton设置移到单独的类

[英]Move JButton setup to separate class

I have a loop that creates a series of JButtons. 我有一个创建一系列JButton的循环。 This code is buried deep inside a class that is primarily concerned with multi-threading, so it uses Executors, Callables, Futures, etc. I'm trying to keep this class fairly encapsulated, so I want to move the work of setting up the JButton to its own class. 这段代码被深埋在主要与多线程有关的类的内部,因此它使用了Executors,Callables,Futures等。我试图将此类封装得相当清楚,因此我想着手建立该类的工作。 JButton为其自己的类。 This is the body of my loop where it creates a single button. 这是循环的主体,在其中创建了一个按钮。 It works just fine: 它工作正常:

    JButton imageButton = new JButton(new ImageIcon(image));
    imageButton.setMinimumSize(imageSize);
    imageButton.setPreferredSize(imageSize);
    imageButton.setMaximumSize(imageSize);
    imageButton.setVisible(true);

    imageButton.addMouseMotionListener(new MouseMotionListener() {
        @Override
        public void mouseDragged(java.awt.event.MouseEvent mouseEvent) {
            // do a bunch of stuff          }
        }

        @Override
        public void mouseMoved(java.awt.event.MouseEvent mouseEvent) {}
    });

    imagesPanel.add(imageButton);

This is only going to get messier, so here is my attempt to move it to a separate class: 这只会变得更加混乱,因此这是我尝试将其移至单独的类中的尝试:

ImageButton imageButton = new ImageButton(image, imageSize);
imageButton.addMouseMotionListener();
imagesPanel.add(imageButton);

And this is my class: 这是我的课:

public class ImageButton extends JButton {

    JButton button;
    static final long serialVersionUID = 1;

    public ImageButton(Image image, Dimension imageSize) {
        button = new JButton(new ImageIcon(image));
        button.setMinimumSize(imageSize);
        button.setPreferredSize(imageSize);
        button.setMaximumSize(imageSize);
        button.setVisible(true);
    }

    public void addMouseMotionListener() {
        button.addMouseMotionListener(new MouseMotionListener() {
            @Override
            public void mouseDragged(java.awt.event.MouseEvent mouseEvent) {
                // do a bunch of stuff
            }

            @Override
            public void mouseMoved(java.awt.event.MouseEvent mouseEvent) {}
        });
    }   
}

This all compiles without error, for whatever that is worth. 所有这些都可以毫无错误地进行编译,无论值多少。 The buttons layout in their proper sizes and proper places. 按钮按其适当的大小和适当的位置进行布局。 However, the images do not appear (the buttons are blank) and the mouse listener is not functioning. 但是,图像不会出现(按钮为空白),并且鼠标侦听器不起作用。 Can anyone see what I am doing wrong and how to make this work? 谁能看到我在做什么错以及如何使它工作?

Your ImageButton class is wrong. 您的ImageButton类错误。 You're holding an unnecessary instance to JButton inside. 您正在将不必要的实例保存到JButton中。 So, the implementation should be: 因此,实现应为:

public class ImageButton extends JButton {

    static final long serialVersionUID = 1;

    public ImageButton(Image image, Dimension imageSize) {
        super(new ImageIcon(image));
        this.setMinimumSize(imageSize);
        this.setPreferredSize(imageSize);
        this.setMaximumSize(imageSize);
        this.setVisible(true);
    }

    public void addMouseMotionListener() {
        this.addMouseMotionListener(new MouseMotionListener() {
            @Override
            public void mouseDragged(java.awt.event.MouseEvent mouseEvent) {
                // do a bunch of stuff
            }

            @Override
            public void mouseMoved(java.awt.event.MouseEvent mouseEvent) {}
        });
    }   
}

This way, you're setting the parameters to the class instead to a member, and all instances of ImageButton will have the same configuration. 这样,您将参数设置为类而不是成员,并且ImageButton所有实例将具有相同的配置。

You extending JButton AND you create another JButton inside your class. 扩展JButton并在类中创建另一个JButton。 Probably you wanted to do something like this: 可能您想做这样的事情:

public static class ImageButton extends JButton {

    JButton button; // Remove me
    static final long serialVersionUID = 1;

    public ImageButton(Image image, Dimension imageSize) {
        super(new ImageIcon(image));
        setMinimumSize(imageSize);
        setPreferredSize(imageSize);
        setMaximumSize(imageSize);
        setVisible(true);

        // Dont see why there should be separate method for the addMouseMotionListeners
        addMouseMotionListener(new MouseMotionListener() {
            @Override
            public void mouseDragged(java.awt.event.MouseEvent mouseEvent) {
                // do a bunch of stuff
            }

            @Override
            public void mouseMoved(java.awt.event.MouseEvent mouseEvent) {}
        });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM