import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Clock extends JPanel implements ActionListener{
JFrame frame;
int center_X,center_Y;
int r,start_X,start_Y;
public Clock()
{
frame = new JFrame("hellboy clock");
//this.setSize(300,300);
this.setBackground(Color.black);
frame.setBounds(200,200,300,300);
this.init();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(this,"Center");
frame.setVisible(true);
}
public void init()
{
center_X=(int)(frame.getSize().width/2);
center_Y=(int)(frame.getSize().height/2);
System.out.println(center_X+" "+center_Y);
r = (int)(center_X*0.8);
start_X=center_X-r;
start_Y=center_Y-r;
System.out.println(start_X+""+start_Y);
}
public void paint(Graphics g)
{
super.paintComponent(g);
g.drawOval(start_X,start_Y,2*r,2*r);
g.drawRect(start_X,start_Y,2*r,2*r);
g.fillOval(center_X,center_Y,5,5);
}
public void actionPerformed(ActionEvent e)
{
}
public static void main(String[] arg){
new Clock();
}
}
----------------解决方案--------------------------------------------------------
是你的定位错了
这里的JFrame.size()得到的是包括标题栏在内的窗口大小,也就是说(0,0)这一点不是从中间那块算的而是标题栏的左顶端
----------------解决方案--------------------------------------------------------
谢谢 了, 已经搞定
----------------解决方案--------------------------------------------------------
不用……
----------------解决方案--------------------------------------------------------