gpt4 book ai didi

java - 我怎样才能给 "MouseListener"一个 Action 来在java中绘制一个形状?

转载 作者:行者123 更新时间:2023-12-02 05:55:43 26 4
gpt4 key购买 nike

这是我的代码我想在我单击的位置绘制矩形,但它没有绘制任何东西:(,我不知道如何将操作赋予“MouseListener”

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Transparenttxom extends JPanel implements MouseListener {

Graphics g=null;

public void init(){
Transparenttxom panel=new Transparenttxom();
panel.addMouseListener(this);
}//end init
//********************************************************************
public void paint(Graphics g){
}
//********************************************************************
public void mouseClicked(MouseEvent e){
int mousex=e.getX();
int mousey=e.getY();

g.drawRect(20,20,mousex,mousey);

}//end mouseclicked method
//********************************************************************
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
public void mousePressed(MouseEvent e){
}
public void mouseReleased(MouseEvent e){
}
//********************************************************************
public static void main(String[] args) {
Transparenttxom panel=new Transparenttxom();
JFrame frame = new JFrame("java lover");
frame.add(panel);
frame.setSize(300, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}
}

感谢您的帮助。

最佳答案

我将从 Graphics g=null; 的事实开始,并且由于您从未为其分配过任何内容,因此它很可能仍为 null

然后我会转到永远不会调用 init 的地方,但你的 init 方法让我害怕......

public void init(){
Transparenttxom panel=new Transparenttxom();
panel.addMouseListener(this);
}//end init

为什么要创建 Transparenttxom 的新实例?简单调用 addMouseListener(this)...

但即便如此,这...

public void paint(Graphics g){
}

意味着没有任何东西可以被绘制...

首先查看 Performing Custom Painting了解更多详情

您应该重写paintComponent,而不是重写paint,并确保在您自己进行任何绘制之前调用super.paintComponent

在您的 mouseClicked 事件中,您应该定义您想要绘制的内容,并简单地调用 repaint 这将触发一个绘制事件,该事件最终将调用您的 paintComponent 你在哪里画东西。

更新了基本示例

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Transparenttxom extends JPanel implements MouseListener {

private Point mousePoint;

public Transparenttxom() {
addMouseListener(this);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (mousePoint != null) {
g.drawRect(20, 20, mousePoint.x - 20, mousePoint.y - 20);
}
}

@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}



public void mouseClicked(MouseEvent e) {
mousePoint = e.getPoint();
repaint();
}//end mouseclicked method

public void mouseEntered(MouseEvent e) {
}

public void mouseExited(MouseEvent e) {
}

public void mousePressed(MouseEvent e) {
}

public void mouseReleased(MouseEvent e) {
}
//********************************************************************

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new Transparenttxom());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}

关于java - 我怎样才能给 "MouseListener"一个 Action 来在java中绘制一个形状?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23101290/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com