java 如何把一个小程序改写成applet
importjava.awt.*;importjavax.swing.*;importjava.awt.event.*;publicclassTestTextFielde...
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class TestTextField extends JFrame implements ActionListener
{
JTextField jtfNumber1,jtfNumber2 ,jtfResult;
JLabel jlbAdd;
JButton jbtEquals;
public TestTextField(String title)
{
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 static void main(String[] args)
{
TestTextField frame = new TestTextField("Computer");
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));
}
}
就是算一个数加另一个数输出一个数的,如何变成网页上的applet?带上网页……没学过html…… 展开
import javax.swing.*;
import java.awt.event.*;
public class TestTextField extends JFrame implements ActionListener
{
JTextField jtfNumber1,jtfNumber2 ,jtfResult;
JLabel jlbAdd;
JButton jbtEquals;
public TestTextField(String title)
{
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 static void main(String[] args)
{
TestTextField frame = new TestTextField("Computer");
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));
}
}
就是算一个数加另一个数输出一个数的,如何变成网页上的applet?带上网页……没学过html…… 展开
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:
<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.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
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询