java applet wiki_如何在JAVA中使用swing applet?(How to use swing applet in JAVA?)

如何在JAVA中使用swing applet?(How to use swing applet in JAVA?)

问题描述 (Problem Description)

如何在JAVA中使用swing applet?

解决方案 (Solution)

下面的示例演示了如何通过实现ActionListener和创建JLabel来在JAVA中使用Swing Applet。import javax.swing.*;

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

public class SApplet extends Applet implements ActionListener {

TextField input,output;

Label label1,label2;

Button b1;

JLabel lbl;

int num, sum = 0;

public void init() {

label1 = new Label("please enter number : ");

add(label1);

label1.setBackground(Color.yellow);

label1.setForeground(Color.magenta);

input = new TextField(5);

add(input);

label2 = new Label("Sum : ");

add(label2);

label2.setBackground(Color.yellow);

label2.setForeground(Color.magenta);

output = new TextField(20);

add(output);

b1 = new Button("Add");

add(b1);

b1.addActionListener(this);

lbl = new JLabel("Swing Applet Example. ");

add(lbl);

setBackground(Color.yellow);

}

public void actionPerformed(ActionEvent ae ){

try {

num = Integer.parseInt(input.getText());

sum = sum+num;

input.setText("");

output.setText(Integer.toString(sum));

lbl.setForeground(Color.blue);

lbl.setText("Output of the second Text Box : " + output.getText());

} catch(NumberFormatException e) {

lbl.setForeground(Color.red);

lbl.setText("Invalid Entry!");

}

}

}

结果 (Result)

上面的代码示例将在启用Java的Web浏览器中生成以下结果。View in Browser.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值