Java语言程序设计第九章课后习题答案1.编写一个程序,该程序绘制一个5×9的网络,使用drawLine方法。
//NetWork类import java.awt.Graphics;import javax.swing.JFrame;public class NetWork extends JFrame{public NetWork(){// 设置窗体大小this.setSize(130, 130);//设置窗体大小不可改变this.setResizable(false);// 设置默认关闭方式,关闭窗体的同时结束程序this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 将窗体显示出来this.setVisible(true);}//横纵格之间都间隔10像素,起点在(20,40)public void paint(Graphics g){//绘制横向线for(int i=0;i<=5;i++){g.drawLine(20, 40+i*10, 110, 40+i*10);}//绘制纵向线for(int i=0;i<=9;i++){g.drawLine(20+i*10, 40, 20+i*10, 90);}}}//test9_1类public class test9_1 {public static void main(String[] args){new NetWork();}}运行结果:2.编写一个程序,该程序以不同的颜色随机产生三角形,每个三角形用不同的颜色进行填充。
//Triangle类import java.awt.Color;import java.awt.Graphics;import java.util.Random;import javax.swing.JFrame;public class Triangle extends JFrame{Random rnd = new Random();//这里定义4个三角形int[][] x=new int[4][3];int[][] y=new int[4][3];int[][] color=new int[4][3];public Triangle(){for(int i=0;i<4;i++){for(int j=0;j<3;j++){color[i][j]=rnd.nextInt(255);x[i][j]=rnd.nextInt(i*100+100);y[i][j]=rnd.nextInt(i*100+100)+50;//加50像素是为了避免顶到窗体上沿}}//窗体标题this.setTitle("随机三角形");//窗体大小this.setSize(500,500);//窗体大小不可变this.setResizable(false);//关闭窗体的同时结束程序this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//显示窗体this.setVisible(true);}public void paint(Graphics g){for(int i=0;i<4;i++){g.setColor(new Color(color[i][0],color[i][1],color[i][2]));g.fillPolygon(x[i], y[i], 3);}}}//test9_2public class test9_2 {public static void main(String[] args){new Triangle();}}运行结果:3.编写一个Applet,该程序请求用户输入圆的半径,然后显示该圆的直径、周长和面积。
//test9_3import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class test9_3 extends JApplet {//声明5个标签private JLabel jLabel1;private JLabel jLabel2;private JLabel jLabel3;private JLabel jLabel4;private JLabel jLabel5;//1个单行文本private JTextField textOfRadius;//2个按钮private JButton jButton1;private JButton jButton2;//初始化public void init() {try {java.awt.EventQueue.invokeAndWait(new Runnable() {public void run() {initComponents();}});} catch (Exception ex) {ex.printStackTrace();}}private void initComponents() {//声明8个组件jLabel1 = new JLabel("输入圆的半径:", SwingConstants.CENTER);jLabel2 = new JLabel("圆的周长:", SwingConstants.CENTER);jLabel3 = new JLabel("", SwingConstants.CENTER);jLabel4 = new JLabel("圆的面积:", SwingConstants.CENTER);jLabel5 = new JLabel("", SwingConstants.CENTER);textOfRadius = new JTextField("半径");jButton1 = new JButton("计算");jButton2 = new JButton("退出");//按钮添加监听器jButton1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) {jButton1ActionPerformed(evt);}});//按钮添加监听器jButton2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) {jButton2ActionPerformed(evt);}});//声明定义内容面板,并且设置其布局格式为:4行2列格子Container c = getContentPane();c.setLayout(new GridLayout(4, 2));//将8个组件加入到内容面板c.add(jLabel1);c.add(textOfRadius);c.add(jLabel2);c.add(jLabel3);c.add(jLabel4);c.add(jLabel5);c.add(jButton1);c.add(jButton2);}// 求周长方法private String Round(double a) {double perimeter = a * 2 * 3.14;String s = new String(String.valueOf(perimeter));return s;}// 求面积方法private String Area(double a) {double area = a * a * 3.14;String s = new String(String.valueOf(area));return s;}//点击“计算”按钮jButton1触发的方法private void jButton1ActionPerformed(ActionEvent evt) {//捕获单文本输入非数字的异常try {String s = textOfRadius.getText();//获得单文本字符double a = Double.valueOf(s).floatValue();//字符转化为双精度jLabel3.setText(Round(a));//标签内容为周长jLabel5.setText(Area(a));//标签内容为面积} catch (NumberFormatException r) {//单文本为非数字弹出提示“输入错误”框JOptionPane.showMessageDialog(this, "请输入数字类型", "输入错误",JOptionPane.WARNING_MESSAGE);textOfRadius.setText("");}}//点击“退出”按钮jButton2触发的方法public void jButton2ActionPerformed(ActionEvent evt) {System.exit(0);}}运行结果:编译text9_3.java产生字节码文件test9_3.class,接下来需要编写一个HTML文件text9_3.html来嵌入text9_3.class,代码如下:<html><applet code="test9_3.class"></applet></html>将test9_3.html文件和test9_3.class文件放在同一个目录下,在浏览器中打开这个test9_3.html文件,实现的效果如下:4.编写一个Applet,向其输入五个数,然后以条形图(bar graph)的形式来表示这些数。
5.编写一个绘制圆形的程序,当鼠标在绘制区域中单击时,该正方形的左上角顶点应准确的跟随鼠标光标移动,重绘该圆形。
//MyJFrame类import java.awt.Graphics;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import javax.swing.JFrame;public class MyJFrame extends JFrame implements MouseListener{ int x=50;int y=50;int radius=50;public MyJFrame(){this.setTitle("绘制圆形");this.setSize(200,200);this.setResizable(false);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.addMouseListener(this);this.setVisible(true);}public void paint(Graphics g){g.drawOval(x, y, radius, radius);}public void mouseClicked(MouseEvent e) {// TODO Auto-generated method stubthis.x=e.getX();this.y=e.getY();this.repaint();System.out.println("x: " + e.getX() + "\ny: " + e.getY());}public void mouseEntered(MouseEvent e) {// TODO Auto-generated method stub}public void mouseExited(MouseEvent e) {// TODO Auto-generated method stub}public void mousePressed(MouseEvent e) {// TODO Auto-generated method stub}public void mouseReleased(MouseEvent e) {// TODO Auto-generated method stub}}//test9_5public class test9_3 {public static void main(String[] args){new MyJFrame();}}运行结果:6.编写一个“猜数”程序:该程序随机在1到100的范围内选择一个供用户猜测的整数,然后改程序显示提示信息,要求用户输入一个1到100之间的整数,根据输入偏大、偏小、正确,程序将显示不同的图标。