我正在解决一个关于创造一对最近点的练习。我正在做的第一个尝试是用鼠标做点(cricles)。但我对左键没有任何响应(在(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);
}
}
}
答案 0 :(得分:1)
鼠标左键正常响应(用鼠标左键做了System.out.println
),但是,正如Hovercraft Full of Eels所说,你的createCircle()
方法是空的。那是你的问题。
就实际绘制圆圈而言,我不会为您编写整个代码,但我会告诉您e.getX()
和e.getY()
在找出鼠标的位置时会派上用场是鼠标点击发生的时间。
答案 1 :(得分:1)
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的位置,然后调用repaint以便再次调用paint组件并将圆圈重新绘制在正确的位置。
哈哈,在我打字的时候,似乎另一个人发布了答案。如上所述,事件对象“e”包含有关鼠标单击的信息,因此使用getX()和getY()方法,您可以获得鼠标单击的x和y位置。此外,您不需要JPanel p = new JPanel();在你的代码中..因为“canvas”已经是一个JPanel和你添加到JFrame的那个。
希望这有帮助