}
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语言程序设计(郑莉)第九章课后习题答案



