除非我右键单击它,否则ActionListener不会处理按钮?

时间:2016-03-10 01:53:55

标签: jbutton actionlistener paint graphics2d

我有一个问题我无法解决。按钮b1和b2完美地工作,但是当我尝试在b3上实现动作监听器时,(与我添加到按钮1和2的方式完全相同)它不起作用。我很难过。我实际上复制并粘贴了我为b3实现b1和b2的方式并且它不起作用!!它很奇怪,因为只有当我点击b3,然后右键单击它才能工作,但是我应该点击b3(命名边缘)并左键单击任何地方以显示消息。

b1将显示一个矩形,b2将显示椭圆,右键单击一个形状将删除它。

   import java.awt.BorderLayout;
   import java.awt.Color;
   import java.awt.Graphics;
   import java.awt.Graphics2D;
   import java.awt.GridBagConstraints;
   import java.awt.GridBagLayout;
   import java.awt.Insets;
   import java.awt.Rectangle;
   import java.awt.Shape;
   import java.awt.event.ActionEvent;
   import java.awt.event.ActionListener;
   import java.awt.event.MouseEvent;
   import java.awt.event.MouseListener;
   import java.awt.geom.Ellipse2D;
   import java.awt.geom.Line2D;
   import java.awt.geom.Point2D;
   import java.util.ArrayList;

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

    public class Tutorial extends JPanel
{

    /**
 * 
 */
private static final long serialVersionUID = 1L;
    public static void storeShape(Shape shape){
        shapes.add(shape);
    }
    public static void removeShape(int index){
        shapes.remove(index);
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g; 

        if(shape != null) 
    for ( Shape shape1 : shapes){
        g2.draw(shape1);
    }

    }
     private static ArrayList<Shape> shapes = new ArrayList<Shape>();
     private Object lastbuttonpressed;
     static Shape shape;
    public static void main (String[] args){

        Tutorial t = new Tutorial();
        JFrame f = new JFrame();
        f.setTitle("Tutorial");
        f.setSize(600, 400);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(t);
        JPanel p = new JPanel(new GridBagLayout());


        JButton b1 = new JButton("Rectangle");
        JButton b2 = new JButton("Ellipse");
        JButton b3 = new JButton("Edge");
        JButton b4 = new JButton("Label");
        GridBagConstraints gbc2 = new GridBagConstraints();
        gbc2.insets = new Insets(5,5,5,5);
        gbc2.gridx = 0;
        gbc2.gridy = 0;
        p.add(b1,gbc2);
        gbc2.gridx = 1;
        gbc2.gridy = 0;
        p.add(b2,gbc2) ;
        gbc2.gridx = 2;
        gbc2.gridy = 0;
        p.add(b3,gbc2);
        gbc2.gridx = 0;
        gbc2.gridy = 1;
        p.add(b4,gbc2);
        f.add(p, BorderLayout.NORTH);
        JTextField textField = new JTextField(" text field");
        gbc2.gridx = 1;
        gbc2.gridy = 1;
        p.add(textField, gbc2);



        b1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                t.lastbuttonpressed = e.getSource();

            }
        });
        b2.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                t.lastbuttonpressed = e.getSource();
            }
        });
        b3.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                t.lastbuttonpressed = e.getSource();
            }
        });

        f.addMouseListener(new MouseListener() {

            @Override
            public void mouseReleased(MouseEvent e) {
                // TODO Auto-generated method stub
            }

            @Override
            public void mousePressed(MouseEvent e) {
                if (SwingUtilities.isLeftMouseButton(e)){
                if (t.lastbuttonpressed == b1){



                    shape = new Rectangle(e.getX()-50,e.getY()-120,100,50);
                    storeShape(shape);
                    t.repaint();
                }
                else if ( t.lastbuttonpressed == b2){
                    shape = new Ellipse2D.Double(e.getX()-50,e.getY()-120,100,50);  
                    storeShape(shape);
                    t.repaint();
                }

            }
                else if (t.lastbuttonpressed == b3){
                    JOptionPane.showMessageDialog(null, "button 3");
                    t.repaint();
                }
                else if (SwingUtilities.isRightMouseButton(e)){

                    for (int i=0; i<shapes.size(); i++){

                    if (shapes.get(i).contains(e.getX()-50,e.getY()-100)){

                shapes.remove(i);
                t.repaint();

                    }

            }
                }

            }

            @Override
            public void mouseExited(MouseEvent e) {
                // TODO Auto-generated method stub
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                // TODO Auto-generated method stub  
            }

            @Override
            public void mouseClicked(MouseEvent e) {


            }

        });
        f.setVisible(true);
    }

}

1 个答案:

答案 0 :(得分:0)

您的格式有点偏差,难以阅读。 看起来你正在检查被按下的b3:

否则if(t.lastbuttonpressed == b3){

在此之外如果检查:

if(SwingUtilities.isLeftMouseButton(e)){