Java语言程序设计
1 / 81
&
[编程练习题答案]Java语言程序设计(基础篇)(进阶篇)第13、14、
15、24、25、26、28、29、30、31章部分习题答案,奇数题和部分
双数题是我自己做的,在此和大家分享(^_^~
Chapter 13 Exerci 13.2 / 13.8 / 13.11
import javax.swing.*;
import java.awt.*;
public class Exerci13_2 extends JFrame {
// Create two buttons
private OvalButton jbtOk = new OvalButton("OK");
private OvalButton jbtCancel = new OvalButton("Cancel");
/** Default constructor */
public Exerci13_2() {
// Set the window title
tTitle("Exerci13_2");
// Set FlowLayout manager to arrange the components
Java语言程序设计
2 / 81
// inside the frame
getContentPane().tLayout(new FlowLayout());
/
/ Add buttons to the frame
getContentPane().add(jbtOk);
getContentPane().add(jbtCancel);
}
/** Main method */
public static void main(String[] args) {
Exerci13_2 frame = new Exerci13_2();
frame.tDefaultCloOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.tVisible(true);
}
}
class 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 getPreferredSize() {
return new Dimension(100, 50);
}
矮墩墩/** Override get method for minimumSize */
public Dimension getMinimumSize() {
return new Dimension(100, 50);
}
}
Java语言程序设计
3 / 81
import java.awt.*;
import javax.swing.*;
又岂在朝朝暮暮上一句
public class Exerci13_8 extends JFrame {
public static void main(String[] args) {
JFrame frame = new Exerci13_8();
frame.tSize(300, 300);
frame.tTitle("Exerci13_8");
frame.tDefaultCloOperation(JFrame.EXIT_ON_CLOSE);
frame.tVisible(true);
}
public Exerci13_8() {
getContentPane().add(new OctagonPanel());
}
}
class OctagonPanel extends JPanel {
protected void paintComponent(Graphics 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((int)(xCenter + radius * s(2 * Math.PI / 8)),
(int)(yCenter - radius * Math.sin(2 * Math.PI / 8)));
polygon.addPoint((int)(xCenter + radius * s(2 * 2 * Math.PI
Java语言程序设计
4 / 81
/ 8)),
(int)(yCenter - radius * M
ath.sin(2 * 2 * Math.PI / 8)));
polygon.addPoint((int)(xCenter + radius * s(3 * 2 * Math.PI
/ 8)),
(int)(yCenter - radius * Math.sin(3 * 2 * Math.PI / 8)));
polygon.addPoint((int)(xCenter + radius * s(4 * 2 * Math.PI
/ 8)),
(int)(yCenter - radius * Math.sin(4 * 2 * Math.PI / 8)));
polygon.addPoint((int)(xCenter + radius * s(5 * 2 * Math.PI
/ 8)),
(int)(yCenter - radius * Math.sin(5 * 2 * Math.PI / 8)));
polygon.addPoint((int)(xCenter + radius * s(6 * 2 * Math.PI
/
8)),
(int)(yCenter - radius * Math.sin(6 * 2 * Math.PI / 8)));
polygon.addPoint((int)(xCenter + radius * s(7 * 2 * Math.PI
/ 8)),
(int)(yCenter - radius * Math.sin(7 * 2 * Math.PI / 8)));
// Draw the polygon
g.drawPolygon(polygon);
}
}
import javax.swing.*;
import java.awt.*;
public class Exerci13_11 extends JFrame {
public Exerci13_11(){
add(new SquareFunction());
}
/**
* @param args
*/
public static void main(String[] args) {
Java语言程序设计
5 / 81
// TODO 自动生成方法存根
Exerci13_11 frame = new Exerci13_11();
frame.tTitle("Exerci13.11");
frame.tSize(300,200);
frame.tLocationRelativeTo(null);
frame.tDefaultCloOperation(JFrame.EXIT_ON_CLOSE);
frame.tVisible(true);
}
}
class SquareFunction extends JPanel {
public SquareFunction(){
}
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.drawLine(10, Height()-20, Width()-10,
过年做什么菜好
g.Width()-30, Height()-15,
g.Width()-30, Height()-25,
g.drawString("X", Width()-10, Height()-30);
g.Width()/2, 20, Width()/2,
g.Width()/2, 20, Width()/2-5, 35);
g.Width()/2, 20, Width()/2+5, 35);
g.drawString("Y", Width()/2 + 10, 30);
Polygon p = new Polygon();
double scaleFactor = 0.01;
for (int x=-100; x<=100; x++){
p.addPoint(Width()/2,
}
g.drawPolyline(p.xpoints, p.ypoints, p.npoints);
}
}
Java语言程序设计
6 / 81
Chapter 14 Exerci 14.4 / 14.8 / 14.9
package chapter14;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Exerci14_4 extends JFrame {
public Exerci14_4(){
MouPosition p = new MouPosition();
add(p);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
Exerci14_4 frame = new Exerci14_4(
);
frame.tTitle("Exerci14.4");
frame.tSize(300, 200);
frame.tDefaultCloOperation(JFrame.EXIT_ON_CLOSE);
frame.tLocationRelativeTo(null);
frame.tVisible(true);
}
static class MouPosition extends JPanel {
private String position = "";
private int x;
private int y;
public MouPosition(){
addMouListener(new MouAdapter(){
public void mouPresd(MouEvent e){
Java语言程序设计
7 / 81
x = e.getX();
y = e.getY();
position = "(" + x + "," + y + ")";
repaint();
}
public void mouRelead(MouEvent e){
x = e.getX();
y = e.getY();
position = "";
repaint();
}
public void mouClicked(MouEvent e){
x = e.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 Exerci14_8 extends JFrame {
Java语言程序设计
8 / 81
private CharacterPanel characterPanl = new CharacterPanel();
public Exerci14_8(){
add(characterPanl);
characterPanl.tFocusable(true);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
JFrame frame = new Exerci14_8();
frame.tTitle("Exerci14.8");
frame.tSize(300, 200);
frame.tLocationRelativeTo(null);
frame.tDefaultCloOperation(JFrame.EXIT_ON_CLOSE);
frame.tVisible(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(KeyEvent e){
character = e.getKeyChar();
repaint();
}
});
addMouListener(new MouAdapter(){
public void mouClicked(MouEvent e){
x = e.getX();
y = e.getY();
repaint();
}
});
}
Java语言程序设计
9 / 81
protected void paintComponent(Graphics g){
super.paintComponent(g);
//g.tFont(new Font("TimesRoman", Font.PLAIN, 24));
g.drawString("" + character, x, y);
}
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Exerci14_9 extends JFrame{
private DrawLinesPanel drawLinesPanel = new DrawLinesPanel();
public Exerci14_9(){
add(drawLinesPanel);
drawLinesPanel.tFocusable(true);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
Exerci14_9 frame = new Exerci14_9();
frame.tTitle("Exerci14.9");
frame.tSize(300, 200);
frame.tDefaultCloOperation(JFrame.EXIT_ON_CLOSE);
frame.tLocationRelativeTo(null);
frame.tVisible(true);
}
Java语言程序设计
10 / 81
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 keyPresd(KeyEvent e){
KeyCode()){
ca KeyEvent.VK_UP:
y -= 10;
polygon.addPoint(x, y);
break;
ca KeyEvent.VK_DOWN:
y += 10;
polygon.addPoint(x, y);
break;
ca KeyEvent.VK_LEFT:
x -= 10;
polygon.addPoint(x, y);
break;
ca KeyEvent.VK_RIGHT:
x += 10;
polygon.addPoint(x, y);
break;
}
repaint();
}
});
}
protected void paintComponent(Graphics g){
super.paintComponent(g);
Polygon p = new Polygon();
int xCenter = getWidth()/2;
Java语言程序设计
11 / 81
int yCenter = getHeight()/2;
for(int i=0; i<polygon.npoints; i++){
p.addPoint(polygon.xpoints[i] +
xCenter,polygon.ypoints[i] + yCenter);
}
g.drawPolyline(p.xpoints, p.ypoints, p.npoints);
}
}
}
Chapter 15 Exerci 15.4 / 15.6 / 15.7
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Exerci15_4 extends JFrame {
protected JButton jbtAdd, jbtSubtract, jbtMultiply, jbtDivide;
protected JTextField jtfNum1, jtfNum2, jtfResult;
protected double x, y, result;
public Exerci15_4(){
tLayout(new BorderLayout());
JPanel jpIO = new JPanel();
jpIO.tLayout(new FlowLayout());
add(jpIO, BorderLayout.CENTER);
jpIO.add(new JLabel("Number 1"));
jpIO.add(jtfNum1 = new JTextField(4));
Java语言程序设计
12 / 81
jpIO.add(new JLabel("Number 2"));
jpIO.add(jtfNum2 = new JTextField(4));
jpIO.add(new JLabel("Result"));
jpIO.add(jtfResult = new JTextField(8));
jtfNum1.tHorizontalAlignment(JTextField.RIGHT);
jtfNum2.tHorizontalAlignment(JTextField.RIGHT);
jtfResult.tHorizontalAlignment(JTextField.RIGHT);
jtfResult.tEditable(fal);
JPanel jpButtons = new JPanel();
jpButto
ns.tLayout(new FlowLayout());
add(jpButtons, BorderLayout.SOUTH);
jpButtons.add(jbtAdd = new JButton("Add"));
jpButtons.add(jbtSubtract = new JButton("Subtract"));
jpButtons.add(jbtMultiply = new JButton("Multiply"));
jpButtons.add(jbtDivide = new JButton("Divide"));
ButtonGroup group = new ButtonGroup();
group.add(jbtAdd);
group.add(jbtSubtract);
group.add(jbtMultiply);
group.add(jbtDivide);
jbtAdd.tMnemonic('A');
jbtSubtract.tMnemonic('S');
jbtMultiply.tMnemonic('M');
jbtDivide.tMnemonic('D');
春节别称jbtAdd.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
x = Double.Text());
y = Double.Text());
result = add(x, y);
jtfResult.tText("" + (float)result);
嘴下有痣}
});
jbtSubtract.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
x = Double.Text());
y = Double.Text());
Java语言程序设计
13 / 81
result = subtract(x, y);
jtfResult.tText("" + (float)result);
}
});
jbtMultiply.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
x = Double.Text());
y = Double.Text());
result = multiply(x, y);
jtfResult.tText("" + (float)result);
}
});
jbtDivide.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
x = Double.Text());
y = Double.Text());
result = divide(x, y);
jtfResult.tText("" + (float)result);
}
});
}
public double add(double x, double y){
return x + y;
}
public double subtract(double x, double y){ 周长有
return x - y;
}
public double multiply(double x, double y){
return x * y;
}
public double divide(double x, double y){
return x / y;
}
/**
* @param args
Java语言程序设计
14 / 81
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
Exerci15_4 frame = new Exerci15_4();
frame.pack();
frame.tLocationRelativeTo(null);
frame.tDefaultCloOperation(JFrame.EXIT_ON_CLOSE);
frame.tTitle("Exerci15.4");
frame.tVisible(true);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Exerci15_6 extends JFrame {
private double futureValue, investmentAmount, annualInterestRate,
years;
protected JLabel jlbFutureValue, jlbInvestmentAmount,
jlbAnnualInterestRate,