当前位置: 技术问答>java相关
小问题,帮帮我?
来源: 互联网 发布时间:2015-06-11
本文导语: 我在下列代码编译时出错实在是不明白,请大家帮我明白一下. 错误: ---------- 編譯java ---------- NotHelloWorld.java:28: 需要??符。 contentpane.add(np); ^ 1 ??? Normal Termination Output completed (3 sec consu...
我在下列代码编译时出错实在是不明白,请大家帮我明白一下.
错误:
---------- 編譯java ----------
NotHelloWorld.java:28: 需要??符。
contentpane.add(np);
^
1 ???
Normal Termination
Output completed (3 sec consumed).
---------------------------------
程序:
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class NotHelloWorldPanel extends JPanel
{
public void paintComponent(Graphics g)
{
g.drawString("這是我的第二個窗口JFrame!",75,100);
}
}
class NotHelloWorldFrame extends JFrame
{
public NotHelloWorldFrame()
{
setTitle("窗口TWO");
setSize(300,300);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
Container contentpane = getContentPane();
NotHelloWorldPanel np = new NotHelloWorldPanel();
contentpane.add(np);
}
public class NotHelloWorld
{
public static void main(String[] args)
{
NotHelloWorldFrame nj = new NotHelloWorldFrame();
nj.show();
}
}
错误:
---------- 編譯java ----------
NotHelloWorld.java:28: 需要??符。
contentpane.add(np);
^
1 ???
Normal Termination
Output completed (3 sec consumed).
---------------------------------
程序:
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class NotHelloWorldPanel extends JPanel
{
public void paintComponent(Graphics g)
{
g.drawString("這是我的第二個窗口JFrame!",75,100);
}
}
class NotHelloWorldFrame extends JFrame
{
public NotHelloWorldFrame()
{
setTitle("窗口TWO");
setSize(300,300);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
Container contentpane = getContentPane();
NotHelloWorldPanel np = new NotHelloWorldPanel();
contentpane.add(np);
}
public class NotHelloWorld
{
public static void main(String[] args)
{
NotHelloWorldFrame nj = new NotHelloWorldFrame();
nj.show();
}
}
|
contentpane.add(np);
这一行不在public NotHelloWorldFrame()中,
你的public NotHelloWorldFrame()到
Container contentpane = getContentPane();
之前就结束了,
contentpane.add(np);
是非定义语句,不能出现在mothod定义之外。
这一行不在public NotHelloWorldFrame()中,
你的public NotHelloWorldFrame()到
Container contentpane = getContentPane();
之前就结束了,
contentpane.add(np);
是非定义语句,不能出现在mothod定义之外。
|
public NotHelloWorldFrame()
{
setTitle("窗口TWO");
setSize(300,300);
Container contentpane = getContentPane();
NotHelloWorldPanel np = new NotHelloWorldPanel();
contentpane.add(np);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
仅此而已
{
setTitle("窗口TWO");
setSize(300,300);
Container contentpane = getContentPane();
NotHelloWorldPanel np = new NotHelloWorldPanel();
contentpane.add(np);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
仅此而已