微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Java Applet查找素数小程序代码实例

这篇文章主要介绍了Java Applet查找素数小程序代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

1. Applet 这个远古的东西,今天我同学让我帮他看看代码,说applet运行出错。额,反正闲着也是闲着,看看呗 ,结果看到代码。。。

2.就是实现这破玩意

package calculate; import java.applet.Applet; import java.awt.*; import java.awt.event.*; public abstract class primeNumBetween extends Applet implements ActionListener { int c=0,d=0; int[] res; int length; Label prompt1 =new Label("上限"); Label prompt2 =new Label("下限"); TextField input1 =new TextField(10); TextField input2 =new TextField(10); TextField output =new TextField(10); public void init() { add(prompt1); add(input1); add(prompt2); add(input2); add(new Label("素数有:")); add(output); input1.addActionListener(this); input2.addActionListener(this); output.addActionListener(this); } public void paint(Graphics g) { int i; for(i=0;i

修改后的代码

package chapter.array; import java.applet.Applet; import java.awt.Graphics; import java.awt.Label; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.SwingUtilities; public class ClasA extends Applet implements ActionListener { static int[] res = new int[1000000]; Label prompt1 = new Label("下限"); Label prompt2 = new Label("上限"); TextField input1 = new TextField(10); TextField input2 = new TextField(10); TextField output = new TextField(100); int c, d, k = 0; @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == input2) { c = Integer.parseInt(input1.getText()); d = Integer.parseInt(input2.getText()); output.setText(""); if (c d) { continue; } output.setText(output.getText()+" "+Integer.toString(res[i])); } // g.drawString(Integer.toString(res[i]), 50, 50); // repaint(); } @Override public void paint(Graphics g) { } }

创建HTML文件

值得注意的是到目前为止你已经确切的遵循相同的步骤,如果你在创建一个Java应用程序。Applet被 创建并保存在一个文本文件中,通过javac compiler已经进行编译。

Java Applets不同于Java 应用程序,当它们运行的时候。现在需要的是涉及FirstApplet.class文件 的网页。记住,类文件是你的applet已编译的版本;这是你的电脑可以知道并执行的文件

创建html文件“First-App.html:

My First Java Applet >Here's my first Java Applet:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐