怎样将JPanel置空? 本帖最后由 t13977168314 于 2013-06-04 22:01:46 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 static class Point extends JPanel { private int x = 0; private int y = 0; private String str = " "; private boolean clear; public Point() { this.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { clear = false; x = e.getX(); y = e.getY(); str = "(" + x + "," + y + ")"; repaint(); } public void mouseReleased(MouseEvent e) { // 当鼠标释放时清除显示的坐标,即显示为空面板 clear = true; repaint(); } }); } protected void paintComponent(Graphics g) { if (!clear) { super.paintComponents(g); g.drawString(str, x, y); } else { g.clearRect(0, 0, getWidth(), getHeight()); } }}重绘一下^_^ 1、你代码中 super.paintComponents(g); 改为 super.paintComponent(g); 2、至于两个函数的区别,你查API吧。3、加一个标示,用于处理repaint import java.awt.Graphics;import java.awt.event.*;import javax.swing.*;public class MouseColor extends JFrame{public MouseColor(){Point p=new Point();add(p);}public static void main(String[] args){MouseColor frame=new MouseColor();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setVisible(true);frame.setLocationRelativeTo(null);frame.setSize(500,400);}static class Point extends JPanel{private int x=0;private int y=0;private String str=" ";boolean blflag = false;public Point(){this.addMouseListener(new MouseAdapter(){public void mousePressed(MouseEvent e){x=e.getX();y=e.getY();str="("+x+","+y+")";blflag = true;repaint();}public void mouseReleased(MouseEvent e){//当鼠标释放时清除显示的坐标,即显示为空面板 blflag = false; repaint();}});}protected void paintComponent(Graphics g){super.paintComponent(g);if (blflag){ g.drawString(str, x, y);}}}} public void mouseReleased(MouseEvent e){//当鼠标释放时清除显示的坐标,即显示为空面板setVisibleFalse();//加一句这个}//加个方法。功能是让JPanel不显示public void setVisibleFalse(){ this.setVisible(false);} 100 分 java SQL语句拼接问题 谁有javatalk.jar的下载地址 initialized新手错误 编程以及java新手,请问如何编写这个程序 一个弱问题 飞利浦官方网站提供的免费短信服务! 这个也能通过编译,怎么解释 jre有何用? unix下,装有oracle8.1.5和jdk........ java 入门初级问题?请HELP! 如何用java代码 获取打印机列表 文件上传,不报错,但是服务器的文件内容为空
private int x = 0;
private int y = 0;
private String str = " ";
private boolean clear; public Point() {
this.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
clear = false;
x = e.getX();
y = e.getY();
str = "(" + x + "," + y + ")";
repaint();
} public void mouseReleased(MouseEvent e) {
// 当鼠标释放时清除显示的坐标,即显示为空面板
clear = true;
repaint();
}
});
} protected void paintComponent(Graphics g) {
if (!clear) {
super.paintComponents(g);
g.drawString(str, x, y);
}
else {
g.clearRect(0, 0, getWidth(), getHeight());
}
}
}
重绘一下^_^
2、至于两个函数的区别,你查API吧。
3、加一个标示,用于处理repaint
import java.awt.Graphics;
import java.awt.event.*;
import javax.swing.*;public class MouseColor extends JFrame{
public MouseColor(){
Point p=new Point();
add(p);}
public static void main(String[] args){
MouseColor frame=new MouseColor();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setSize(500,400);
}static class Point extends JPanel{
private int x=0;
private int y=0;
private String str=" ";
boolean blflag = false;
public Point(){
this.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
x=e.getX();
y=e.getY();
str="("+x+","+y+")";
blflag = true;
repaint();
}
public void mouseReleased(MouseEvent e){
//当鼠标释放时清除显示的坐标,即显示为空面板
blflag = false;
repaint();
}});
}
protected void paintComponent(Graphics g){
super.paintComponent(g);
if (blflag)
{
g.drawString(str, x, y);
}}
}
}
//当鼠标释放时清除显示的坐标,即显示为空面板
setVisibleFalse();//加一句这个
}
//加个方法。功能是让JPanel不显示
public void setVisibleFalse(){
this.setVisible(false);
}