admin 管理员组

文章数量: 1086019


2025年1月1日发(作者:tcpip四层网络模型)

Java语言程序设计基础篇 课后答案-机械工业出版社

0

Java语言程序设计

1 / 81

&

0

0

[编程练习题答案]Java语言程序设计(基础篇)(进阶篇)第13、14、

0

15、24、25、26、28、29、30、31章部分习题答案,奇数题和部分

0

双数题是我自己做的,在此和大家分享(^_^~

Chapter 13 Exercise 13.2 / 13.8 / 13.11

import .*;

import .*;

public class Exercise13_2 extends JFrame {

// Create two buttons

0

0

private OvalButton jbtOk = new OvalButton("OK");

private OvalButton jbtCancel = new OvalButton("Cancel");

0

/** Default constructor */

public Exercise13_2() {

// Set the window title

// Set FlowLayout manager to arrange the components

0

0

0

setTitle("Exercise13_2");

0

Java语言程序设计

2 / 81

0

0

0

// inside the frame

getContentPane().setLayout(new FlowLayout());

// Add buttons to the frame

0

getContentPane().add(jbtOk);

}

/** Main method */

0

getContentPane().add(jbtCancel);

0

public static void main(String[] args) {

Exercise13_2 frame = new Exercise13_2();

0

aultCloseOperation(_ON_CLOSE);

0

();

}

}

ible(true);

0

class OvalButton extends JButton {

public OvalButton() {

}

public OvalButton(String text) {

super(text);

}

0

0

0

0

protected void paintComponent(Graphics g) {

0

// Draw an oval

omponent(g);

0

al(5, 5, getWidth() - 10, getHeight() - 10);

}

/** Override get method for preferredSize */

0

public Dimension getPreferredSize() {

return new Dimension(100, 50);

}

0

/** Override get method for minimumSize */

0

0

public Dimension getMinimumSize() {

return new Dimension(100, 50);

}

}

Java语言程序设计

3 / 81

import .*;

0

0

0

import .*;

public class Exercise13_8 extends JFrame {

public static void main(String[] args) {

JFrame frame = new Exercise13_8();

e(300, 300);

0

0

le("Exercise13_8");

0

aultCloseOperation(_ON_CLOSE);

ible(true);

}

public Exercise13_8() {

0

getContentPane().add(new OctagonPanel());

}

}

0

class OctagonPanel extends JPanel {

0

protected void paintComponent(Graphics g) {

omponent(g);

0

int xCenter = getWidth() / 2;

int radius =

int yCenter = getHeight() / 2;

0

0

(int) ((getWidth(), getHeight()) * 0.4);

// Create a Polygon object

// Add points to the polygon

0

Polygon polygon = new Polygon();

nt(xCenter + radius, yCenter);

nt((int)(xCenter + radius * (2 *

/ 8)),

0

(int)(yCenter - radius * (2 * / 8)));

0

nt((int)(xCenter + radius * (2 * 2

*

4 / 81

/ 8)),

Java语言程序设计

0

(int)(yCenter - radius * (2 * 2 * / 8)));

0

nt((int)(xCenter + radius * (3 * 2

*

/ 8)),

(int)(yCenter - radius * (3 * 2 * / 8)));

0

nt((int)(xCenter + radius * (4 * 2

*

/ 8)),

(int)(yCenter - radius * (4 * 2 * / 8)));

0

nt((int)(xCenter + radius * (5 * 2

*

/ 8)),

(int)(yCenter - radius * (5 * 2 * / 8)));

0

nt((int)(xCenter + radius * (6 * 2

*

/ 8)),

(int)(yCenter - radius * (6 * 2 * / 8)));

0

nt((int)(xCenter + radius * (7 * 2

*

/ 8)),

(int)(yCenter - radius * (7 * 2 * / 8)));

0

// Draw the polygon

}

}

import .*;

import .*;

public class Exercise13_11 extends JFrame {

public Exercise13_11(){

}

/**

0

0

0

0

lygon(polygon);

0

add(new SquareFunction());

* @param args

*/

0

0

public static void main(String[] args) {

Java语言程序设计

5 / 81

0

0

0

// TODO 自动生成方法存根

Exercise13_11 frame = new Exercise13_11();

le("Exercise13.11");

e(300,200);

ationRelativeTo(null);

0

aultCloseOperation(_ON_CLOSE);

ible(true);

}

}

0

class SquareFunction extends JPanel {

public SquareFunction(){

}

0

0

protected void paintComponent(Graphics g){

0

0

omponent(g);

ne(10, ght()-20, th()-10,

0

ght()-20);

ne(th()-30, ght()-15,

0

th()-10, ght()-20);

0

ne(th()-30, ght()-25,

0

th()-10, ght()-20);

ring("X", th()-10,

ght()-30);

0

ne(th()/2, 20, th()/2,

0

ght()-5);

35);

35);

ne(th()/2, 20, th()/2-5,

0

ne(th()/2, 20, th()/2+5,

0

ring("Y", th()/2 + 10, 30);

0

Polygon p = new Polygon();

double scaleFactor = 0.01;

for (int x=-100; x<=100; x++){

0

nt(x+th()/2,

0

0

ght()-20-(int)(scaleFactor*x*x));

0

}

lyline(s, s, s);

}

}

0

Java语言程序设计

6 / 81

0

0

Chapter 14 Exercise 14.4 / 14.8 / 14.9

package chapter14;

import .*;

import .*;

import .*;

public class Exercise14_4 extends JFrame {

public Exercise14_4(){

add(p);

}

/**

0

MousePosition p = new MousePosition();

0

* @param args

*/

0

0

public static void main(String[] args) {

// TODO 自动生成方法存根

0

0

Exercise14_4 frame = new Exercise14_4();

le("Exercise14.4");

e(300, 200);

0

0

aultCloseOperation(_ON_CLOSE);

ationRelativeTo(null);

ible(true);

}

static class MousePosi

0

0


本文标签: 答案 语言 程序设计 网络