我正在解决要创建最接近的点的练习。我正在做的第一个坐着是试图用鼠标提出点(cricle)。但是我对左键没有响应((0,0中的一个圆),其他按钮2和3的工作正常。我陷入了为什么以及如何解决?任何提示或帮助都可以欣赏。
这是代码:
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);
}
}
}
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并将其传递给MouseClick的位置,然后调用重新粉刷,以便可以再次调用油漆组件,然后圆圈重新绘制在正确的位置。
哈哈,似乎另一个人在我打字时发布了答案。如前所述,事件对象" e"包含有关鼠标单击的信息,因此使用getx()和gety()方法,您可以获得鼠标的x和y位置。
另外,您不需要jpanel p = new jpanel();在您的代码中。
希望这有帮助
左鼠标按钮正常响应(使用左鼠标按钮做了System.out.println
),但是,如Hovercraft充满鳗鱼所述,您的createCircle()
方法是空的。那是你的问题。
从实际绘制圆圈的角度来看,我不会为您编写整个代码,但是我会告诉您,当弄清楚鼠标单击时鼠标在哪里时,e.getX()
和e.getY()
会派上用场。/p>