我希望在JFrame下的JPanel面板上画曲线,可是运行后,无法实现,我是新手,555 谢谢各位大家给出错误,在线等
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class drawlines extends JFrame
{
public drawlines()
{
JFrame frame = new JFrame();
JPanel contentPane = new JPanel();
frame.setContentPane(contentPane);
frame.setSize(600,600);
frame.setVisible(true);
// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.red);
int temp[]=new int[1000];
for(int i=0;i<10;i++)
{
//每秆邮1秒,利用两点的坐标画出一条线
try
{
Thread.currentThread().sleep(1 * 1000);
temp[i]=1+(int)(Math.random()*10);
if(i==0)
g.drawLine(0,0,i*20,temp[i]*30);
else
g.drawLine((i-1)*20,temp[i-1]*30,i*20,temp[i]*30);
}
catch(InterruptedException e) {}
}
}
public static void main(String args[])
{
drawlines application = new drawlines();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
import javax.swing.*;
import java.awt.event.*;
public class drawlines extends JFrame
{
public drawlines()
{
//JFrame frame = new JFrame();
JPanel contentPane = new JPanel();
//jframe,改为this
this.setContentPane(contentPane);
this.setSize(600,600);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.red);
int temp[]=new int[1000];
for(int i=0;i <10;i++)
{
//每秆邮1秒,利用两点的坐标画出一条线
try
{
Thread.currentThread().sleep(1 * 1000);
temp[i]=1+(int)(Math.random()*10);
if(i==0)
g.drawLine(0,0,i*20,temp[i]*30);
else
g.drawLine((i-1)*20,temp[i-1]*30,i*20,temp[i]*30);
}
catch(InterruptedException e){}
}
} public static void main(String args[])
{
drawlines application = new drawlines();
// application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}还得写些repaint函数。