import java.util.*;
import java.awt.Color;
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,800,400);
this.setBackground(Color.lightGray);
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 mon=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+"年"+mon+"月";
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(mon==12)
year++;
mon=mon%12+1;
leavings-=pay;
}
table=new JTable(datas,titles);
}
public static void main(String arg[])
{
new LoanJFrame();
}
}
我检查不出问题,难道我的Java系统库遭病毒破坏了?(是不是想太多?)
------解决方案--------------------
是swing吧?
你在界面看下 button 的事件 是不是你后台的那个事件
------解决方案--------------------
button.addActionListener(this);
------解决方案--------------------
想太多了。你那个button要add到父容器中:
package com.withiter.csdn.test;
import java.util.*;
import java.awt.Color;
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,800,400);
this.setBackground(Color.lightGray);
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("%/月 贷款年限"));