2016-10-01 71 views
4

你好,我已經創建了一個Java圖形計算 。現在我想添加點擊 事件到特定的按鈕,以便 處理數據,我應該添加到 我的代碼?我希望在Javascript中有 類似「onclik」。 Max謝謝!Java添加事件特定按鈕

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

public class Fenetre extends JFrame{ 
    private JPanel pan = new JPanel(); 
    private JButton bouton1 = new JButton("C"); 
    private JButton bouton2 = new JButton("+"); 
    private JButton bouton3 = new JButton("-"); 
    private JButton bouton4 = new JButton("*"); 
    private JButton bouton5 = new JButton("/"); 

    private JButton bouton6 = new JButton("1"); 
    private JButton bouton7 = new JButton("2"); 
    private JButton bouton8 = new JButton("3"); 
    private JButton bouton9 = new JButton("4"); 
    private JButton bouton10 = new JButton("5"); 
    private JButton bouton11 = new JButton("6"); 
    private JButton bouton12 = new JButton("7"); 
    private JButton bouton13 = new JButton("8"); 
    private JButton bouton14 = new JButton("9"); 
    private JButton bouton15 = new JButton("0"); 
    private JButton bouton16 = new JButton("."); 
    private JButton bouton17 = new JButton("="); 


    private JLabel ecran = new JLabel(); 

    public Fenetre(){ 
    ecran = new JLabel("0"); 
    this.setTitle("Calculatrice"); 
    this.setSize(300, 450); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    this.setLocationRelativeTo(null); 
    //Ajout du bouton à notre content pane 
    pan.setLayout(null); 
    ecran.setBounds(100,2,100,60); 

    bouton1.setBounds(220,60,60,60); 
    bouton2.setBounds(220,130,60,60); 
    bouton3.setBounds(220,200,60,60); 
    bouton4.setBounds(220,270,60,60); 
    bouton5.setBounds(220,340,60,60); 

    bouton6.setBounds(10,60,60,60); 
    bouton7.setBounds(80,60,60,60); 
    bouton8.setBounds(150,60,60,60); 
    bouton9.setBounds(10,130,60,60); 
    bouton10.setBounds(80,130,60,60); 
    bouton11.setBounds(150,130,60,60); 

    bouton12.setBounds(10,200,60,60); 
    bouton13.setBounds(80,200,60,60); 
    bouton14.setBounds(150,200,60,60); 

    bouton15.setBounds(10,270,60,60); 
    bouton16.setBounds(80,270,60,60); 
    bouton17.setBounds(150,270,60,60); 

    pan.add(ecran); 
    pan.add(bouton1); 
    pan.add(bouton2); 
    pan.add(bouton3); 
    pan.add(bouton4); 
    pan.add(bouton5); 
    pan.add(bouton6); 
    pan.add(bouton7); 
    pan.add(bouton8); 
    pan.add(bouton9); 
    pan.add(bouton10); 
    pan.add(bouton11); 
    pan.add(bouton12); 
    pan.add(bouton13); 
    pan.add(bouton14); 
    pan.add(bouton15); 
    pan.add(bouton16); 
    pan.add(bouton17); 
    this.setContentPane(pan); 
    this.setVisible(true); 
    }  
}enter code here 

回答

1

你只需要一個ActionListener添加到應該觸發點擊動作按鈕,這樣的事情:

bouton1.addActionListener(
    new ActionListener() { 
     @Override 
     public void actionPerformed(final ActionEvent e) { 
      // do something here 
     } 
    } 
); 

How to Write an Action Listener

這相當於一個onclick所以應該在你的特定情況下足夠了,但是如果你需要訪問更多的鼠標事件,則應該在下面添加一個MouseListener

bouton1.addMouseListener(
    new MouseListener() { 
     @Override 
     public void mouseClicked(final MouseEvent e) { 
      ... 
     } 

     @Override 
     public void mousePressed(final MouseEvent e) { 
      ... 
     } 

     @Override 
     public void mouseReleased(final MouseEvent e) { 
      ... 
     } 

     @Override 
     public void mouseEntered(final MouseEvent e) { 
      ... 
     } 

     @Override 
     public void mouseExited(final MouseEvent e) { 
      ... 
     } 
    } 
); 
2

您可以使用addActionListener方法。
此方法接受一個ActionListener實例 - 這是一個方法 - actionPerformed,當按下按鈕,您這將被執行的接口:

例如:

b1.addActionListener(
    new ActionListener() { 
     @Override 
     public void actionPerformed(final ActionEvent e) { 
      // do whatever 
     } 
    } 
); 

有教程的普朗蒂在那裏關於揮杆事件處理,如this one這是相當不錯

+0

謝謝,我該如何指定事件是「點擊」? –

+0

@MaxHunton - 「ActionEvent」表示發生了組件定義的操作。特別是對於一個按鈕,這意味着它被按下了 –