Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:检查框架MousePressListener中是否按下了JButton_Java - Fatal编程技术网

Java:检查框架MousePressListener中是否按下了JButton

Java:检查框架MousePressListener中是否按下了JButton,java,Java,我有一个JFrame,里面有一个按钮。当用户单击框架上的任何位置时,我想检查按钮是否在按下之前被按下或单击 例如,如果用户没有先单击按钮就单击了框架,则应显示“按钮未按下”,但如果用户单击按钮,然后按下框架上的任何位置,则应显示“按钮已按下” 我的代码: import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.eve

我有一个JFrame,里面有一个按钮。当用户单击框架上的任何位置时,我想检查按钮是否在按下之前被按下或单击

例如,如果用户没有先单击按钮就单击了框架,则应显示“按钮未按下”,但如果用户单击按钮,然后按下框架上的任何位置,则应显示“按钮已按下”

我的代码:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Test1 
{
    public static void main(String[] args)
    {
        JFrame frame = new JFrame();

        final int FRAME_WIDTH  = 400;
        final int FRAME_HEIGHT = 400;

        frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
        frame.setTitle("Test 1");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());

        JPanel panel = new JPanel();
        frame.add(panel,BorderLayout.NORTH);

        final JButton btnRectangle = new JButton("Rectangle");
        panel.add(btnRectangle);

        class MousePressListener implements MouseListener
        {
            public void mousePressed(MouseEvent event)
            {
                int x = event.getX() ; 
                int y = event.getY() ;

                System.out.println("you have press the mouse at X : " + x + " and Y : " + y);

                if(btnRectangle.getModel().isSelected())
                    System.out.println("the button is pressed");
                else 
                    System.out.println("the button is NOT pressed");
            }

            public void mouseReleased(MouseEvent event){}
            public void mouseClicked(MouseEvent event){}
            public void mouseEntered(MouseEvent event){}
            public void mouseExited(MouseEvent event){}
        }

        MousePressListener mListener = new MousePressListener();
        frame.addMouseListener(mListener);

        frame.setVisible(true);
    }

}
如您所见,我试图检查按钮是否在JFrame鼠标按下的侦听器中被单击。但是没有成功。我亦会看看这个问题:


但似乎什么也做不到,请帮助

完成以下步骤

public class Test2 
{
    static boolean isPressed = false;

     public static void main(String[] args)
     {
        .....
             btnRectangle.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                isPressed = true;
            }
        });
        .....


        class MousePressListener implements MouseListener
        {
            public void mousePressed(MouseEvent event)
            {
                if(!isPressed){
                     System.out.println("the button is NOT pressed");

                }else{
                    System.out.println("the button is pressed");
                }
               .....
             }
          }


     }

}   
见产出


完成以下步骤

public class Test2 
{
    static boolean isPressed = false;

     public static void main(String[] args)
     {
        .....
             btnRectangle.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                isPressed = true;
            }
        });
        .....


        class MousePressListener implements MouseListener
        {
            public void mousePressed(MouseEvent event)
            {
                if(!isPressed){
                     System.out.println("the button is NOT pressed");

                }else{
                    System.out.println("the button is pressed");
                }
               .....
             }
          }


     }

}   
见产出


我建议在
JButton
中添加一个
ActionListener
,它应该优先于帧级侦听器。这样,您就有了自定义的
JFrame
MouseListener
和一个单独的侦听器来处理
JButton
逻辑。我建议在
JButton
中添加一个
ActionListener
,该侦听器应优先于帧级侦听器。这样,您就有了自定义的
JFrame
MouseListener
和一个单独的侦听器来处理
JButton
逻辑。