您的位置:首页 > 其它

16.3

2016-07-02 15:51 274 查看
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test_16 extends JFrame implements ActionListener{
private JButton j1 = new JButton("left");
private JButton j2 = new JButton("right");
private JButton j3 = new JButton("up");
private JButton j4 = new JButton("down");
private JP j = new JP();
private int x =100,y =200;

public Test_16(){
JPanel J = new JPanel();
J.add(j1);J.add(j2);J.add(j3);J.add(j4);
j1.addActionListener(this);
j2.addActionListener(this);
j3.addActionListener(this);
j4.addActionListener(this);
add(J,BorderLayout.SOUTH);
add(j,BorderLayout.CENTER);
}

public static void main(String[] args) {
// TODO Auto-generated method stub
Test_16 t1 = new Test_16();
t1.setTitle("Test_16");
t1.setSize(400,400);
t1.setFocusable(true);
t1.setLocationRelativeTo(null);
t1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
t1.setVisible(true);
}

class JP extends JPanel{
private int radius = 5;

protected void paintComponent(Graphics g){
super.paintComponent(g);
g.drawOval(x, y, 2*radius, 2*radius);
}
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == j1)
{
x -= 20;
repaint();
}
else if(e.getSource() == j2){
x += 20;
repaint();
}else if(e.getSource() == j3){
y -= 20;
repaint();
}else if(e.getSource() == j4){
y += 20;
repaint();
}
}
}


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