为什么java中一个程序改成Applet形式小程序启动,但是里面没内容? 10
1个回答
2015-11-27
展开全部
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 :)
参考资料: http://zhangliancheng.com
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 :)
参考资料: http://zhangliancheng.com
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询