简单的java程序,希望高手帮我改成我想要的功能……

这是我们教科书上的小程序,我实现之后,我发现在那个计算按钮没有了用武之地,我想能不能在actionPerformed(ActionEvente)if(e.getSourc... 这是我们教科书上的小程序,我实现之后,我发现在那个计算按钮没有了用武之地,我想能不能在actionPerformed(ActionEvent e)
if(e.getSource()==button){}来把默认实现的功能加入到计算按钮上,求高手帮我实现一下,应该很简单吧……
现在把源程序附上呵呵:
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
public class LoanJFrame extends JFrame implements ActionListener
{
private JTextField text_money,text_rate,text_year;
private JSpinner spin_year,spin_month;
private JButton button;
private JTable table;

public LoanJFrame()
{
super("银行贷款按月还本利息的计算");
this.setBounds(300,240,740,400);
this.setBackground(Color.LIGHT_GRAY);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel=new JPanel();
this.getContentPane().add(panel,"North");

panel.add(new JLabel("贷款金额"));
text_money=new JTextField("100000",6);
panel.add(text_money);

panel.add(new JLabel("元 贷款利率"));
text_rate=new JTextField("0.5025",6);
panel.add(text_rate);

panel.add(new JLabel("%/月 贷款年限"));
text_year=new JTextField("5",3);
panel.add(text_year);

panel.add(new JLabel("年 起始年月"));
Calendar today=Calendar.getInstance();
int year=today.get(Calendar.YEAR);
int nextmonth=today.get(Calendar.MONTH)+1;
nextmonth=nextmonth%12+1;
if(nextmonth==1)
year++;
spin_year=new JSpinner();
spin_year.setValue(year);
panel.add(spin_year);
panel.add(new JLabel("年"));
spin_month=new JSpinner(new SpinnerNumberModel(nextmonth,1,12,1));
panel.add(spin_month);
panel.add(new JLabel("月"));
button=new JButton("计算");
panel.add(button);
button.addActionListener(this);
actionPerformed(null);
this.getContentPane().add(new JScrollPane(table));
this.setVisible(true);
}

public void actionPerformed(ActionEvent e)
{
String titles[]={"年月","本金余额(元)","月还本金(元)","月还利息(元)","月还本息(元)"};
int months=Integer.parseInt(text_year.getText())*12;
Object datas[][]=new Object[months][5];
int year=Integer.parseInt(""+spin_year.getValue());
int month=Integer.parseInt(""+spin_month.getValue());
double leavings=Double.parseDouble(""+text_money.getText());
double pay=leavings/months;
double rate=Double.parseDouble(""+text_rate.getText());
for(int i=0;i<months;i++)
{
datas[i][0]=year+"年"+month+"月";
datas[i][1]=String.format("%9.2f",leavings);
datas[i][2]=String.format("%9.2f",pay);
datas[i][3]=String.format("%9.2f",leavings*rate*0.01);
datas[i][4]=String.format("%9.2f",pay+leavings*rate*0.01);
if(month==12)
year++;
month=month%12+1;
leavings-=pay;
}
table=new JTable(datas,titles);
}

public static void main(String arg[])
{
new LoanJFrame();
}
}
展开
 我来答
wangshixi2002
2012-01-09 · TA获得超过156个赞
知道小有建树答主
回答量:436
采纳率:0%
帮助的人:165万
展开全部
import java.awt.*;
import java.util.*;
import java.awt.event.*;

import javax.swing.*;
public class LoanJFrame extends JFrame implements ActionListener
{
private JTextField text_money,text_rate,text_year;
private JSpinner spin_year,spin_month;
private JButton button;
private JTable table;
private String titles[]={"年月","本金余额(元)","月还本金(元)","月还利息(元)","月还本息(元)"};
public LoanJFrame()
{
super("银行贷款按月还本利息的计算");
this.setBounds(300,240,740,400);
this.setBackground(Color.LIGHT_GRAY);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel=new JPanel();
this.getContentPane().add(panel,"North");

panel.add(new JLabel("贷款金额"));
text_money=new JTextField("100",6);
panel.add(text_money);

panel.add(new JLabel("元 贷款利率"));
text_rate=new JTextField("0.5025",6);
panel.add(text_rate);

panel.add(new JLabel("%/月 贷款年限"));
text_year=new JTextField("1",3);
panel.add(text_year);

panel.add(new JLabel("年 起始年月"));
Calendar today=Calendar.getInstance();
int year=today.get(Calendar.YEAR);
int nextmonth=today.get(Calendar.MONTH)+1;
nextmonth=nextmonth%12+1;
if(nextmonth==1)
year++;
spin_year=new JSpinner();
spin_year.setValue(year);
panel.add(spin_year);
panel.add(new JLabel("年"));
spin_month=new JSpinner(new SpinnerNumberModel(nextmonth,1,12,1));
panel.add(spin_month);
panel.add(new JLabel("月"));
button=new JButton("计算");
panel.add(button);
button.addActionListener(this);
table=new JTable( getSzFh(),titles);
this.getContentPane().add(new JScrollPane(table));
this.setVisible(true);
}
//监听
public void actionPerformed(ActionEvent e)
{
table=new JTable(getSzFh(),titles);
this.getContentPane().add(new JScrollPane(table));
this.setVisible(true);
}

//计算 返回一个数组
public Object[][] getSzFh(){
System.out.println(text_year.getText());
int months=Integer.parseInt(text_year.getText())*12;
Object datas[][]=new Object[months][5];
int year=Integer.parseInt(""+spin_year.getValue());
int month=Integer.parseInt(""+spin_month.getValue());
double leavings=Double.parseDouble(""+text_money.getText());
double pay=leavings/months;
double rate=Double.parseDouble(""+text_rate.getText());
for(int i=0;i<months;i++)
{
datas[i][0]=year+"年"+month+"月";
datas[i][1]=String.format("%9.2f",leavings);
datas[i][2]=String.format("%9.2f",pay);
datas[i][3]=String.format("%9.2f",leavings*rate*0.01);
datas[i][4]=String.format("%9.2f",pay+leavings*rate*0.01);
if(month==12)
year++;
month=month%12+1;
leavings-=pay;
}
return datas;
}
public static void main(String arg[])
{
new LoanJFrame();
}

}
风霜紫兰
2012-01-09 · TA获得超过671个赞
知道小有建树答主
回答量:308
采纳率:0%
帮助的人:339万
展开全部
你好,其实计算那个按钮已经起作用了。只不过滚动条那个组件没有重载数据。因为你每次都new一个table了。
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
public class LoanJFrame extends JFrame implements ActionListener
{
private JTextField text_money,text_rate,text_year;
private JSpinner spin_year,spin_month;
private JButton button;
private JTable table;
private JScrollPane jsp;
public LoanJFrame()
{
super("银行贷款按月还本利息的计算");
this.setBounds(300,240,740,400);
this.setBackground(Color.LIGHT_GRAY);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel=new JPanel();
this.getContentPane().add(panel,"North");

panel.add(new JLabel("贷款金额"));
text_money=new JTextField("100000",6);
panel.add(text_money);

panel.add(new JLabel("元 贷款利率"));
text_rate=new JTextField("0.5025",6);
panel.add(text_rate);

panel.add(new JLabel("%/月 贷款年限"));
text_year=new JTextField("5",3);
panel.add(text_year);

panel.add(new JLabel("年 起始年月"));
Calendar today=Calendar.getInstance();
int year=today.get(Calendar.YEAR);
int nextmonth=today.get(Calendar.MONTH)+1;
nextmonth=nextmonth%12+1;
if(nextmonth==1)
year++;
spin_year=new JSpinner();
spin_year.setValue(year);
panel.add(spin_year);
panel.add(new JLabel("年"));
spin_month=new JSpinner(new SpinnerNumberModel(nextmonth,1,12,1));
panel.add(spin_month);
panel.add(new JLabel("月"));
button=new JButton("计算");
panel.add(button);
button.addActionListener(this);
calc();
jsp = new JScrollPane(table);
this.getContentPane().add(jsp);
this.setVisible(true);
}

public void actionPerformed(ActionEvent e)
{
if(e.getSource() == button){
calc();
jsp.setViewportView(table);
}
}
public void calc(){
String titles[]={"年月","本金余额(元)","月还本金(元)","月还利息(元)","月还本息(元)"};
int months=Integer.parseInt(text_year.getText())*12;
Object datas[][]=new Object[months][5];
int year=Integer.parseInt(""+spin_year.getValue());
int month=Integer.parseInt(""+spin_month.getValue());
double leavings=Double.parseDouble(""+text_money.getText());
double pay=leavings/months;
double rate=Double.parseDouble(""+text_rate.getText());
for(int i=0;i<months;i++)
{
datas[i][0]=year+"年"+month+"月";
datas[i][1]=String.format("%9.2f",leavings);
datas[i][2]=String.format("%9.2f",pay);
datas[i][3]=String.format("%9.2f",leavings*rate*0.01);
datas[i][4]=String.format("%9.2f",pay+leavings*rate*0.01);
if(month==12)
year++;
month=month%12+1;
leavings-=pay;
}
table=new JTable(datas,titles);
}

public static void main(String arg[])
{
new LoanJFrame();
}
}
追问
哈哈,实现了,如果说这个程序写出来能有点作用的话,我想也只有这样改了之后才算是有用,不然想算什么不能总在源代码里写吧呵呵……非常感谢……
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
1031386687
2012-01-09
知道答主
回答量:7
采纳率:0%
帮助的人:3.8万
展开全部
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式