Java扫雷如何捕捉右键Button单击事件
我想右键单击一个button,然后该button上面显示文字“H”。如何能办到?我需要可运行的程序。...
我想右键单击一个button,然后该button上面显示文字“H”。如何能办到?
我需要可运行的程序。 展开
我需要可运行的程序。 展开
1个回答
展开全部
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class A extends JFrame implements MouseListener {
private JButton btn = new JButton("右键点我");
public A() {
this.setTitle("test");
this.setSize(300, 200);
this.add(btn);
btn.addMouseListener(this);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
}
public static void main(String[] args) {
new A();
}
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {//右键点击
btn.setText("H");
}
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
}
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class A extends JFrame implements MouseListener {
private JButton btn = new JButton("右键点我");
public A() {
this.setTitle("test");
this.setSize(300, 200);
this.add(btn);
btn.addMouseListener(this);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
}
public static void main(String[] args) {
new A();
}
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {//右键点击
btn.setText("H");
}
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询