import java.awt.*;
import java.awt.event.*;/**
* Sample application using Frame.
*
* @author
* @version 1.00 05/12/29
*/
public class StringTestFrame extends Frame {
char charArr[];
public void init()
{
charArr=new char[12];
charArr[0]='你';
charArr[1]='在';
charArr[2]='干';
charArr[3]='什';
charArr[4]='么';
charArr[5]='?';
charArr[6]='我';
charArr[7]='在';
charArr[8]='学';
charArr[9]='习';
charArr[10]='呢';
charArr[11]='!';
}
public void paint(Graphics g)
{
g.drawChars(charArr,0,12,5,20);
g.drawChars(charArr,0,6,5,40);
g.drawChars(charArr,6,6,5,60);
g.drawString("注意休息!不要太累",5,100);
}
/**
* The constructor.
*/
public StringTestFrame() {
MenuBar menuBar = new MenuBar();
Menu menuFile = new Menu();
MenuItem menuFileExit = new MenuItem();
menuFile.setLabel("File");
menuFileExit.setLabel("Exit");
// Add action listener.for the menu button
menuFileExit.addActionListener
(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
StringTestFrame.this.windowClosed();
}
}
);
menuFile.add(menuFileExit);
menuBar.add(menuFile);
setTitle("StringTest");
setMenuBar(menuBar);
setSize(new Dimension(400, 400));
// Add window listener.
this.addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
StringTestFrame.this.windowClosed();
}
}
);
}
/**
* Shutdown procedure when run as an application.
*/
protected void windowClosed() {
// TODO: Check if it is safe to close the application
// Exit application.
System.exit(0);
}
}public class FontInfoTest {
public static void main(String[] args) {
// Create application frame.
FontInfoTestFrame frame = new FontInfoTestFrame();
frame.setSize(0,400);
// Show frame
frame.setVisible(true);
}
}
编译可以通过但是运行后出现空指针异常
在 g.drawChars(charArr,0,12,5,20);上哪位高手能告诉我一下
import java.awt.event.*;/**
* Sample application using Frame.
*
* @author
* @version 1.00 05/12/29
*/
public class StringTestFrame extends Frame {
char charArr[];
public void init()
{
charArr=new char[12];
charArr[0]='你';
charArr[1]='在';
charArr[2]='干';
charArr[3]='什';
charArr[4]='么';
charArr[5]='?';
charArr[6]='我';
charArr[7]='在';
charArr[8]='学';
charArr[9]='习';
charArr[10]='呢';
charArr[11]='!';
}
public void paint(Graphics g)
{
g.drawChars(charArr,0,12,5,20);
g.drawChars(charArr,0,6,5,40);
g.drawChars(charArr,6,6,5,60);
g.drawString("注意休息!不要太累",5,100);
}
/**
* The constructor.
*/
public StringTestFrame() {
MenuBar menuBar = new MenuBar();
Menu menuFile = new Menu();
MenuItem menuFileExit = new MenuItem();
menuFile.setLabel("File");
menuFileExit.setLabel("Exit");
// Add action listener.for the menu button
menuFileExit.addActionListener
(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
StringTestFrame.this.windowClosed();
}
}
);
menuFile.add(menuFileExit);
menuBar.add(menuFile);
setTitle("StringTest");
setMenuBar(menuBar);
setSize(new Dimension(400, 400));
// Add window listener.
this.addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
StringTestFrame.this.windowClosed();
}
}
);
}
/**
* Shutdown procedure when run as an application.
*/
protected void windowClosed() {
// TODO: Check if it is safe to close the application
// Exit application.
System.exit(0);
}
}public class FontInfoTest {
public static void main(String[] args) {
// Create application frame.
FontInfoTestFrame frame = new FontInfoTestFrame();
frame.setSize(0,400);
// Show frame
frame.setVisible(true);
}
}
编译可以通过但是运行后出现空指针异常
在 g.drawChars(charArr,0,12,5,20);上哪位高手能告诉我一下
解决方案 »
- eclipse 生成jar文件不能运行 求解
- 验证"验证码"并提交,来者有分!!!
- 关于String和数组
- 一个滚动条的代码,设置了长度是1到90,为什么运行出来实际长度是1到80?
- 如何监听JTable的选中行? 此时的JTable实例已经被加入TabbedPane中
- 求救高手!!在线苦等。。。。。
- 如何在用swing设定图片背景?
- 我的配置为什么老出现Exception in thread "main" java.lang.NoClassDefFoundError: test
- 打包
- 自学了Java好几个月,不知道可否找到Java的工作呢?
- 关于获取文件属性的问题
- 如何在程序种调用其他程序啊
你在 public void paint(Graphics g)这个方法中,加一句调用
init()方法
public void paint(Graphics g){
init();
g.drawChars(....
...
...
}就可以了。。另外,你代码中的FontInfoTestFrame 照你的意思应该是StringTestFrame。。好运。。