Java Applet查找素數(shù)小程序代碼實例
1. Applet 這個遠(yuǎn)古的東西,今天我同學(xué)讓我?guī)退纯创a,說applet運行出錯。額,反正閑著也是閑著,看看唄 ,結(jié)果看到代碼。。。
2.就是實現(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('素數(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('素數(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)建一個Java應(yīng)用程序。Applet被 創(chuàng)建并保存在一個文本文件中,通過javac compiler已經(jīng)進(jìn)行編譯。
Java Applets不同于Java 應(yīng)用程序,當(dāng)它們運行的時候。現(xiàn)在需要的是涉及FirstApplet.class文件 的網(wǎng)頁。記住,類文件是你的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>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
