【发布时间】:2018-03-09 14:42:49
【问题描述】:
我怎样才能通过按左右键移动球? 代码如下:
private JPanel panel;
private JButton left;
private JButton right;
private Container c = getContentPane();
cycle() {
panel = new JPanel();
right = new JButton("RIGHT");
right.addActionListener(this);
left = new JButton("LEFT");
left.addActionListener(this);
c.add(panel);
panel.add(right);
panel.add(left);
}
public static void main(String[] args) {
JFrame f = new JFrame();
cycle ball = new cycle();
f.add(ball);
f.setTitle("Move the ball");
f.setSize(500, 500);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
@Override
public void paint(Graphics g) {
g.drawOval(150, 50, 150, 150);
g.setColor(Color.yellow);
}
@Override
public void actionPerformed(ActionEvent ae) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
【问题讨论】: