使用事件处理程序时不会触发操作-KeyListener

时间:2019-03-07 07:05:50

标签: java swing keylistener repaint mouselistener

所以我是事件处理程序的新手,我想创建一个程序,在框架内制作一个紫色面板,如果用户单击面板区域中的鼠标按钮,然后使用键盘键入%,希望面板在面板区域中绘制一条线段。这只是测试事件处理程序。目前,我正在尝试,但是没有用。所有帮助将不胜感激。掌握了这一点之后,我想尝试一下repaint()方法,如果有人执行与以前相同的操作,我想检查面板的背景。如果您能同时帮助我,那会很棒,但这不是优先事项。谢谢。

 import java.awt.Graphics;          // for classes Graphics, Color, Font, Image
import java.awt.Color;
import java.awt.Font;

import java.awt.event.KeyEvent; // for classes KeyListener, MouseListener
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;      // for classes JFrame, JPanel, JLabel
import javax.swing.JPanel;

public class GardenGrows
{
    public GardenGrows()
    {
    }

    public static void main(String[] args)
    {
        GardenGrows gg = new GardenGrows();
        gg.runIt();
    }

    // makes the frame and handles all properties of the frame 
    // also instantiates the Garden.java object 
    public void runIt()
    {
        JFrame frame = new JFrame ("Garden");

        frame.setDefaultCloseOperation( frame.EXIT_ON_CLOSE );
        frame.setLayout(null);

        frame.setBackground(Color.GRAY);
        frame.setSize( 1200,700);
        frame.setLocation( 50, 10);     
        frame.setResizable(true);
        frame.setVisible(true);

        Garden gar = new Garden();
        frame.getContentPane().add(gar);


    }

}

class Garden extends JPanel implements KeyListener, MouseListener
{
    private boolean mouseClicked;
    private boolean keyClicked;
    private int xpos;
    private int ypos; 

    public Garden()
    {
        mouseClicked = false;
        keyClicked = false;
        addKeyListener(this);
        addMouseListener(this);

        setBackground(Color.PINK);
        setLocation(50,50);
        setSize(1000,500);



    }



    public void mousePressed(MouseEvent evt)
    {
        int x = evt.getX();
        int y = evt.getY();

        if(x>=50 || x<=1050 && y<=50 || y>=550)
        {
            mouseClicked = true;
        }
    } 
    public void mouseClicked(MouseEvent evt){}
    public void mouseReleased(MouseEvent evt){}
    public void mouseEntered(MouseEvent evt){}
    public void mouseExited(MouseEvent evt){}

    public void keyReleased(KeyEvent evt){}
    public void keyPressed(KeyEvent evt){}

    public void keyTyped(KeyEvent evt)
    {
        int letter = evt.getKeyCode();
        if (letter == 13)
        {
             keyClicked =true;
        }
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        if(keyClicked && mouseClicked)
             g.drawLine(50,0,110,120);

    }


}

1 个答案:

答案 0 :(得分:2)

在JPanel中使关键侦听器工作不会那么简单,因为它不能集中。 (考虑一下JTextField,当您在其上编写文本时,它会被聚焦)。因此,为了完成这项工作,您将必须use KeyBindings。实际上,请从您的JPanel中删除KeyListener实现,并使用KeyBindings。

public class GardenGrows {
    public GardenGrows() {
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            // All swing applications must run on EDT Thread
            GardenGrows gg = new GardenGrows();
            gg.runIt();
        });
    }

    // makes the frame and handles all properties of the frame
    // also instantiates the Garden.java object
    public void runIt() {
        JFrame frame = new JFrame("Garden");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(null);

        frame.setBackground(Color.GRAY);
        frame.setSize(1200, 700);
        frame.setLocation(50, 10);
        frame.setResizable(true);
        frame.setVisible(true);

        Garden gar = new Garden();
        frame.getContentPane().add(gar);

    }

}

class Garden extends JPanel implements MouseListener {
    private boolean mouseClicked;
    private boolean keyClicked;
    private int xpos;
    private int ypos;

    public Garden() {
        mouseClicked = false;
        keyClicked = false;
        addMouseListener(this);

        setBackground(Color.PINK);
        setLocation(50, 50);
        setSize(1000, 500);
        getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_5, KeyEvent.SHIFT_MASK),
                "percentageClicked");
        getActionMap().put("percentageClicked", new AbstractAction() {

            @Override
            public void actionPerformed(ActionEvent e) {
                keyClicked = true;
                repaint(); // Try to draw the line
            }
        });
    }

    public void mousePressed(MouseEvent evt) {
        int x = evt.getX();
        int y = evt.getY();

        if (x >= 50 || x <= 1050 && y <= 50 || y >= 550) {
            mouseClicked = true;
            repaint(); // Try to draw the line
        }
    }

    public void mouseClicked(MouseEvent evt) {
    }

    public void mouseReleased(MouseEvent evt) {
    }

    public void mouseEntered(MouseEvent evt) {
    }

    public void mouseExited(MouseEvent evt) {
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        if (keyClicked && mouseClicked)
            g.drawLine(50, 0, 110, 120);

    }

}