有人可以帮我在我的作业中提出这个问题吗?“制作一个通过显示一条指示用户单击位置的消息来响应鼠标单击的程序。”
这是我的代码
package testprogh;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Testprogh extends MouseInputAdapter {
public static void main(String[] args) {
JFrame frame=new JFrame();
JButton btn=new JButton("RIGHT");
JButton btn2=new JButton("LEFT");
JButton btn3=new JButton("CENTER");
JButton btn4=new JButton("DOWN");
JButton btn5=new JButton("UP");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(btn, BorderLayout.EAST);
frame.add(btn2, BorderLayout.WEST);
frame.add(btn3, BorderLayout.CENTER);
frame.add(btn4, BorderLayout.SOUTH);
frame.add(btn5, BorderLayout.NORTH);
frame.setSize(400,400);
frame.setVisible(true);
Testprogh ha =new Testprogh();
frame.addMouseListener(ha);
frame.addMouseMotionListener(ha);
}
public void mouseClicked(MouseEvent event){
JOptionPane.showMessageDialog(null, "test");
}
}
但是这里的问题是它没有显示消息对话框
答案 0 :(得分:0)
import javax.swing.*;
import javax.swing.event.MouseInputAdapter;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class TestProgh extends MouseInputAdapter {
public static void main(String[] args) {
JFrame frame = new JFrame();
JButton btn = new JButton("RIGHT");
JButton btn2 = new JButton("LEFT");
JButton btn3 = new JButton("CENTER");
JButton btn4 = new JButton("DOWN");
JButton btn5 = new JButton("UP");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(btn, BorderLayout.EAST);
frame.add(btn2, BorderLayout.WEST);
frame.add(btn3, BorderLayout.CENTER);
frame.add(btn4, BorderLayout.SOUTH);
frame.add(btn5, BorderLayout.NORTH);
frame.setSize(400, 400);
frame.setVisible(true);
TestProgh ha = new TestProgh();
frame.addMouseListener(ha);
frame.addMouseMotionListener(ha);
btn.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent mouseEvent) {
System.out.println("Mouse clicked on coordinates x : " + mouseEvent.getX() + " y : " + mouseEvent.getY());
}
@Override
public void mousePressed(MouseEvent mouseEvent) {
System.out.println("Mouse button pressed " + mouseEvent.getButton() + "on coordinates x : " + mouseEvent.getX() + " y : " + mouseEvent.getY());
}
@Override
public void mouseReleased(MouseEvent mouseEvent) {
System.out.println("Mouse button released" + mouseEvent.getButton() + "on coordinates x : " + mouseEvent.getX() + " y : " + mouseEvent.getY());
}
@Override
public void mouseEntered(MouseEvent mouseEvent) {
// I don't know those
System.out.println("Mouse entered in button area " + mouseEvent.getButton() + "on coordinates x : " + mouseEvent.getX() + " y : " + mouseEvent.getY());
}
@Override
public void mouseExited(MouseEvent mouseEvent) {
System.out.println("Mouse exited button area" + mouseEvent.getButton() + "on coordinates x : " + mouseEvent.getX() + " y : " + mouseEvent.getY());
}
});
}
public void mouseClicked(MouseEvent event) {
JOptionPane.showMessageDialog(null, "test");
}
}
答案 1 :(得分:0)
您发布的代码将不会显示JOptionPane消息对话框,因为您的框架已装满按钮,因此当您单击任何按钮时,将不会触发mouseClicked事件,因为它已添加到JFrame中。因此,您需要将MouseAdapter添加到如下所示的按钮。
btn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
JOptionPane.showMessageDialog(null, "test");
}
});
或
Testprogh ha =new Testprogh();
btn.addMouseListener(ha);