您的位置:首页 > 其它

今天自己写了个无聊的测试

2016-01-06 21:48 204 查看
功能是:点击底下那个按钮生成我是好人,代码如下,无聊的人可以自己玩了- - 

class jpanel extends JPanel{
public int x=100,y=100;
@Override
public void paintComponent(Graphics g){
g.setColor(Color.blue);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.red);
g.drawString("我是好人", x, y);
requestFocus();
JButton jb = new JButton("点击生成你是好人");
this.setLayout(new BorderLayout());
this.add(jb,BorderLayout.SOUTH);
jb.addMouseListener(new ml(this));
}



class ml extends MouseAdapter{
jpanel jp1;
ml(jpanel j){
jp1 = j;
}
@Override 
public void mouseClicked(MouseEvent e){
Random r = new Random();
jp1.x = r.nextInt(jp1.getWidth());
jp1.y = r.nextInt(jp1.getHeight());
jp1.paintComponent(jp1.getGraphics());
}

}

public class jframe extends JFrame{
jpanel jp = new jpanel();
jframe(){
JFrame j = new JFrame();
j.setVisible(true);
j.setBounds(0,0,500,500);
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j.setLayout(new BorderLayout());
jp.setVisible(true);
j.add(jp);
}
public static void main(String[] args) {
new jframe();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  测试