以下是我写的一个用于股票计算的applet:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Gupiaojisuan extends JApplet implements ActionListener
{
JLabel mrjL,mrlL,sglL,mcjL,mclL,zcbL,zcglL,syL,pjcbL;
JTextField mrjF,mrlF,sglF,mcjF,mclF,zcbF,zcglF,syF,pjcbF;
JButton button1,button2;
double mrj,mrl,sgl,mcj,mcl,zcb,zcgl,sy,pjcb;
public void init()
{
Container container = getContentPane();
container.setLayout(new FlowLayout());
mrjL = new JLabel( "买入价 ");
container.add(mrjL);
mrjF = new JTextField(10);
container.add(mrjF);
mrlL = new JLabel( "买入量 ");
container.add(mrlL);
mrlF = new JTextField(10);
container.add(mrlF);
sglL = new JLabel( "送股量 ");
container.add(sglL);
sglF = new JTextField(10);
container.add(sglF);
mcjL = new JLabel( "卖出价 ");
container.add(mcjL);
mcjF = new JTextField(10);
container.add(mcjF);
mclL = new JLabel( "卖出量 ");
container.add(mclL);
mclF = new JTextField(10);
container.add(mclF);
zcbL = new JLabel( "总成本 ");
container.add(zcbL);
zcbF = new JTextField(10);
zcbF.setEditable(false);
container.add(zcbF);
zcglL = new JLabel( "总持股量 ");
container.add(zcglL);
zcglF = new JTextField(10);
zcglF.setEditable(false);
container.add(zcglF);
syL = new JLabel( "收益 ");
container.add(syL);
syF = new JTextField(10);
syF.setEditable(false);
container.add(syF);
pjcbL = new JLabel( "平价成本 ");
container.add(pjcbL);
pjcbF = new JTextField(10);
pjcbF.setEditable(false);
container.add(pjcbF);
button1 = new JButton( "计算 ");
button1.addActionListener(this);
container.add(button1);
}
public void actionPerformed(ActionEvent e)
{
mrj = Double.parseDouble(mrjF.getText());
mrl = Double.parseDouble(mrlF.getText());
mcj = Double.parseDouble(mcjF.getText());
mcl = Double.parseDouble(mclF.getText());
sgl = Double.parseDouble(sglF.getText());
zcb = mrj * mrl * 1.004;
zcgl = mrl + mrl / 10 * sgl;
pjcb = zcb / zcgl;
sy = mcj * mcl * 0.994;
zcglF.setText(Double.toString(zcgl));
zcbF.setText(Double.toString(zcb));
zcglF.setText(Double.toString(zcgl));
pjcbF.setText(Double.toString(pjcb));
syF.setText(Double.toString(sy));
}
}
我想请教以下如何实现如下功能:
在输入完“买入价”、“买入量”、“送股量”之后,自动的显示出“总成本”、“总持股量”以及“平均成本”的数值,再点击“计算”按钮后,显示出“收益”的数值。
初学者,程序可能写的非常乱,非常难看,请见谅……
------解决方案--------------------
是否是在输入“送股量”之后按回车,然后自动计算出结果,然后显示出“总成本”、“总持股量”以及“平均成本”的数值?如果是这样的话,那么在表示“送股量”的JTextFild中添加接收回车的事件addActionListener(),在它的事件处理函数中,设置“总成本”、“总持股量”以及“平均成本”对应的组件的文本就可以了。