Java – 重新绘制JPanel会出错
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java – 重新绘制JPanel会出错,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3366字,纯文字阅读大概需要5分钟。
内容图文
![Java – 重新绘制JPanel会出错](/upload/InfoBanner/zyjiaocheng/704/24e5d49c2b1545e0b9f028f54be20188.jpg)
我是Java的初学者,我正在尝试创建一个绘制矩形的应用程序,无论光标位于何处.我已经完成了所有工作,但是我无法使用mouseMoved(MouseEvent)方法重新绘制JPanel.没有重绘,矩形只绘制一次,就是这样.使用重绘,它编译得很好,但是当我运行它时,每次移动鼠标时,我都会得到这个大的“线程异常”AWT-EventQueue-0“java.lang.NullPointerException”错误.
那么,任何人都可以帮我解决这个问题吗?
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class Game extends JPanel implements MouseMotionListener
{
public static void main(String[] args) {
new Game().game();
}
JPanel panel;
JButton button2;
JButton button;
public void game() {
JPanel panel = new Game();
button = new JButton("Ok");
panel.setLayout(new FlowLayout());
panel.add(button);
button2 = new JButton("Cancel");
JFrame frame = new JFrame("Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setResizable(false);
frame.add(panel);
frame.setVisible(true);
panel.addMouseMotionListener(this);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
PointerInfo a = MouseInfo.getPointerInfo();
Point b = a.getLocation();
int x = (int) b.getX();
int y = (int) b.getY();
g.fillRect(x,y,100,100);
}
public void mouseMoved(MouseEvent evt) {
panel.repaint; //This is the line of code that I need help with. Thanks!
}
public void mouseDragged(MouseEvent evt) {}
}
解决方法:
希望代码示例中的注释能够告诉您代码中的错误:-),否则总有理由提出您的疑问……
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Game extends JPanel {
/*
* Now x and y are instance variables,
* whose values you can change at each
* MouseMove Event, and call repaint()
* to see the effects
*/
private int x;
private int y;
private MouseAdapter mouseActions =
new MouseAdapter() {
@Override
public void mouseMoved(MouseEvent me) {
/*
* Now as the Mouse moves, we simply
* updating the instance variables,
* i.e. x and y to the new values
* of the Mouse Location and calling
* repaint() to draw the rectangle.
* Since this class (Game) extends JPanel,
* hence all the functions of the JPanel
* belongs to this class, hence like
* as we call any other method of this
* class, without using the object,
* we can call repaint, likewise.
*/
x = me.getX();
y = me.getY();
repaint();
}
};
/*
* This JPanel panel is unnecessary in
* this case, since the class itself
* extends JPanel, hence you can use
* this (keyword) to access the instance
*/
//JPanel panel;
// Not needed for this case.
//JButton button2;
//JButton button;
public void game() {
JFrame frame = new JFrame("Game");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setResizable(false);
addMouseMotionListener(mouseActions);
/*
* Here this means the instance
* of the current class
*/
frame.add(this);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
/*
* While overriding methods of the
* super class, try to keep the
* ACCESS SPECIFIER, as close to
* the original thingy as possible
* In this case, it's protected
* and not public
*/
@Override
protected void paintComponent(Graphics g) {
/*
* Do not perform calculation in this method
* atleast.
*/
super.paintComponent(g);
g.fillRect(x, y, 100, 100);
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
@Override
public void run() {
new Game().game();
}
};
EventQueue.invokeLater(runnable);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(500, 500);
}
}
内容总结
以上是互联网集市为您收集整理的Java – 重新绘制JPanel会出错全部内容,希望文章能够帮你解决Java – 重新绘制JPanel会出错所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。