第一范文网 - 专业文章范例文档资料分享平台

Java语言程序设计(郑莉)第九章课后习题答案

来源:用户分享 时间:2025/6/1 9:08:12 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

}

this.add(jl3); this.add(jtf1); this.add(jtf2); this.add(jtf3); this.add(jb); this.add(jl4);

this.setTitle(\加减乘除\); this.setSize(250, 100); this.setLocation(200, 200);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true);

public void actionPerformed(ActionEvent e) {

float n1 = 0; float n2 = 0;

System.out.println(\输入符号为:\ + this.jtf2.getText()); n1 = Float.parseFloat(this.jtf1.getText()); n2 = Float.parseFloat(this.jtf3.getText()); if (this.jtf2.getText().equals(\)) { this.jl4.setText(\结果为:\ + (n1 + n2)); } else {

if (this.jtf2.getText().equals(\)) { this.jl4.setText(\结果为:\ + (n1 - n2)); } else { }

if (this.jtf2.getText().equals(\)) { this.jl4.setText(\结果为:\ + (n1 * n2)); } else { }

if (this.jtf2.getText().equals(\)) { this.jl4.setText(\结果为:\ + (n1 / n2)); } else {

this.jl4.setText(\输入有误!\); }

}

}

}

public static void main(String[] args) { }

new Test9_14();

运行结果:

15.扩充上一个练习,先弹出一对话框,输入密码正确,方能进行计算界面。

import java.awt.*;

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

public class Test9_15 extends JFrame implements ActionListener { private JLabel jl1, jl2, jl3, jl4; private JTextField jtf1, jtf2, jtf3; private JButton jb; public Test9_15() { this.jl1 = new JLabel(\数字1\ this.jl2 = new JLabel(\符号\ this.jl3 = new JLabel(\数字2\ this.jl4 = new JLabel(); this.jtf1 = new JTextField(); this.jtf2 = new JTextField(); this.jtf3 = new JTextField(); this.jb = new JButton(\结果\

}

this.jb.addActionListener(this); this.setLayout(new GridLayout(3, 3)); this.add(jl1); this.add(jl2); this.add(jl3); this.add(jtf1); this.add(jtf2); this.add(jtf3); this.add(jb); this.add(jl4);

this.setTitle(\加减乘除\this.setSize(250, 100); this.setLocation(450, 300);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true);

public void actionPerformed(ActionEvent e) { float n1 = 0; float n2 = 0;

System.out.println(\输入符号为:\n1 = Float.parseFloat(this.jtf1.getText()); n2 = Float.parseFloat(this.jtf3.getText()); if (this.jtf2.getText().equals(\

this.jl4.setText(\结果为:\} else {

if (this.jtf2.getText().equals(\ this.jl4.setText(\结果为:\} else {

if (this.jtf2.getText().equals(\ this.jl4.setText(\结果为:\} else { if (this.jtf2.getText().equals(\ this.jl4.setText(\结果为:\

}

}

}

}

} else { }

this.jl4.setText(\输入有误!\

public static void main(String[] args) {

String inputValue = JOptionPane.showInputDialog(\请输入密码(我爱苍老师):\ while(!inputValue.equals(\我爱苍老师\ JOptionPane.showMessageDialog(null, \密码错误\提示\JOptionPane.ERROR_MESSAGE); }

}

inputValue = JOptionPane.showInputDialog(\请输入密码(我爱苍老师):\ break; }

new Test9_15();

运行结果:

16.改造上一个练习为Applet。

编译text9_15.java产生字节码文件test9_15.class,接下来需要编写一个 HTML文件text9_16.html来嵌入text9_15.class,代码如下:

将test9_16.html文件和test9_15.class文件放在同一个目录下,在浏览器中打开这个test9_16.html文件,就有你想要的效果。

17.编写一个程序,以随机的颜色和宽度绘制10条长度随机的直线,使用Line2D.Double对象和Fraphics2D类的方法来绘制直线。该绘制区域设置为300×400.

import java.awt.*;

import java.awt.geom.Line2D;

import javax.swing.*;

public class Test9_17 extends JFrame {

public static void main(String[] args) {

new Test9_17(); }

}

//随即长度

int x1 = (int) (Math.random() * 300); int y1 = (int) (Math.random() * 400); int x2 = (int) (Math.random() * 300); int y2 = (int) (Math.random() * 400); e.drawLine(x1, y1, x2, y2);

//10次

for (int i = 0; i < 10; i++) {

// 随即颜色

int r = (int) (Math.random() * 255); int g = (int) (Math.random() * 255); int b = (int) (Math.random() * 255); e.setColor(new Color(r, g, b));

public void paint(Graphics e) { }

public Test9_17() {

this.setTitle(\随机绘直线\); this.setSize(400, 400);

this.setLocation(500, 300);// 窗口显示的位置

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true);

Java语言程序设计(郑莉)第九章课后习题答案.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c2i3wv61yi252amw9lhr375cln2z0an008f4_7.html(转载请注明文章来源)
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top