简体   繁体   English

Java-MouseListener在另一个类中?

[英]Java - MouseListener in another class?

I realize this is a repeat question, but my circumstances are a little bit different. 我意识到这是一个重复的问题,但是我的情况有所不同。 I need to have a MouseListener in another class that can altar the background color of the object that calls it. 我需要在另一个类中增加一个MouseListener,该类可以使调用它的对象的背景色变高。 Please help me. 请帮我。

public class LeftListPanel extends JPanel {

public LeftListPanel() {
    setBackground(Settings.BACKGROUND_COLOR);
    setLayout(null);

    addPersonalStatsTab();
}

private void addPersonalStatsTab() {
    JPanel personalStatsPanel = new JPanel();
    personalStatsPanel.addMouseListener(new CustomMouseListener());

    JLabel personalStatsText = new JLabel("Text");
    personalStatsPanel.add(personalStatsText);

    add(personalStatsPanel);
}

Then I have an inner-nested class for the MouseListener because this is the only place this MouseListener will be called. 然后,我为MouseListener提供了一个内部嵌套的类,因为这是将调用此MouseListener的唯一位置。

class CustomMouseListener implements MouseListener {

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {
        setBackground(Settings.BACKGROUND_COLOR.brighter());
    }

    @Override
    public void mouseExited(MouseEvent e) {
        setBackground(Settings.BACKGROUND_COLOR);
    }

}

The setBackground(COLOR) lines are those who don't work... this.setBack and super.setBack ARE NOT working in this case.. I'M DESPERATE FOR HELP! setBackground(COLOR)行是无效的……this.setBack和super.setBack在这种情况下不起作用。我非常希望获得帮助!

The reason you don't see the background changes is that when you call setBackground , you are de-referring (implicitly) the this object, ie the instance of LeftListPanel . 您看不到背景变化的原因是,当您调用setBackground ,您正在取消(隐式)引用this对象,即LeftListPanel的实例。 So, you are actually changing its background, but you don't see it because inside the LeftListPanel instance there is another JPanel (instantiated at the addPersonalStatsTab method) which occupies the whole visible space (or even it is not visible at all, because of that weird null layout; I don't know exactly). 因此,您实际上是在更改其背景,但看不到它,因为在LeftListPanel实例内部还有另一个JPanel(在addPersonalStatsTab方法上实例化),该JPanel占据了整个可见空间(甚至根本不可见),因为那奇怪的null布局;我不完全知道)。

  • Fist of all, I recommend to you not to set null as a layout. 首先,我建议您不要将null设置为布局。 Chose a proper layout, or let it be defaulted - do not call setLayout(null) . 选择适当的布局,或者将其设置为默认布局-不要调用setLayout(null)

  • Then, set personalStatsPanel as a private member of LeftListPanel . 然后,将personalStatsPanel设置为LeftListPanel私有成员 And when calling to setBackground , use it as the scope reference: 并且在调用setBackground ,将其用作范围参考:

      LeftListPanel.this.personalStatsPanel.setBackground(...); 

This works, I instead just created a private method where I pass in the panel I want to apply it too. 这行得通,我只是创建了一个私有方法,在其中我也要应用它的面板中传递。

private void CustomMouseListener(JPanel panel) {
    panel.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseReleased(MouseEvent e) {

        }

        @Override
        public void mouseEntered(MouseEvent e) {
            panel.setBackground(Settings.BACKGROUND_COLOR.brighter());
        }

        @Override
        public void mouseExited(MouseEvent e) {
            panel.setBackground(Settings.BACKGROUND_COLOR);
        }

    });
}

Thank you all for your time and suggestions :) 谢谢大家的时间和建议:)

You could... 你可以...

Pass a reference of the component you want changed to the CustomMouseListener 将要更改的组件的引用传递给CustomMouseListener

class CustomMouseListener implements MouseListener {

    private JPanel panel;

    public CustomMouseListener(JPanel panel) {
        this.panel = panel;
    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {
        panel.setBackground(Settings.BACKGROUND_COLOR.brighter());
    }

    @Override
    public void mouseExited(MouseEvent e) {
        panel.setBackground(Settings.BACKGROUND_COLOR);
    }

}

This is okay if you want to use the listener on a limited number of components, but if you want to use the same listener on a number of components... 如果要在有限数量的组件上使用侦听器,这是可以的,但是如果要在多个组件上使用相同的侦听器,这是可以的...

You could... 你可以...

Use the source property of the MouseEvent to get which component triggered the event 使用MouseEventsource属性获取触发事件的组件

@Override
public void mouseEntered(MouseEvent e) {
    if (!(e.getSource() instanceof JPanel)) {
        return;
    }
    JPanel panel = (JPanel)e.getSource();
    panel.setBackground(Settings.BACKGROUND_COLOR.brighter());
}

or, a better solution would be to do something more like... 或者,更好的解决方案是做更多类似的事情...

@Override
public void mouseEntered(MouseEvent e) {
    e.getComponent().setBackground(Settings.BACKGROUND_COLOR.brighter());
}

since the information is already provided to you (just not, this returns an instance of Component , so if you need to access the Swing specific properties, you'd still need to cast it). 由于信息已经提供给您(只是没有提供,所以它返回Component一个实例,因此,如果您需要访问Swing的特定属性,则仍然需要对其进行强制转换)。

Why is this approach better? 为什么这种方法更好?

CustomMouseListener listener = new CustomMouseListener();
panel1.addMouseListener(listener);
panel2.addMouseListener(listener);
panel3.addMouseListener(listener);
panel4.addMouseListener(listener);
panel5.addMouseListener(listener);
panel6.addMouseListener(listener);
panel7.addMouseListener(listener);

because it's agnostic, meaning you can create a single instance of the listener and re-use on multiple components 因为它是不可知论的,这意味着您可以创建侦听器的单个实例并在多个组件上重用

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

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