Java 为什么我不能用鼠标画一个圆圈?
我正在解决一个关于创建最近点对的练习。我做的第一个动作是用我的鼠标做一个点(cricles)。但是我没有得到左边按钮的响应(只有(0,0)中的一个圆圈),其他按钮2和3工作正常。我陷入了为什么以及如何解决这个问题?任何提示或帮助都将不胜感激 代码如下:Java 为什么我不能用鼠标画一个圆圈?,java,event-handling,mouseevent,actionlistener,Java,Event Handling,Mouseevent,Actionlistener,我正在解决一个关于创建最近点对的练习。我做的第一个动作是用我的鼠标做一个点(cricles)。但是我没有得到左边按钮的响应(只有(0,0)中的一个圆圈),其他按钮2和3工作正常。我陷入了为什么以及如何解决这个问题?任何提示或帮助都将不胜感激 代码如下: import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ClosestPairOfPoints extends JFrame { //
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ClosestPairOfPoints extends JFrame {
// Create a canvas
private Circle canvas = new Circle();
public ClosestPairOfPoints() {
// Create a panel
JPanel p = new JPanel();
// Add canvas and panel
add(canvas, BorderLayout.CENTER);
// add(p);
canvas.addMouseListener(new MouseAdapter() {
@Override
// Handle mouse clicked event
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1)
canvas.createCircle();
else if (e.getButton() == MouseEvent.BUTTON2)
System.out.println("Try again with the left button");
else if (e.getButton() == MouseEvent.BUTTON3)
System.out.println("Try again with the left button");
}
});
}
public static void main(String[] args) {
JFrame frame = new ClosestPairOfPoints();
frame.setTitle("Closest pair of Ppoints");
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setVisible(true);
}
static class Circle extends JPanel { // Inner class
private int x;
private int y;
private int radius = 10; // Default circle radius
// Create a circle
public void createCircle() {
}
// paint the component
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawOval(x, y, radius, radius);
}
}
}
鼠标左键响应正常(用鼠标左键执行了
System.out.println
),但正如满是鳗鱼的气垫船所述,您的createCircle()
方法为空。那是你的问题
就实际绘制圆而言,我不会为您编写完整的代码,但我会告诉您,在计算鼠标单击时鼠标的位置时,e.getX()
和e.getY()
将非常有用
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ClosestPairOfPoints extends JFrame {
// Create a canvas
private Circle canvas = new Circle();
public ClosestPairOfPoints() {
// Add canvas and panel
add(canvas, BorderLayout.CENTER);
canvas.addMouseListener(new MouseAdapter() {
@Override
// Handle mouse clicked event
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1){
canvas.createCircle(e.getX(), e.getY());
}else if (e.getButton() == MouseEvent.BUTTON2){
System.out.println("Try again with the left button");
}else if (e.getButton() == MouseEvent.BUTTON3){
System.out.println("Try again with the left button");
}
}
});
}
public static void main(String[] args) {
JFrame frame = new ClosestPairOfPoints();
frame.setTitle("Closest pair of Ppoints");
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setVisible(true);
}
static class Circle extends JPanel { // Inner class
private int x;
private int y;
private int radius = 10; // Default circle radius
// Create a circle
public void createCircle(int x, int y) {
this.x = x;
this.y = y;
repaint();
}
// paint the component
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawOval(x, y, radius, radius);
}
}
}
现在应该可以了。您需要调用createCircle并将鼠标单击的位置传递给它,然后调用repaint,以便可以再次调用绘制组件并在正确的位置重新绘制圆
哈哈,我打字的时候好像有人发了答案。如前所述,事件对象“e”包含有关鼠标单击的信息,因此使用getX()和getY()方法,可以获得鼠标单击的x和y位置
此外,您不需要JPanel p=newjpanel();在你的代码中。。因为“canvas”已经是一个JPanel,也是您添加到JFrame中的一个
希望这有帮助您如何知道“左键”没有给您任何响应?你没有任何指示吗?你的
createCircle()
方法没有任何作用。当我执行代码时,我在(0,0)中得到圆圈,当我单击B2或B3时,我从B1中得到消息,但什么也没有。我陷入了createCircle()应该是什么样子的困境?我只需要一个提示我不知道很简单这很有帮助谢谢。现在我了解了整个pbm以及如何反思(我是一个非常新的初学者!!)。没问题,作为一个初学者很难!