当前位置: 技术问答>java相关
错!错!如何把char类型转化成string类型的输出,帮忙调试一下~
来源: 互联网 发布时间:2015-02-01
本文导语: import java.awt.*; import java.awt.event.*; import javax.swing.*; class XiamPanel extends JPanel implements KeyListener { public XiamPanel() { addKeyListener(this);} public void keyPressed(KeyEvent evt) { } public void keyReleased(KeyEvent evt) ...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class XiamPanel extends JPanel implements KeyListener
{ public XiamPanel()
{ addKeyListener(this);}
public void keyPressed(KeyEvent evt)
{ }
public void keyReleased(KeyEvent evt)
{}
public void keyTyped(KeyEvent evt)
{ char keyChar= evt.getKeyChar();
String s=keyChar.toString();
showkey(s);
}
public boolean isFocusTraversable() { return true; }
public void showkey(String k)
{
Graphics g = getGraphics();
g.drawString(k);
g.dispose();
}
}
class XiamFrame extends JFrame
{ public XiamFrame()
{ setTitle("Xiam");
setSize(300, 200);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
} );
Container contentPane = getContentPane();
contentPane.add(new XiamPanel());
}
}
public class Xiam
{ public static void main(String[] args)
{ JFrame frame = new XiamFrame();
frame.show();
}
}
import java.awt.event.*;
import javax.swing.*;
class XiamPanel extends JPanel implements KeyListener
{ public XiamPanel()
{ addKeyListener(this);}
public void keyPressed(KeyEvent evt)
{ }
public void keyReleased(KeyEvent evt)
{}
public void keyTyped(KeyEvent evt)
{ char keyChar= evt.getKeyChar();
String s=keyChar.toString();
showkey(s);
}
public boolean isFocusTraversable() { return true; }
public void showkey(String k)
{
Graphics g = getGraphics();
g.drawString(k);
g.dispose();
}
}
class XiamFrame extends JFrame
{ public XiamFrame()
{ setTitle("Xiam");
setSize(300, 200);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
} );
Container contentPane = getContentPane();
contentPane.add(new XiamPanel());
}
}
public class Xiam
{ public static void main(String[] args)
{ JFrame frame = new XiamFrame();
frame.show();
}
}
|
两个错误
1、g.drawString(k)
应该是g.drawString(k,x,y)
2、keyChar.toString()
char是基本类型,不是类,所以keyChar不是对象,不能执行toString方法
可以这样:
char[] keyChar= {evt.getKeyChar()};
String s=new String(keyChar);
1、g.drawString(k)
应该是g.drawString(k,x,y)
2、keyChar.toString()
char是基本类型,不是类,所以keyChar不是对象,不能执行toString方法
可以这样:
char[] keyChar= {evt.getKeyChar()};
String s=new String(keyChar);
|
转换的方法不对,应该是
public void keyTyped(KeyEvent evt)
{
char keyChar= evt.getKeyChar();
//String s=keyChar.toString();错误!
String s=String.valueOf(keyChar);
showkey(s);
}
public void keyTyped(KeyEvent evt)
{
char keyChar= evt.getKeyChar();
//String s=keyChar.toString();错误!
String s=String.valueOf(keyChar);
showkey(s);
}
|
public void keyTyped(KeyEvent evt)
public void keyTyped(KeyEvent evt)
{ char keyChar= evt.getKeyChar();
String s=keyChar.toString();
/*char是基本类型,不是object,哪有方法啊?改为:
String s = String.valueOf(keyChar);
*/
showkey(s);
}
public void keyTyped(KeyEvent evt)
{ char keyChar= evt.getKeyChar();
String s=keyChar.toString();
/*char是基本类型,不是object,哪有方法啊?改为:
String s = String.valueOf(keyChar);
*/
showkey(s);
}