当前位置: 代码迷 >> Java相关 >> 在Panel上画圆,然后加载到JFrame上 为什么不行
  详细解决方案

在Panel上画圆,然后加载到JFrame上 为什么不行

热度:186   发布时间:2007-05-15 02:07:27.0
在Panel上画圆,然后加载到JFrame上 为什么不行

import java.awt.*;
import javax.swing.*;

public class MyTest extends JPanel{
public static float pi=3.1415926f;

public void paint(Graphics g){
float i=0;
Graphics2D g2=(Graphics2D)g;

for(i=0;i<=2*pi;i=i+0.1f){
float x=(float) Math.cos(i)*50+60;
float y=(float)Math.sin(i)*50+70;

g2.drawString("★", x, y);
}
}

public static void main(String[] args){
JPanel panel = new JPanel();
JFrame Frame=new JFrame("paint test");
Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.add(new MyTest());
Frame.add(panel);
Frame.setSize(400,400);
Frame.setVisible(true);
}
}

搜索更多相关的解决方案: float  Panel  加载  JFrame  public  

----------------解决方案--------------------------------------------------------

import java.awt.*;
import javax.swing.*;

class MyTest extends JPanel{
public static float pi=3.1415926f;
JButton sure = new JButton("AAA");

public void paint(Graphics g){
float i=0;
Graphics2D g2=(Graphics2D)g;

for(i=0;i<=2*pi;i=i+0.1f){
float x=(float) Math.cos(i)*50+60;
float y=(float)Math.sin(i)*50+70;

g2.drawString("★", x, y);
}
}

public static void main(String[] args){
JPanel panel = new MyTest();//要实例的是MyTest类,不是JPanel
JFrame frame=new JFrame("paint test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.add(new MyTest());
frame.add(panel);//另外命名变量时不要用类名.
frame.setSize(400,400);
frame.setVisible(true);

}
}


----------------解决方案--------------------------------------------------------

谢谢帮忙!


----------------解决方案--------------------------------------------------------