Java Applet查找素?cái)?shù)小程序代碼實(shí)例
1. Applet 這個(gè)遠(yuǎn)古的東西,今天我同學(xué)讓我?guī)退纯创a,說(shuō)applet運(yùn)行出錯(cuò)。額,反正閑著也是閑著,看看唄 ,結(jié)果看到代碼。。。
2.就是實(shí)現(xiàn)這破玩意

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('素?cái)?shù)有:')); add(output); input1.addActionListener(this); input2.addActionListener(this); output.addActionListener(this); }public void paint(Graphics g) { int i; for(i=0;i<length;i++) g.drawString(Integer.toString(res[i]), 50, 50); } public void actionPerformed(ActionEvent e) { int i = c,j=0,k=0; if(e.getSource()==input2) { c=Integer.parseInt(input1.getText()); d=Integer.parseInt(input2.getText());for(;i<=d;i++) { for(j=2;j<d;j++) { if(j%i==0) break;} if(j==d) {res[k]=i; k++; } }length=k; } }}
修改后的代碼
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 < 2)return; } repaint(); f(this.getGraphics()); } @Override public void init() { this.setSize(800, 600); add(prompt1); add(input1); add(prompt2); add(input2); add(new Label('素?cái)?shù)有:')); add(output); input2.addActionListener(this); for (int i = 2; i <= 100000; i++) { if (isPrime(i)) {res[++k] = i; } } } private boolean isPrime(int r) { for (int i = 2; i < r; i++) { if (r % i == 0) {return false; } } return true; } void f(Graphics g) { for (int i = 1; i < d; i++) { if(res[i]>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) { }}
創(chuàng)建HTML文件
值得注意的是到目前為止你已經(jīng)確切的遵循相同的步驟,如果你在創(chuàng)建一個(gè)Java應(yīng)用程序。Applet被 創(chuàng)建并保存在一個(gè)文本文件中,通過(guò)javac compiler已經(jīng)進(jìn)行編譯。
Java Applets不同于Java 應(yīng)用程序,當(dāng)它們運(yùn)行的時(shí)候。現(xiàn)在需要的是涉及FirstApplet.class文件 的網(wǎng)頁(yè)。記住,類文件是你的applet已編譯的版本;這是你的電腦可以知道并執(zhí)行的文件。
創(chuàng)建html文件“First-App.html:
<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>My First Java Applet</title></head><body>>Here’s my first Java Applet:<applet code='FirstApplet.class' height ='300'></applet></body></html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. IntelliJ IDEA導(dǎo)入jar包的方法2. docker鏡像完全卸載的操作步驟3. IntelliJ IDEA配置Tomcat服務(wù)器的方法4. idea刪除項(xiàng)目的操作方法5. 使用Maven 搭建 Spring MVC 本地部署Tomcat的詳細(xì)教程6. IntelliJ IDEA調(diào)整字體大小的方法7. idea打開多個(gè)窗口的操作方法8. idea導(dǎo)入maven項(xiàng)目的方法9. IntelliJ IDEA設(shè)置自動(dòng)提示功能快捷鍵的方法10. IntelliJ IDEA安裝插件的方法步驟

網(wǎng)公網(wǎng)安備