为addActionListener找到正确的参数

时间:2017-03-29 07:18:19

标签: java jframe jbutton actionlistener

我正在尝试按下按钮以切换按钮的状态(更改颜色和文本)。但我无法使addActionListener工作,我无法从我的主类中找到ToggleState方法。我是java新手,非常感谢你的帮助。

我得到了错误   - “AbstractButton类型中的方法addActionListener(ActionListener)不适用于参数” - “ActionListener无法解析为类型”

package Grafiktest;

import javax.swing.*;
import java.awt.*;

public class MyButton extends JButton{

private static final long serialVersionUID = 1L;

public MyButton(Color c1, Color c2, String s1, String s2){

    setText(s1);
    setForeground(c1);
    setBackground(c1);
    setOpaque(true);

}

public void toggleState(Color c1, Color c2, String s1, String s2){

    setText(s2);
    setForeground(c2);
    setBackground(c2);
    setOpaque(true);

}

这是按钮类

dotnet publish

}

3 个答案:

答案 0 :(得分:2)

让我们停下来思考一下你正在尝试做什么。您想要在按下按钮时更改按钮的状态。

真正的问题是,实际上谁负责这样做?任何ActionListener监控按钮应该专注于执行他们需要做的工作,而不是管理按钮,当按钮上连接了多个ActionListener时会发生什么?谁负责呢?

相反,如果我们让按钮自我管理呢?也就是说,按钮监视自身并相应地更新其状态

因为您无论如何都需要管理所选状态,我使用的是JToggleButton,因为它有一个"选择" /"未选中&# 34;国内。

public class MyButton extends JToggleButton {

    private static final long serialVersionUID = 1L;

    public class State {
        public Color color;
        public String text;

        public State(Color color, String text) {
            this.color = color;
            this.text = text;
        }

    }

    private final State unselectedState;
    private final State selectedState;

    public MyButton(Color c1, Color c2, String s1, String s2) {

        selectedState = new State(c1, s1);
        unselectedState = new State(c2, s2);

        setContentAreaFilled(false);
        setBorderPainted(false);
        setFocusPainted(false);
        setOpaque(true);

        toggleState();

        getModel().addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                toggleState();
            }
        });

    }

    public void toggleState() {

        State state = !getModel().isSelected() ? selectedState : unselectedState;

        setText(state.text);
        setForeground(Color.WHITE);
        setBackground(state.color);

    }
}

基本上所有这一切都将根据selected的{​​{1}}状态配置按钮。

您可以使用ButtonModel并以类似方式将其直接注册到按钮本身,但您需要管理其使用的状态

答案 1 :(得分:1)

完成@Japu_D_Cret的评论:

您需要使用toggleState方法静态调用它,就像使用actionPerformed方法一样,或者使用按钮调用它:

button.toggleState(Color.green, Color.red, "RUN","STOP");

因为它是您MyButton课程中定义的方法,而不是coolgrafik课程中定义的方法。

答案 2 :(得分:0)

  

“AbstractButton类型中的方法addActionListener(ActionListener)不适用于参数” - “ActionListener无法解析为类型”

你没有导入ActionListener类,因为它不在java.awt。*中但在java.awt.event.ActionListener中 - 用于引用https://docs.oracle.com/javase/7/docs/api/java/awt/event/ActionListener.html

  

但我仍然无法使用actionperformer中的toggleState方法。怎么会?

你的void actionPerformed(ActionEvent)试图调用一个名为void toggleState(Color,Color,String,String)的方法 - 但它不是在同一个类中声明,也不是静态导入的。

您显然希望在类toggleState的对象button上调用MyButton方法,为此,您必须像button.toggeState(...)那样调用该对象上的方法 这也不会直接起作用,因为您的对象button仅在构造函数中已知,并且您必须向您的coolgrafik类添加一个对象属性,就像您对面板所做的那样 - 那么它应该可以正常工作

这里是您需要在coolgrafik类中更改的代码片段

属性:

private JPanel panel;
private JLabel label;
private MyButton button;
你的coolgrafik构造函数中的

//...
this.button = new MyButton(Color.green, Color.red, "RUN","STOP");
this.panel.add(this.button);
this.button.addActionListener(this);
//...

和你的actionPerformed方法

public void actionPerformed(ActionEvent e) {
    button.toggleState(Color.green, Color.red, "RUN","STOP");
}