Java语言程序设计[编程练习题答案]

时间:2025-07-12

[编程练习题答案]Java语言程序设计(基础篇)(进阶篇)第13、14、15、24、25、26、28、29、30、31章部分习题答案

&

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

[编程练习题答案]Java语言程序设计(基础篇)(进阶篇)第13、14、15、24、25、26、28、29、30、31章部分习题答案,奇数题和部分

import javax.swing.*; import java.awt.*;

public class Exercise13_2 extends JFrame { // Create two buttons

private OvalButton jbtOk = 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

[编程练习题答案]Java语言程序设计(基础篇)(进阶篇)第13、14、15、24、25、26、28、29、30、31章部分习题答案

// inside the frame

getContentPane().setLayout(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); } }

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语言程序设计(基础篇)(进阶篇)第13、14、15、24、25、26、28、29、30、31章部分习题答案

import java.awt.*; import javax.swing.*;

public class Exercise13_8 extends JFrame { public static void main(String[] args) { JFrame 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 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 * 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

[编程练习题答案]Java语言程序设计(基础篇)(进阶篇)第13、14、15、24、25、26、28、29、30、31章部分习题答案

/ 8)),

(int)(yCenter - radius * Math.sin(2 * 2 * Math.PI / 8)));

polygon.addPoint((int)(xCenter + radius * 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)),

(int)(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 * Math.PI / 8)));

// Draw the polygon g.drawPolygon(polygon); } }

import javax.swing.*; import java.awt.*;

public class Exercise13_11 extends JFrame {

/**

* @param args */

public static void

main(String[] args) { public Exercise13_11(){ }

add(new SquareFunction());

[编程练习题答案]Java语言程序设计(基础篇)(进阶篇)第13、14、15、24、25、26、28、29、30、31章部分习题答案

}

class SquareFunction extends JPanel { }

public SquareFunction(){ }

protected void paintComponent(Graphics g){ }

super.paintComponent(g);

g.drawLine(10, this.getHeight()-20, this.getWidth()-10, g.drawLine(this.getWidth()-30, this.getHeight()-15, g.drawLine(this.getWidth()-30, this.getHeight()-25,

g.drawString("X", this.getWidth()-10, this.getHeight()-30);

g.drawLine(this.getWidth()/2, 20, this.getWidth()/2,

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()/2 + 10, 30);

Polygon p = new Polygon(); double scaleFactor = 0.01; for (int x=-100; x<=100; x++){ }

g.drawPolyline(p.xpoints, p.ypoints, p.npoints);

p.addPoint(x+this.getWidth()/2, }

// TODO 自动生成方法存根

Exercise13_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);

this.getHeight()-20);

this.getWidth()-10, this.getHeight()-20); this.getWidth()-10, this.getHeight()-20);

this.getHeight()-5);

this.getHeight()-20-(int)(scaleFactor*x*x));

…… 此处隐藏:12407字,全部文档内容请下载后查看。喜欢就下载吧 ……
Java语言程序设计[编程练习题答案].doc 将本文的Word文档下载到电脑

    精彩图片

    热门精选

    大家正在看

    × 游客快捷下载通道(下载后可以自由复制和排版)

    限时特价:7 元/份 原价:20元

    支付方式:

    开通VIP包月会员 特价:29元/月

    注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
    微信:fanwen365 QQ:370150219