【Java开源代码栏目提醒】:网学会员Java开源代码为您提供ControlWindow.java参考,解决您在ControlWindow.java学习中工作中的难题,参考学习。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ControlWindow extends JFrame implements ActionListener {
private JButton turnBtn, moveBtn;
private Robot theRobot;
/** post: this window is created
* and getX() == 13*72+10 and getY()==20
* and getWidth() == 200 and getHeight() == 120
* and theRobot == r
* and turnBtn is created and placed on this
* and moveBtn is created and placed on this
*/
public ControlWindow(Robot r) {
super("Control Buttons");
setBounds(13*72+30, 20, 200, 120);
getContentPane().setLayout(null);
setVisible(true);
theRobot = r;
turnBtn = new JButton("Turn");
turnBtn.setBounds(60, 10, 80, 30);
turnBtn.addActionListener(this);
getContentPane().add(turnBtn, 0);
moveBtn = new JButton("Step");
moveBtn.setBounds(60, 50, 80, 30);
moveBtn.addActionListener(this);
getContentPane().add(moveBtn, 0);
repaint();
}
/** This is an event hanlder for both buttons.
* post: e.getSource()==turnBtn implies postcondition of theRobot.rotateClockwise()
* and e.getCourse==moveBtn implies postcondition of theRobot.moveByOneStep()
*/
public void actionPerformed(ActionEvent e) {
if (e.getSource() == turnBtn)
theRobot.rotateClockwise();
else if (e.getSource() == moveBtn)
theRobot.moveByOneStep();
}
}
上一篇:
Controls3.java
下一篇:
不放弃终有希望