学习head fierst java中在eclipse输入以下代码,出来的窗口里什么也没有,菜鸟求助。
importjavax.swing.*;importjava.awt.*;publicclassSimpleAnimation{intx=70;inty=70;publi...
import javax.swing.*;
import java.awt.*;
public class SimpleAnimation {
int x=70;
int y=70;
public static void main(String[] args) {
SimpleAnimation gui=new SimpleAnimation();
gui.go();
}
public void go(){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyDrawPanel drawPanel = new MyDrawPanel();
frame.getContentPane().add(drawPanel);
frame.setSize(300, 300);
frame.setVisible(true);
for(int i=0;i<130;i++){
x++;
y++;
drawPanel.repaint();
try{
Thread.sleep(50);
}catch(Exception ex){}
}
}
class MyDrawPanel extends JPanel{
public void panitComponent(Graphics g){
g.setColor(Color.green);
g.fillOval(x, y,40,40);
}
}
} 展开
import java.awt.*;
public class SimpleAnimation {
int x=70;
int y=70;
public static void main(String[] args) {
SimpleAnimation gui=new SimpleAnimation();
gui.go();
}
public void go(){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyDrawPanel drawPanel = new MyDrawPanel();
frame.getContentPane().add(drawPanel);
frame.setSize(300, 300);
frame.setVisible(true);
for(int i=0;i<130;i++){
x++;
y++;
drawPanel.repaint();
try{
Thread.sleep(50);
}catch(Exception ex){}
}
}
class MyDrawPanel extends JPanel{
public void panitComponent(Graphics g){
g.setColor(Color.green);
g.fillOval(x, y,40,40);
}
}
} 展开
展开全部
drawPanel其实已经显示出来了,我给drawPanel设置了一个背景色,你可以看到效果。你没有调用panitComponent()这个函数,也就是说没有画出来一个绿色的框框。
我改了两行你看看。如果对你有帮助,记得给好评O(∩_∩)O~
import javax.swing.*;
import java.awt.*;
public class SimpleAnimation {
int x=70;
int y=70;
public static void main(String[] args) {
SimpleAnimation gui=new SimpleAnimation();
gui.go();
}
public void go(){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyDrawPanel drawPanel = new MyDrawPanel();
drawPanel.setBackground(Color.green);/** 为drawPanel设置背景色 */
frame.getContentPane().add(drawPanel);
frame.setSize(300, 300);
frame.setVisible(true);
for(int i=0;i<130;i++){
x++;
y++;
drawPanel.repaint();
try{
Thread.sleep(50);
}catch(Exception ex){}
}
}
class MyDrawPanel extends JPanel{
/** 加上这句话消除警告 */
private static final long serialVersionUID = 1L;
public void panitComponent(Graphics g){
g.setColor(Color.green);
g.fillOval(x, y,40,40);
}
}
}
我改了两行你看看。如果对你有帮助,记得给好评O(∩_∩)O~
import javax.swing.*;
import java.awt.*;
public class SimpleAnimation {
int x=70;
int y=70;
public static void main(String[] args) {
SimpleAnimation gui=new SimpleAnimation();
gui.go();
}
public void go(){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyDrawPanel drawPanel = new MyDrawPanel();
drawPanel.setBackground(Color.green);/** 为drawPanel设置背景色 */
frame.getContentPane().add(drawPanel);
frame.setSize(300, 300);
frame.setVisible(true);
for(int i=0;i<130;i++){
x++;
y++;
drawPanel.repaint();
try{
Thread.sleep(50);
}catch(Exception ex){}
}
}
class MyDrawPanel extends JPanel{
/** 加上这句话消除警告 */
private static final long serialVersionUID = 1L;
public void panitComponent(Graphics g){
g.setColor(Color.green);
g.fillOval(x, y,40,40);
}
}
}
追问
抱歉,回来晚了,在内部类里面填充了一个绿色的圆,再用上for循环,做出来应该是一个圆从左上角到右下角的一个小动画,但是出来没东西,只有窗口。
追答
/*MyDrawPanel这个类继承Applet类就ok了,记得给好评O(∩_∩)O~*/
import javax.swing.*;
import java.applet.Applet;
import java.awt.*;
public class SimpleAnimation {
int x=70;
int y=70;
public static void main(String[] args) {
SimpleAnimation gui=new SimpleAnimation();
gui.go();
}
public void go(){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyDrawPanel drawPanel = new MyDrawPanel();
frame.getContentPane().add(drawPanel);
frame.setSize(300, 300);
frame.setVisible(true);
for(int i=0;i<130;i++){
x++;
y++;
drawPanel.repaint();
try{
Thread.sleep(50);
}catch(Exception ex){}
}
}
class MyDrawPanel extends Applet{
/** 加上这句话消除警告 */
private static final long serialVersionUID = 1L;
public void paint(Graphics g){
g.setColor(Color.green);
g.fillOval(x, y,40,40);
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询