为什么java中一个程序改成Applet形式小程序启动,但是里面没内容?

2024-10-31 12:09:01
推荐回答(1个)
回答1:

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:


TestTextFieldApplet






然后用浏览器打开 hello.html就可以
Hope this helps :)
参考资料: http://zhangliancheng.com