问答题

本题程序的功能是:主窗口中有一个按钮,按钮的长和宽每200ms增加1,当达到100时又恢复原来大小重新进行增加。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。
import java.awt.*;
import java.awt.event.*;
public class simple

public static void main (String args[])

Mywin win = new Mywin();


class Mywin extends Frame ______

Button b = new Button("按钮");int x = 5;
Thread bird = null;
Mywin ()

setBounds(100,100,400,400); setLayout(new FlowLayout());
setTitle ("simple");
setVisible (true);
add(b);
b.setBackground (Color.green);
addWindowListener (new WindowAdapter ()
public void windowClosing(WindowEvent e)

System.exit (0);

);
bird = new Thread(this);
bird.start();

public ______

while (true)

x = x+1;
if(x > 100)
x = 5;
b.setBounds(40,40,x,x);
try

bird.sleep (200);

catch(InterruptedException e)


【参考答案】

implements Runnable。
void run。
热门 试题

问答题
本题程序的功能是:主窗口中有两个按钮“Start”和“Close”,单击按钮“Start”后会在窗口左上角生成一个黑色实心小球,小球做斜线运动,碰到面板边缘后反弹几次后停止运动,再次单击按钮“Start”后又会生成一个小球做同样的运动。程序中存在若干错误,请找出并改正(注意:不得改动程序的结构,不得增行或删行)。 import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.util.*; import javax.swing.*; public class advance public static void main (String[] args) JFrame frame = new BounceFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) frame.show(); class BounceFrame extends JFrame public BounceFrame() setSize(WIDTH,HEIGHT); setTitle( advance ); Container contentPane = getContentPane(); Canvas = new BallCanvas(); contentPane.add(canvas,BorderLayout.CENTER); JPanel buttonPanel = new JPanel(); addButton(buttonPanel, Start ,new ActionListener() public void actionPerformed(ActionEvent evt) addBall(); ); addButton(buttonPanel, Close ,new ActionListener() public void actionPerformed(ActionEvent evt) System.exit (0); ); contentPane.add(buttonPanel,BorderLayout.SOUTH); public void addButton(Container c,String title,Listener listener) JButton button = new JButton(title); c.add(button); button.addActionListener(listener); public void addBall() try Ball b = new Ball(canvas); canvas.add(b); for (int i = 1;i <= 1000; i++) b.move (); Runnable.sleep (5); catch (InterruptedException exception) private BallCanvas canvas; public static final int WIDTH = 450; public static final int HEIGHT = 350; class BallCanvas extend JPanel public void add(Ball b) balls.add(b); public void paintComponent(Graphics g) super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; for (int i = 0; i < balls.size(); i++) Ball b = (Ball)balls.get(i); b.draw(g2); private ArrayList balls = new ArrayList(); class Ball public Ball(Component c) canvas = c; public void draw(Graphics2D g2) g2.fill(new Ellipse2D.Double(x,y,XSIZE,YSIZE)); public void move() x += dx; y += dy; if(x < 0) x = 0; dx = -dx; if(x+XSIZE >= canvas.getWidth()) x = canvas.getWidth() - XSIZE; dx = -dx; if(y < 0) y = 0; dy = -dy; if (y+YSIZE >= canvas.getHeight()) y = canvas.getHeight() - YSIZE; dy = -dy; canvas.paint(canvas.getGraphics()); private Component canvas; private static final int XSIZE = 15; private static final int YSIZE = 15; private int x = 0; private int y = 0; private int dx = 2; private int dy = 2;