关于java中鼠标事件

JButtonjbt=newJButton();现在要单击jbt,要分别设置左击和右击效果怎么办... JButton jbt = new JButton();
现在要单击jbt,要分别设置左击和右击效果
怎么办
展开
 我来答
erbo2008
2008-12-28 · TA获得超过309个赞
知道答主
回答量:103
采纳率:0%
帮助的人:101万
展开全部
  很简单,你只要将这个按扭设置鼠标监听就行了。
  想必你是初学者,为了让你更好的理解看一下这个程序吧
  import java.awt.BorderLayout;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  import javax.swing.JTextField;

  public class Moble extends JFrame {
  // Java实现一个简单的手机面板及数字输入功能
  private static final long serialVersionUID = 1L;
  private JTextField txtView;

  public static void main(String args[]) {
  new Moble();
  }

  public Moble() {
  setTitle("Moble");
  setBounds(100, 100, 200, 300);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  final JPanel panel = new JPanel();
  getContentPane().add(panel, BorderLayout.CENTER);
  setResizable(false);

  txtView = new JTextField();
  panel.setLayout(null);
  panel.add(txtView);

  final JButton btn1 = new JButton();
  btn1.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent arg0) {
  txtView.setText(txtView.getText().concat("1"));
  }
  });

  btn1.setText("1");
  btn1.setBounds(0, 73, 59, 23);
  panel.add(btn1);

  final JButton btn2 = new JButton();
  btn2.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent arg0) {
  txtView.setText(txtView.getText().concat("2"));
  }
  });
  btn2.setText("2");
  btn2.setBounds(65, 73, 59, 23);
  panel.add(btn2);

  final JButton btn3 = new JButton();
  btn3.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent arg0) {
  txtView.setText(txtView.getText().concat("3"));
  }
  });
  btn3.setText("3");
  btn3.setBounds(130, 73, 59, 23);
  panel.add(btn3);

  final JButton btn4 = new JButton();
  btn4.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent arg0) {
  txtView.setText(txtView.getText().concat("4"));
  }
  });
  btn4.setText("4");
  btn4.setBounds(0, 102, 59, 23);
  panel.add(btn4);

  final JButton btn5 = new JButton();
  btn5.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent arg0) {
  txtView.setText(txtView.getText().concat("5"));
  }
  });
  btn5.setText("5");
  btn5.setBounds(65, 102, 59, 23);
  panel.add(btn5);

  final JButton btn6 = new JButton();
  btn6.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent arg0) {
  txtView.setText(txtView.getText().concat("6"));
  }
  });
  btn6.setText("6");
  btn6.setBounds(130, 102, 59, 23);
  panel.add(btn6);

  final JButton btn7 = new JButton();
  btn7.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent arg0) {
  txtView.setText(txtView.getText().concat("7"));
  }
  });
  btn7.setText("7");
  btn7.setBounds(0, 131, 59, 23);
  panel.add(btn7);

  final JButton btn8 = new JButton();
  btn8.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent arg0) {
  txtView.setText(txtView.getText().concat("8"));
  }
  });
  btn8.setText("8");
  btn8.setBounds(65, 131, 59, 23);
  panel.add(btn8);

  final JButton btn9 = new JButton();
  btn9.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent arg0) {
  txtView.setText(txtView.getText().concat("9"));
  }
  });
  btn9.setText("9");
  btn9.setBounds(130, 131, 59, 23);
  panel.add(btn9);

  final JButton btn0 = new JButton();
  btn0.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent arg0) {
  txtView.setText(txtView.getText().concat("0"));
  }
  });
  btn0.setText("0");
  btn0.setBounds(68, 160, 57, 23);
  panel.add(btn0);

  txtView = new JTextField();
  txtView.setBounds(59, 24, 90, 21);
  panel.add(txtView);

  final JButton btnSend = new JButton();
  btnSend.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent arg0) {
  if (txtView.getText().indexOf("calling...") == -1) {
  txtView.setText(txtView.getText().concat("calling..."));
  } else {
  return;
  }
  }
  });
  btnSend.setText("Send");
  btnSend.setBounds(40, 189, 127, 23);
  panel.add(btnSend);

  final JButton btnclear = new JButton();
  btnclear.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent arg0) {
  txtView.setText("");
  }
  });
  btnclear.setText("Clear");
  btnclear.setBounds(40, 212, 127, 23);
  panel.add(btnclear);

  final JButton btnOff = new JButton();
  btnOff.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent arg0) {
  System.exit(0);
  }
  });
  btnOff.setText("Exit");
  btnOff.setBounds(40, 232, 127, 23);
  panel.add(btnOff);
  setVisible(true);
  }
  }
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式