controlwindow.java

来自「Java程序设计(美) David D. Riley著 机械工业出版社 书籍配套」· Java 代码 · 共 43 行

JAVA
43
字号
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();    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?