2016-03-10 86 views
0

我有一個問題,我無法解決。按鈕b1和b2完美地工作,但是當我嘗試在b3上實現動作偵聽器時(與我添加到按鈕1和2中的方式完全相同),它不起作用。我很難過。我從字面上複製並粘貼了我爲b3實現b1和b2的方式,但它不起作用!它的奇怪,因爲它只會在我點擊b3時起作用,然後右鍵點擊它,但我應該點擊b3(命名邊緣)並在任何地方點擊鼠標左鍵以使消息出現。ActionListener不在按鈕上工作,除非我右鍵單擊它?

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); 
    } 

}

回答

0

您的格式是有點偏離,作出一個艱難的閱讀。 看起來你被按下檢查爲B3:

否則,如果(t.lastbuttonpressed == B3){

之外這一點,如果檢查:

如果(SwingUtilities.isLeftMouseButton(E)) {

+0

啊我想通了。它與mousePressed的if語句中的大括號有關。我的新問題是我如何將一個Line2D.Double對象存儲在Shape類型的數組中? – Bossrevz

+0

是java,我會使用ArrayList 。 – zecmo

相關問題