怎样把这个JAVA变成JAVA Applet,可以放到网页中的那种

怎样把这个JAVA变成JAVA Applet,可以放到网页中的那种,第1张

public claas MyApplet extend Applet

{

放程序,主要在方法paint()中

}

网页<body>郑凳中

<applet code = "包名.MyApplet" height=......>

<param name=....value = ...>孝掘//传参

</喊慎旅applet>

源代码:

import java.applet.*

import java.awt.*

import javax.swing.*

import java.awt.event.*

public class TestTextFieldApplet extends JApplet implements ActionListener

{

JTextField jtfNumber1,jtfNumber2 ,jtfResult

JLabel jlbAdd

JButton jbtEquals

public TestTextFieldApplet()

{

//super(title)

Container cp = this.getContentPane()

cp.setLayout(new FlowLayout())

cp.add(jtfNumber1=new JTextField(5))

cp.add(jlbAdd = new JLabel(" + "))

cp.add(jtfNumber2=new JTextField(5))

cp.add(jbtEquals = new JButton(" = "))

cp.add(jtfResult =new JTextField(5))

jtfResult.setEditable(false)

jbtEquals.addActionListener(this)

}

public void init()

{

TestTextFieldApplet frame = new TestTextFieldApplet()

//frame.setSize(300,100)

//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

//frame.setVisible(true)

}

public void actionPerformed(ActionEvent e)

{

double n1 = 0,n2 = 0

n1 = Double.parseDouble(jtfNumber1.getText())

n2 = Double.parseDouble(jtfNumber2.getText())

jtfResult.setText(String.valueOf(n1+n2))

}

}

HTML文件hello.html:

<html>

<head>

<title>TestTextFieldApplet<唤老/title>

</head>

<body>

<applet code="TestTextFieldApplet.class" width="400" height="300"></applet>

</body>

</html>

然后用浏览器打开 hello.html就可埋链轿以弯肆

Hope this helps :)

随意写了租基下,不是如型禅很好,楼主可参考渣尘下

=======================================

import java.awt.*

import javax.swing.*

import java.awt.event.*

public class MyApplet extends JApplet {

/** Creates a new instance of MyApplet */

public MyApplet() {

}

public void init(){

myFont = new Font("Dialog",Font.BOLD,16)

Bigger = new JButton("BIGGER")

Smaller = new JButton("Smaller")

jPanel1 = new JPanel()

jPanel2 = new JPanel()

jLabel1 = new JLabel("This is a test!!")

jLabel1.setFont(myFont)

jPanel1.setLayout(new FlowLayout())

jPanel1.add(Bigger)

jPanel1.add(Smaller)

jPanel2.add(jLabel1)

Container cp= this.getContentPane()

cp.setLayout(new BorderLayout())

cp.add(jPanel1,BorderLayout.SOUTH)

cp.add(jPanel2,BorderLayout.CENTER)

Bigger.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent evt) {

BiggerActionPerformed(evt)

}

})

Smaller.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent evt) {

SmallerActionPerformed(evt)

}

})

this.setVisible(true)

}

private void BiggerActionPerformed(ActionEvent evt){

int size = myFont.getSize()+2

myFont = new Font("Dialog",Font.BOLD,size)

this.jLabel1.setFont(myFont)

}

private void SmallerActionPerformed(ActionEvent evt){

int size = myFont.getSize()-2

myFont = new Font("Dialog",Font.BOLD,size)

this.jLabel1.setFont(myFont)

}

private JButton Bigger,Smaller

private JPanel jPanel1,jPanel2

private JLabel jLabel1

private Font myFont

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12403161.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存