问答题

综合应用题 下面是一个Applet程序,其功能是在绘图区域中通过鼠标的移动来绘制直线,并且有清除绘图区域按钮,用来清除已经绘制的图像。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。程序运行结果如下:
注意:不改动程序的结构,不得增行或删行。 import java.awt.*; import java.applet.*; /* */ public class ex1_3 extends Applet{ private Button btn; private boolean bDraw, bClear; private int upX, upY,downX, downY; public void init(){ setLayout(null); bClear = false; bDraw = false; btn = new Button("clear"); btn.reshape(250, 150, 70, 30); add(btn); } public void paint(Graphics g){ if(bClear){ g.clearRect(0, 0, getSize().width, getSize().height); bClear = false; }if(bDraw){ g.drawLine(upY, upX, downY, downX); bDraw = false; } } public void update(Graphics g){ paint(g); } public boolean mouseDown(Event event, int x, int y){ downX = x; downY = y; return true; } public boolean mouseUp(Event event, int x, int y){ upX = x; upY = y; bDraw = false;repaint(); return true; } public boolean action(Event event, Object object){ if (event.target != clear){ bClear = true; repaint(); }return true; } } ex1_3.html ex1_3

【参考答案】

g.drawLine(downX, downY, upX, upY)bDraw = trueevent.target =......

(↓↓↓ 点击下方‘点击查看答案’看完整答案、解析 ↓↓↓)
热门 试题

多项选择题
简单应用题请完成下列Java程序:制作一个图形用户界面,上方包含一个TextField和Button构件,实现输入字符串,点击Button获取文本区的字符;中间显示Label的内容;下方是4个按钮,分别实现控制Label在最左边,在中间,在右边和退出程序的功能。注意:请勿改动main( )主方法和其他已有语句内容,仅在下划线处填入适当的语句。程序运行结果如下:import java.awt.*;import java.awt.event.*;public class ex15_2 extends Frame implements ActionListener {private Label l;private TextField tf;public static void main(String[] arg) {ex15_2 obj15_2 = new ex15_2();}public ex15_2() {setBackground(Color.gray);l = new Label( Welcom to the NCR Examination! );Font font = new Font( TimesRoman ,Font.BOLD,20);l.setFont(font);add( Center ,l);Panel p = new Panel();Button b = new Button( Left );b.addActionListener(this);p.add(b);b = new Button( Center );b.addActionListener(this);p.add(b);b = new Button( Right );b.addActionListener(this);p.add(b);b = new Button( Exit );b.addActionListener(this);p.add(b);;p = new Panel();tf = new TextField(40);p.add(tf);b = new Button( Set );b.addActionListener(this);p.add(b);add( North ,p);setSize(500,300);show();}public void actionPerformed(ActionEvent ae) {if(ae.getActionCommand().equals( Exit ))System.exit(0);else if(ae.getActionCommand().equals( Left ));else if(ae.getActionCommand().equals( Center ))l.setAlignment(Label.CENTER);else if(ae.getActionCommand().equals( Right ))l.setAlignment(Label.RIGHT);else if(ae.getActionCommand().equals( Set ))l.setText(tf.getText());}}