当前位置: 技术问答>java.exe以及相关程序环境运行时报内存错误,请教各位什么原因?
iis7站长之家
这个程序错在哪里?原因何在?请指导!关键字[java][swing][event]
来源: 互联网 发布时间:2015-04-20
本文导语: 程序如下: /* * * */ import java.awt.*; import java.awt.event.*; import javax.swing.*; /***********************************************************************/ class SketchPanel extends JPanel implements KeyListener { public SketchPanel() { addKeyListener...
程序如下:
/*
*
*
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/***********************************************************************/
class SketchPanel extends JPanel implements KeyListener
{
public SketchPanel()
{
addKeyListener(this);
}
public void keyPressed(KeyEvent evt)
{
int keyCode=evt.getKeyCode();
int d;
if (evt.isShiftDown())
{
d=5;
}
else
{
d=1;
}
if (keyCode==KeyEvent.VK_LEFT) add(-d,0);
else if (keyCode==KeyEvent.VK_RIGHT) add(d,0);
else if (keyCode==KeyEvent.VK_UP) add(0,-d);
else if (keyCode==KeyEvent.VK_DOWN) add(0,d);
}
public void keyReleased(KeyEvent evt) {}
public void keyTyped(KeyEvent evt)
{
char keyChar=evt.getKeyChar();
int d;
if(Character.isUpperCase(keyChar))
{
d=5;
keyChar=Character.toLowerCase(keyChar);
}
else
{
d=1;
}
if (keyChar=='h') add(-d,0);
else if (keyChar=='l') add(d,0);
else if (keyChar=='k') add(0,-d);
else if (keyChar=='j') add(0,d);
}
public boolean isFocusTraversable()
{
return true;
}
public void add(int dx,int dy)
{
end.x+=dx;
end.y+=dy;
Graphics g=getGraphics();
g.drawLine(start.x,start.y,end.x,end.y);
g.dispose();
start.x=end.x;
start.y=end.y;
}
private Point start =new Point(100,100);
private Point end=new Point(100,100);
}
/***********************************************************************/
class closeWindow extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
/***********************************************************************/
class SketchFrame extends JFrame
{
public SketchFrame()
{
setTitle("sketch");
setSize(600,400);
closeWindow x=new closeWindow();
addWindowListener(x);
/*
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
*/
Container contentPane=getContentPane();
contentPane.add(new SketchPanel());
}
}
/***********************************************************************/
public class Sketch
{
public static void main(String [] args)
{
JFrame frame=new JFrame();
frame.show();
}
}
/***********************************************************************/
编译出下面提示:
--------------------Configuration: j2sdk1.4 --------------------
Note: C:javaxxSketch.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
Process completed.
请各位指点一下,不是我小气,而是我太穷!实在是想不出是哪里出错了:)
/*
*
*
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/***********************************************************************/
class SketchPanel extends JPanel implements KeyListener
{
public SketchPanel()
{
addKeyListener(this);
}
public void keyPressed(KeyEvent evt)
{
int keyCode=evt.getKeyCode();
int d;
if (evt.isShiftDown())
{
d=5;
}
else
{
d=1;
}
if (keyCode==KeyEvent.VK_LEFT) add(-d,0);
else if (keyCode==KeyEvent.VK_RIGHT) add(d,0);
else if (keyCode==KeyEvent.VK_UP) add(0,-d);
else if (keyCode==KeyEvent.VK_DOWN) add(0,d);
}
public void keyReleased(KeyEvent evt) {}
public void keyTyped(KeyEvent evt)
{
char keyChar=evt.getKeyChar();
int d;
if(Character.isUpperCase(keyChar))
{
d=5;
keyChar=Character.toLowerCase(keyChar);
}
else
{
d=1;
}
if (keyChar=='h') add(-d,0);
else if (keyChar=='l') add(d,0);
else if (keyChar=='k') add(0,-d);
else if (keyChar=='j') add(0,d);
}
public boolean isFocusTraversable()
{
return true;
}
public void add(int dx,int dy)
{
end.x+=dx;
end.y+=dy;
Graphics g=getGraphics();
g.drawLine(start.x,start.y,end.x,end.y);
g.dispose();
start.x=end.x;
start.y=end.y;
}
private Point start =new Point(100,100);
private Point end=new Point(100,100);
}
/***********************************************************************/
class closeWindow extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
/***********************************************************************/
class SketchFrame extends JFrame
{
public SketchFrame()
{
setTitle("sketch");
setSize(600,400);
closeWindow x=new closeWindow();
addWindowListener(x);
/*
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
*/
Container contentPane=getContentPane();
contentPane.add(new SketchPanel());
}
}
/***********************************************************************/
public class Sketch
{
public static void main(String [] args)
{
JFrame frame=new JFrame();
frame.show();
}
}
/***********************************************************************/
编译出下面提示:
--------------------Configuration: j2sdk1.4 --------------------
Note: C:javaxxSketch.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
Process completed.
请各位指点一下,不是我小气,而是我太穷!实在是想不出是哪里出错了:)
|
to 楼主:
我没有看程序(今天感冒,极度不爽)
不过你的问题,肯定是用了JDK1.4已经deprecated掉的方法或者属性
具体的细节,你查了1.4 API文档后就一清二楚了:)
我没有看程序(今天感冒,极度不爽)
不过你的问题,肯定是用了JDK1.4已经deprecated掉的方法或者属性
具体的细节,你查了1.4 API文档后就一清二楚了:)
|
import javax.swing.event.*;
试试
试试
|
把你的isFocusTraversable()改成isFocusable()试试~
祝你好运~
祝你好运~
|
看看api
|
Sketch.java:57: warning: isFocusTraversable() in java.awt.Component has been deprecated
public boolean isFocusTraversable()
^
1 warning
这里时指在jdk1.4中,java.awt.Component已经不建议使用的了
下面时从jdk1.4中对此的解释
isFocusTraversable
public boolean isFocusTraversable()Deprecated. As of 1.4, replaced by isFocusable().
Returns whether this Component can become the focus owner.
Returns:
true if this Component is focusable; false otherwise
Since:
JDK1.1
public boolean isFocusTraversable()
^
1 warning
这里时指在jdk1.4中,java.awt.Component已经不建议使用的了
下面时从jdk1.4中对此的解释
isFocusTraversable
public boolean isFocusTraversable()Deprecated. As of 1.4, replaced by isFocusable().
Returns whether this Component can become the focus owner.
Returns:
true if this Component is focusable; false otherwise
Since:
JDK1.1
|
不是出错,是说你的程序用到了一些javasdk1.4中
不再建议使用的方法,你用
javac -deprecation xxx.java
就会给出详细的信息
不再建议使用的方法,你用
javac -deprecation xxx.java
就会给出详细的信息