[计算机软件及应用]java英文第八版十六章答案.doc_第1页
[计算机软件及应用]java英文第八版十六章答案.doc_第2页
[计算机软件及应用]java英文第八版十六章答案.doc_第3页
[计算机软件及应用]java英文第八版十六章答案.doc_第4页
[计算机软件及应用]java英文第八版十六章答案.doc_第5页
已阅读5页,还剩26页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

import javax.swing.*;import java.awt.*;import java.awt.event.*;public class Exercise16_2 extends JFrame implements ComponentListener public Exercise16_2() / Set the window title setTitle(Exercise16_2); / Register the frame as a listener for component events this.addComponentListener(this); /* Main method */ public static void main(String args) Exercise16_2 frame = new Exercise16_2(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(100, 80); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); / Center the frame frame.setVisible(true); public void componentMoved(ComponentEvent e) System.out.println(Component moved); public void componentHidden(ComponentEvent e) System.out.println(Component hidden); public void componentResized(ComponentEvent e) System.out.println(Component resized); public void componentShown(ComponentEvent e) System.out.println(Component shown); import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Exercise16_4 extends JFrame / Text fields for Number 1, Number 2, and Result private JTextField jtfNum1, jtfNum2, jtfResult; / Buttons Add, Subtract, Multiply and Divide private JButton jbtAdd, jbtSub, jbtMul, jbtDiv; / Main Method public static void main(String args) Exercise16_4 frame = new Exercise16_4(); frame.pack(); frame.setTitle(Exercise16_4); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); / Center the frame frame.setVisible(true); / Default Constructor public Exercise16_4() / Panel p1 to hold text fields and labels JPanel p1 = new JPanel(); p1.setLayout(new FlowLayout(); p1.add(new JLabel(Number 1); p1.add(jtfNum1 = new JTextField(3); p1.add(new JLabel(Number 2); p1.add(jtfNum2 = new JTextField(3); p1.add(new JLabel(Result); p1.add(jtfResult = new JTextField(8); jtfResult.setEditable(false); jtfResult.setHorizontalAlignment(SwingConstants.RIGHT); / Panel p2 to hold buttons JPanel p2 = new JPanel(); p2.setLayout(new FlowLayout(); p2.add(jbtAdd = new JButton(Add); p2.add(jbtSub = new JButton(Subtract); p2.add(jbtMul = new JButton(Multiply); p2.add(jbtDiv = new JButton(Divide); / Set mnemonic keys jbtAdd.setMnemonic(A); jbtSub.setMnemonic(S); jbtMul.setMnemonic(M); jbtDiv.setMnemonic(D); / Add panels to the frame setLayout(new BorderLayout(); add(p1, BorderLayout.CENTER); add(p2, BorderLayout.SOUTH); / Register listeners jbtAdd.addActionListener(new Listener(); jbtSub.addActionListener(new Listener(); jbtMul.addActionListener(new Listener(); jbtDiv.addActionListener(new Listener(); class Listener implements ActionListener / Handle ActionEvent from buttons and menu items public void actionPerformed(ActionEvent e) String actionCommand = e.getActionCommand(); if (Add.equals(actionCommand) calculate(+); else if (Subtract.equals(actionCommand) calculate(-); else if (Multiply.equals(actionCommand) calculate(*); else if (Divide.equals(actionCommand) calculate(/); / Calculate and show the result in jtfResult private void calculate(char operator) / Obtain Number 1 and Number 2 double num1 = new Double(jtfNum1.getText().trim().doubleValue(); double num2 = new Double(jtfNum2.getText().trim().doubleValue(); double result = 0; / Perform selected operation switch (operator) case +: result = num1 + num2; break; case -: result = num1 - num2; break; case *: result = num1 * num2; break; case /: result = num1 / num2; / Set result in jtfResult jtfResult.setText(String.valueOf(result); import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Exercise16_6 extends JFrame private DisplayPanel panel = new DisplayPanel(); public Exercise16_6() add(panel, BorderLayout.CENTER); panel.setFocusable(true); /* Main method */ public static void main(String args) JFrame frame = new Exercise16_6(); frame.setTitle(Exercise16_6); frame.setSize(300, 300); frame.setLocationRelativeTo(null); / Center the frame frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); class DisplayPanel extends MessagePanel private String m1 = Java is fun; private String m2 = Java is powerful; private boolean isM1; public DisplayPanel() setCentered(true); this.addMouseListener(new MouseAdapter() public void mouseClicked(MouseEvent e) if (isM1) setMessage(m1); else setMessage(m2); isM1 = !isM1; ); import javax.swing.*;import java.awt.*;import java.awt.event.*;public class Exercise16_8 extends JFrame public Exercise16_8() add(new DisplayCoordinatePanelONE(); /*Main method*/ public static void main(String args) / Create a frame Exercise16_8 frame = new Exercise16_8(); frame.setTitle(Exercise16_8); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 300); frame.setLocationRelativeTo(null); / Center the frame frame.setVisible(true); /* Version 1: Display mouse position when the mouse is clicked */class DisplayCoordinatePanelONE extends JPanel / Point is a Java object for representing points in a plane. / p.x and p.y are the x and y coordinates. private Point p = new Point(0, 0); public DisplayCoordinatePanelONE() addMouseListener(new MouseAdapter() / When mouse is clicked, the mouse pointer location is / captured in the point p. public void mouseClicked(MouseEvent e) p.x = e.getX(); p.y = e.getY(); repaint(); ); / Draw a small solid square around the point public void paintComponent(Graphics g) super.paintComponent(g); g.drawString( + p.x + , + p.y + ), p.x, p.y); /* Version 2: Display mouse position when the mouse is pressed. The display disappears when the mouse is released. */class DisplayCoordinatePanelTWO extends JPanel / Point is a Java object for representing points in a plane. / p.x and p.y are the x and y coordinates. private Point p = new Point(0, 0); private boolean pressed = false; public DisplayCoordinatePanelTWO() addMouseListener(new MouseAdapter() public void mousePressed(MouseEvent e) p.x = e.getX(); p.y = e.getY(); pressed = true; repaint(); public void mouseReleased(MouseEvent e) pressed = false; repaint(); ); / Draw a small solid square around the point public void paintComponent(Graphics g) super.paintComponent(g); if (pressed) g.drawString( + p.x + , + p.y + ), p.x, p.y); import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Exercise16_10 extends JFrame private DisplayPanel panel = new DisplayPanel(); public Exercise16_10() add(panel, BorderLayout.CENTER); panel.setFocusable(true); /* Main method */ public static void main(String args) JFrame frame = new Exercise16_10(); frame.setTitle(Exercise16_10); frame.setSize(300, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); / Center the frame frame.setVisible(true); class DisplayPanel extends MessagePanel private String message = ; private StringBuffer buffer = new StringBuffer(); public DisplayPanel() setCentered(true); this.addKeyListener(new Listener(); / Add listener class Listener extends KeyAdapter public void keyTyped(KeyEvent e) buffer.append(e.getKeyChar(); public void keyPressed(KeyEvent e) if (e.getKeyCode() = KeyEvent.VK_ENTER) message = buffer.toString().trim(); buffer.setLength(0); setMessage(message); import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Exercise16_12 extends JFrame public Exercise16_12() add(new Fan(); / Main method public static void main(String args) / Create a frame Exercise16_12 frame = new Exercise16_12(); frame.setTitle(Exercise16_12: Running Fan); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); / Display the frame frame.setSize(200, 200); frame.setLocationRelativeTo(null); / Center the frame frame.setVisible(true); class Fan extends JPanel private int xCenter, yCenter;private int fanRadius, bladeLength;private int angle = 100;private int direction = 1;private int speed = 10;protected Timer timer = new Timer(speed, new TimerListener(); public Fan() timer.start();public void reverse() direction = -direction; public void setSpeed(int ms) speed = ms; timer.setDelay(speed); public void paintComponent(Graphics g) super.paintComponent(g); / Set clock radius, and center fanRadius = (int)(Math.min(getSize().width, getSize().height)*0.9*0.5); xCenter = (getSize().width)/2; yCenter = (getSize().height)/2; bladeLength = (int)(fanRadius*0.9); angle = (angle+direction)%360; / Draw circle g.setColor(Color.black); g.drawOval(xCenter - fanRadius,yCenter - fanRadius, 2*fanRadius, 2*fanRadius); / Draw four blades drawBlade(g, angle); drawBlade(g, angle+90); drawBlade(g, angle+180); drawBlade(g, angle+270);private void drawBlade(Graphics g, int angle) g.setColor(Color.red); g.fillArc(xCenter-bladeLength, yCenter-bladeLength, 2*bladeLength, 2*bladeLength, angle, 30); class TimerListener implements ActionListener public void actionPerformed(java.awt.event.ActionEvent actionEvent) repaint(); import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.applet.*;import .*;public class Exercise16_14 extends JFrame public Exercise16_14() URL imageURL = this.getClass().getResource(image/us.gif); Image image = new ImageIcon(imageURL).getImage(); add(new FlagAnthemPanel(image); public static class FlagAnthemPanel extends JPanel private Image image; private Timer timer = new Timer(400, new Listener(); int x = 20; int y = 150; public FlagAnthemPanel(Image image) this.image = image; timer.start(); public void paintComponent(Graphics g) super.paintComponent(g); if (y 0) y -= 1; g.drawImage(image, x, y, 60, 40, this); class Listener implements ActionListener public void actionPerformed(ActionEvent e) repaint(); public static void main(String args) / Create a frame JFrame frame = new Exercise16_14(); frame.setTitle(Exercise16_14); / Display the frame frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 200); frame.setLocationRelativeTo(null); / Center the frame frame.setVisible(true); import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Exercise16_16 extends JFrame public Exercise16_16() add(new FlashLabel(Welcome to Java); / Main method public static void main(String args) / Create a frame JFrame frame = new Exercise16_16(); frame.setTitle(Exercise16_16); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); / Display the frame frame.setSize(200, 200); frame.setLocationRelativeTo(null); / Center the frame frame.setVisible(true); class FlashLabel extends JPanel private boolean show = true; private Timer timer = new Timer(200, new ActionListener() public void actionPerformed(ActionEvent e) repaint(); ); String label = Welcome to Java; public FlashLabel(String label) this.label = label; timer.start(); public void setLabel(String label) this.label = label; public void paintComponent(Graphics g) super.paintComponent(g); if (show) g.drawString(label, 20, 20); show = !show; public Dimension getPreferredSize() return new Dimension(200, 50); import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Exercise16_18 extends JFrame public Exercise16_18() add(new MovingCircleByArrowKey(); /* Main method */ public static void main(String args) Exercise16_18 frame = new Exercise16_18(); frame.setTitle(Exercise16_18); frame.setSize(200, 100); frame.setLocationRelativeTo(null); / Center the frame frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); / Displaying a moving circleclass MovingCircleByArrowKey extends JPanel private int x = 20; private int y = 20; public static int RADIUS = 10; public MovingCircleByArrowKey() this.setFocusable(true); this.addKeyListener(new KeyAdapter() public void keyPressed(KeyEvent e) switch (e.getKeyCode() case KeyEvent.VK_DOWN: y += 10; break; case KeyEvent.VK_UP: y -= 10; break; case KeyEvent.VK_LEFT: x -= 10; break; case KeyEvent.VK_RIGHT: x += 10; break; default: ; repaint(); ); /* Paint message */ public void paintComponent(Graphics g) super.paintComponent(g); g.drawOval(x - RADIUS, y - RADIUS, 2 * RADIUS, 2 * RADIUS); import javax.swing.*;import java.awt.*;import java.awt.event.*;import javax.swing.border.*;public class Exercise16_20 extends JFrame private PaintPanel paintPanel = new PaintPanel(); public Exercise16_20() add(paintPanel); static class PaintPanel extends JPanel private MyRectangle2D rectangle = new MyRectangle2D(100, 60, 100, 40); private boolean isInside = false; private Point mousePoint = new Point(0, 0); public static int RADIUS = 50; public PaintPanel() this.addMouseMotionListener(new MouseAdapter() public void mouseMoved(MouseEvent e) if (rectangle.contains(e.getX(), e.getY() isInside = true; else isInside = false; mousePoint.x = e.getX(); mousePoint.y = e.getY(); repaint(); ); /* Paint message */ public void paintComponent(Graphics g) super.paintComponent(g); g.drawRect(int)(rectangle.getX() - (int)(rectangle.getWidth() / 2), (int)(rectangle.getY() - (int)(rectangle.getHeight() / 2), (int)(rectangle.getWidth(), (int)(rectangle.getHeight(); g.drawString(isInside ? Mouse point is in the rectangle : Mouse point is not in the rectangle, mousePoint.x, mousePoint.y); public static void main(String args) JFrame frame = new Exercise16_20(); frame.setTitle(Exercise16_20); frame.setSize(300, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); frame.setVisible(true); import javax.swing.*;import java.awt.*;import java.awt.event.*;public class Exercise16_22 extends JFrame final static int NUMBER_OF_SLOTS = 9; final static int NUMBER_OF_ROWS = NUMBER_OF_SLOTS - 2; private int shift = 0; private int slots = new intNUMBER_OF_SLOTS; private int numberOfBallsDropped = 0; private int moveCount = 0; private int position = 0; private BeanMachinePanel paintPanel = new BeanMachinePanel(); private Timer timer = new Timer(200, new ActionListener() public void actionPerformed(ActionEvent e) moveCount+; if (moveCount = NUMBER_OF_ROWS) if (Math.random() 0.5) paintPanel.moveRedBallLeft(); else paintPanel.moveRedBallRight(); position+; else slotsposition+; paintPanel.startRedBall(); shift = 0; moveCount = 0; position = 0; numberOfBallsDropped+; if (numberOfBallsDropped = 10) timer.stop(); paintPanel.hideRedBall(); ); public Exercise16_22() add(paintPanel); timer.start(); class BeanMachinePanel extends JPanel final static int HGAP = 20; final static int VGAP = 20; final static int RADIUS = 5; f

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论