1、Java 语言程序设计 习题答案- Page 1- Java 语言程序设计 & 编程练习题答案Java 语言程序设计(基础篇)(进阶篇)第13、14、 15、24、25、26、28、29、30、31 章部分习题答案,奇数题和部分 双数题是我自己做的,在此和大家分享(_ Chapter 13 Exercise 13.2 / 13.8 / 13.11 import javax.swing.*; import java.awt.*; public class Exercise13_2 extends JFrame / Create two buttons private OvalButton jbt
2、Ok = new OvalButton(OK); private OvalButton jbtCancel = new OvalButton(Cancel); /* Default constructor */ public Exercise13_2() / Set the window title setTitle(Exercise13_2); / Set FlowLayout manager to arrange the components 1 / 81 - Page 2- Java 语言程序设计 / inside the frame getContentPane().setLayout
3、(new FlowLayout(); / Add buttons to the frame getContentPane().add(jbtOk); getContentPane().add(jbtCancel); /* Main method */ public static void main(String args) Exercise13_2 frame = new Exercise13_2(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); clas
4、s OvalButton extends JButton public OvalButton() public OvalButton(String text) super (text); protected void paintComponent(Graphics g) / Draw an oval super .paintComponent(g); g.drawOval(5, 5, getWidth() - 10, getHeight() - 10); /* Override get method for preferredSize */ public Dimension getPrefer
5、redSize() return new Dimension(100, 50); /* Override get method for minimumSize */ public Dimension getMinimumSize() return new Dimension(100, 50); 2 / 81 - Page 3- Java 语言程序设计 import java.awt.*; import javax.swing.*; public class Exercise13_8 extends JFrame public static void main(String args) JFra
6、me frame = new Exercise13_8(); frame.setSize(300, 300); frame.setTitle(Exercise13_8); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); public Exercise13_8() getContentPane().add(new OctagonPanel(); class OctagonPanel extends JPanel protected void paintComponent(Graphics
7、g) super .paintComponent(g); int xCenter = getWidth() / 2; int yCenter = getHeight() / 2; int radius = (int) (Math.min (getWidth(), getHeight() * 0.4); / Create a Polygon object Polygon polygon = new Polygon(); / Add points to the polygon polygon.addPoint(xCenter + radius, yCenter); polygon.addPoint
8、(int)(xCenter + radius * Math.cos (2 * Math.PI / 8), (int)(yCenter - radius * Math.sin (2 * Math.PI / 8); polygon.addPoint(int)(xCenter + radius * Math.cos (2 * 2 * Math.PI 3 / 81 - Page 4- Java 语言程序设计 / 8), (int)(yCenter - radius * Math.sin (2 * 2 * Math.PI / 8); polygon.addPoint(int)(xCenter + rad
9、ius * Math.cos (3 * 2 * Math.PI / 8), (int)(yCenter - radius * Math.sin (3 * 2 * Math.PI / 8); polygon.addPoint(int)(xCenter + radius * Math.cos (4 * 2 * Math.PI / 8), (int)(yCenter - radius * Math.sin (4 * 2 * Math.PI / 8); polygon.addPoint(int)(xCenter + radius * Math.cos (5 * 2 * Math.PI / 8), (i
10、nt)(yCenter - radius * Math.sin (5 * 2 * Math.PI / 8); polygon.addPoint(int)(xCenter + radius * Math.cos (6 * 2 * Math.PI / 8), (int)(yCenter - radius * Math.sin (6 * 2 * Math.PI / 8); polygon.addPoint(int)(xCenter + radius * Math.cos (7 * 2 * Math.PI / 8), (int)(yCenter - radius * Math.sin (7 * 2 *
11、 Math.PI / 8); / Draw the polygon g.drawPolygon(polygon); import javax.swing.*; import java.awt.*; public class Exercise13_11 extends JFrame public Exercise13_11() add(new SquareFunction(); /* * param args */ public static void main(String args) 4 / 81 - Page 5- Java 语言程序设计 / TODO 自动生成方法存根 Exercise1
12、3_11 frame = new Exercise13_11(); frame.setTitle(Exercise13.11); frame.setSize(300,200); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); class SquareFunction extends JPanel public SquareFunction() protected void paintComponent(Graphics
13、 g) super .paintComponent(g); g.drawLine(10, this .getHeight()-20, this .getWidth()-10, this .getHeight()-20); g.drawLine(this .getWidth()-30, this .getHeight()-15, this .getWidth()-10, this .getHeight()-20); g.drawLine(this .getWidth()-30, this .getHeight()-25, this .getWidth()-10, this .getHeight(
14、)-20); g.drawString(X, this .getWidth()-10, this .getHeight()-30); g.drawLine(this .getWidth()/2, 20, this .getWidth()/2, this .getHeight()-5); g.drawLine(this .getWidth()/2, 20, this .getWidth()/2-5, 35); g.drawLine(this .getWidth()/2, 20, this .getWidth()/2+5, 35); g.drawString(Y, this .getWidth()
15、/2 + 10, 30); Polygon p = new Polygon(); double scaleFactor = 0.01; for (int x=-100; x=100; x+) p.addPoint(x+this .getWidth()/2, this .getHeight()-20-(int)(scaleFactor*x*x); g.drawPolyline(p.xpoints, p.ypoints, p.npoints); 5 / 81 - Page 6- Java 语言程序设计 Chapter 14 Exercise 14.4 / 14.8 / 14.9 package c
16、hapter14; import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Exercise14_4 extends JFrame public Exercise14_4() MousePosition p = new MousePosition(); add(p); /* * param args */ public static void main(String args) / TODO 自动生成方法存根 Exercise14_4 frame = new Exercise14_4(); f
17、rame.setTitle(Exercise14.4); frame.setSize(300, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); frame.setVisible(true); static class MousePosition extends JPanel private String position = ; private int x; private int y; public MousePosition() addMouseLi
18、stener(new MouseAdapter() public void mousePressed(MouseEvent e) 6 / 81 - Page 7- Java 语言程序设计 x = e.getX(); y = e.getY(); position = ( + x + , + y + ); repaint(); public void mouseReleased(MouseEvent e) x = e.getX(); y = e.getY(); position = ; repaint(); public void mouseClicked(MouseEvent e) x = e.
19、getX(); y = e.getY(); position = ( + x + , + y + ); repaint(); ); protected void paintComponent(Graphics g) super .paintComponent(g); g.drawString(position, x, y); import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Exercise14_8 extends JFrame 7 / 81 - Page 8- Java 语言程序设计
20、private CharacterPanel characterPansel = new CharacterPanel(); public Exercise14_8() add(characterPansel); characterPansel.setFocusable(true); /* * param args */ public static void main(String args) / TODO 自动生成方法存根 JFrame frame = new Exercise14_8(); frame.setTitle(Exercise14.8); frame.setSize(300, 2
21、00); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); static class CharacterPanel extends JPanel private int x = 5; private int y = 10; private char character; public CharacterPanel() addKeyListener(new KeyAdapter() public void keyTyped
22、(KeyEvent e) character = e.getKeyChar(); repaint(); ); addMouseListener(new MouseAdapter() public void mouseClicked(MouseEvent e) x = e.getX(); y = e.getY(); repaint(); ); 8 / 81 - Page 9- Java 语言程序设计 protected void paintComponent(Graphics g) super .paintComponent(g); /g.setFont(new Font(TimesRoman,
23、 Font.PLAIN, 24); g.drawString( + character, x, y); import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Exercise14_9 extends JFrame private DrawLinesPanel drawLinesPanel = new DrawLinesPanel(); public Exercise14_9() add(drawLinesPanel); drawLinesPanel.setFocusable(true); /
24、* * param args */ public static void main(String args) / TODO 自动生成方法存根 Exercise14_9 frame = new Exercise14_9(); frame.setTitle(Exercise14.9); frame.setSize(300, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); frame.setVisible(true); 9 / 81 - Page 10- Ja
25、va 语言程序设计 static class DrawLinesPanel extends JPanel private int x, y; private Polygon polygon = new Polygon(); public DrawLinesPanel() polygon.addPoint(x, y); addKeyListener(new KeyAdapter() public void keyPressed(KeyEvent e) switch (e.getKeyCode() case KeyEvent.VK_UP: y -= 10; polygon.addPoint(x,
26、y); break ; case KeyEvent.VK_DOWN: y += 10; polygon.addPoint(x, y); break ; case KeyEvent.VK_LEFT: x -= 10; polygon.addPoint(x, y); break ; case KeyEvent.VK_RIGHT: x += 10; polygon.addPoint(x, y); break ; repaint(); ); protected void paintComponent(Graphics g) super .paintComponent(g); Polygon p = n
27、ew Polygon(); int xCenter = getWidth()/2; 10 / 81 - Page 11- Java 语言程序设计 int yCenter = getHeight()/2; for (int i=0; ipolygon.npoints; i+) p.addPoint(polygon.xpointsi + xCenter,polygon.ypointsi + yCenter); g.drawPolyline(p.xpoints, p.ypoints, p.npoints); Chapter 15 Exercise 15.4 / 15.6 / 15.7 import
28、java.awt.*; import java.awt.event.*; import javax.swing.*; public class Exercise15_4 extends JFrame protected JButton jbtAdd, jbtSubtract, jbtMultiply, jbtDivide; protected JTextField jtfNum1, jtfNum2, jtfResult; protected double x, y, result; public Exercise15_4() setLayout(new BorderLayout(); JPan
29、el jpIO = new JPanel(); jpIO.setLayout(new FlowLayout(); add(jpIO, BorderLayout.CENTER); jpIO.add(new JLabel(Number 1); jpIO.add(jtfNum1 = new JTextField(4); 11 / 81 - Page 12- Java 语言程序设计 jpIO.add(new JLabel(Number 2); jpIO.add(jtfNum2 = new JTextField(4); jpIO.add(new JLabel(Result); jpIO.add(jtfResult = new JTextField(8); jtfNum1.setHorizontalAlignment(JTextField.RIGHT); jtfNum2.setHorizontalAlignment(JTextField.RIGHT); jtfResult.setHorizontalAlignment(JTextField.RIG