用于Jpanel,Java上的按钮的.addMouseListener的MouseListeners和KeyListener的方法

时间:2017-06-18 22:47:14

标签: java swing methods mouselistener

我一直难以在我的代码中重复使用Listener方法。此外,我是新手,所以我很抱歉我遗漏了这一切。虽然,现在这里是我的听众方法:

public static void keysEvents(Optional<String> output)
{
    MouseListener mouseEvents = new MouseAdapter()
    {
        public void mouseClick (MouseEvent mouseEvent)
        {
            Integer mouseModifiers = mouseEvent.getModifiers();
            if ((mouseModifiers & InputEvent.BUTTON1_MASK) == 
            InputEvent.BUTTON1_MASK)
            {
                if (output == null)
                {
                    System.out.println("click");
                }
                //more options...
            }
        }
        public void mouseRelease (MouseEvent mouseEvent)
        {

        }
    };
    //More listeners...
}

编辑版本:

public static MouseListener keysEvents(Optional<String> output)
{
    MouseListener mouseEvents = new MouseAdapter()
    {
        public void mouseClick (MouseEvent mouseEvent)
        {
            Integer mouseModifiers = mouseEvent.getModifiers();
            if ((mouseModifiers & InputEvent.BUTTON1_MASK) == 
            InputEvent.BUTTON1_MASK)
            {
                if (output == null)
                {
                    System.out.println("click");
                }
                //more options...
            }
        }
        public void mouseRelease (MouseEvent mouseEvent)
        {

        }
    };
    //More listeners...
    return mouseEvents;
    //How would I have it return different listeners?
}

以下是程序窗口的代码:

    JPanel MainP = new JPanel();
    MainP.setLayout(new GridLayout(4, 2, 100, 30));
    frame.setLayout(new GridLayout(1, 2));
    frame.setBackground(Color.blue);
    JPanel _B1_ = new JPanel();
    JPanel _B2_ = new JPanel();
    JPanel _B3_ = new JPanel();
    JPanel _B4_ = new JPanel();
    Button _Continue_ = new Button("Continue");
    Button _Load_Game_ = new Button("Load Game");
    Button _Settings_ = new Button("Settings");
    Button _Exit_ = new Button("Exit");
    _Continue_.addMouseListener(keysEvents(Optional.of(""))); //<edited here.
    _Continue_.setBackground(Color.lightGray);
    _Load_Game_.setBackground(Color.lightGray);
    _Settings_.setBackground(Color.lightGray);
    _Exit_.setBackground(Color.lightGray);
    MainP.setBackground(Color.gray);
    _B1_.setBackground(Color.gray);
    _B2_.setBackground(Color.gray);
    _B3_.setBackground(Color.gray);
    _B4_.setBackground(Color.gray);
    MainP.add(_B1_);
    MainP.add(_Continue_);
    MainP.add(_B2_);
    MainP.add(_Load_Game_);
    MainP.add(_B3_);
    MainP.add(_Settings_);
    MainP.add(_B4_);
    MainP.add(_Exit_);
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.setUndecorated(!frameBorder);
    frame.pack();
    frame.setMinimumSize(new Dimension(_minX, _minY));
    frame.setSize(windowX, windowY);
    frame.add(MainP);
    frame.setVisible(true);

最后,如果我以任何方式错误地询问,请告诉我;所以我可以在提问时做得更好。希望它不会像生活中那样扼杀你的生活。

1 个答案:

答案 0 :(得分:0)

您需要使用ActionListener。这是一个与JButtons一起使用的方法,我已经多次使用它了。 JButtons比Buttons更受欢迎,更容易使用。 JButtons是swing组件的一部分。以下是ActionListener的使用示例:

JButton button = new JButton("Example"); //Create a JButton with the text "Example"
button.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        //Code to be executed upon press goes here
    }
});

我还想加入@ camickr的评论; Java变量应始终以小写字母开头,并且永远不要以_开头。例如,JButton continue = new JButton("Continue");将是声明和初始化按钮的正确方法。可变名称中起始单词后面的任何单词都应大写。例如,JButton continueButton = new JButton("Continue");将是正确的命名。