qq_38280197
qq_38280197
采纳率100%
2017-12-25 14:12

java 求大神看看哪里出错了,代码想这样实现:运用两个按钮分别打开两个新界面,并实现自己的功能

20
已采纳

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Date;
public class RateanLoan extends JFrame implements ActionListener
{
JButton Rate=new JButton("汇率计算器");
JButton Loan=new JButton("贷款计算器");
JTextField jtf;
JLabel jl1;
JLabel jl2;
JLabel jl3;
JTextField rmb;
JTextField dollar;
JTextField result;
JButton run;
JFrame f1,f2;
JTextField jtfAnnualInterestRate;
JTextField jtfNumberOfYears;
JTextField jtfLoanAmount;
JTextField jtfMonthlyPayment;
JTextField jtfTotalPayment;

public RateanLoan()
{

    super("贷款及汇率换算程序");
    setSize(600,500);
    setVisible(true);
    setLayout(new FlowLayout());

    add(Rate,"打开汇率计算器");
    add(Loan,"打开贷款计算器");



}

public void actionPerformed(ActionEvent e)
{
if(e.getSource()==Rate)
{
new rateFrame();

}
else if(e.getSource()==Loan)
{

new loanFrame();
}

}
class rateFrame extends JFrame
{
f1=new JFrame("汇率计算器");
Container container=f1.getContentPane();
f1.setVisible(true);
f1.setSize(300, 300);
f1.setLayout(new GridLayout(3,1,10,10));
JPanel jp1=new JPanel(new BorderLayout());
jl1=new JLabel("汇率:");

f1.setDefaultCloseOperation(EXIT_ON_CLOSE);
jp1.add(BorderLayout.WEST,jl1);
jtf=new JTextField(getWidth()/3);
jtf.setText("6.5785");
jp1.add(BorderLayout.CENTER,jtf);
container.add(jp1);
JPanel jp2=new JPanel(new GridLayout(1,4));
jl2=new JLabel("人民币");
jp2.add(jl2);
rmb=new JTextField(" ");
jp2.add(rmb);
jl3=new JLabel("美元:");
jp2.add(jl3);
result=new JTextField;
jp2.add(result);
container.add(jp2);
JPanel jp3=new JPanel();


run=new JButton("计算");
jp3.add(run);
container.add(jp3);

run.addActionListener(new ActionListener()
{
double hv=Double.parseDouble(jtf.getText().toString());

double r=Double.parseDouble(rmb.getText().toString());

double a=r/hv;
result.setText(toString(a));

}

class loanFrame extends JFrame
{
f2=new JFrame("贷款计算器");
f2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f2.setVisible(true);
JPanel p1=newJPanel(new GridLayout(5,2);
jtfAnnualInterestRate=new JTextField("Annual Interest Rate");
jtfNumberOfYears=new JTextField("NumberOfYears");
jtfLoanAmount=new JTextField("LoanAmount");
jtfMonthlyPayment=new JTextField("MonthlyPayment");
jtfTotalPayment=new JTextField("TotalPayment");
p1.add(new JLabel("Annual Interest Rate"));
p1.add(jtfAnnualInterestRate);
p1.add(new JLabel("Number of Years"));
p1.add(jtfNumberOfYears);
p1.add(new JLabel("Loan Amount"));
p1.add(jtfLoanAmount);
p1.add(new JLabel("Monthly Payment"));
p1.add(jtfMonthlyPayment);
p1.add(new JLabel("Total Payment"));
p1.add(jtfTotalPayment);
p1.setBorder(new TitledBorder("请输入金额,贷款率,年数");
JPanel p2=new JPanel(new FlowLayout(FlowLayout.RIGHT));
p2.add(jbtComputeLoan);
add(p1,BorderLayout.CENTER);
add(p2,BorderLayout.SOUTH);
jbtComputeLoan.addActionListener(new ButtonListener());

public void actionPerformed(ActionEvent e)
{
double interest=Double.parseDouble(jtfAnnualInterestRate.getText());
int year=Integer.parseInt(jtfNumberOfYears.getText());
double loanAmount=Double.parseDouble(jtfLoanAmount.getText());
double monthlyInterest=interest/1200;
double monthlyPayment= loanAmount*monthlyInterest/(1-1/Math.pow(1+monthlyInterest,year*12));
double totalPayment=monthlyPayment*year*12;
jtfMonthlyPayment.setText(String.format("%.2f", monthlyPayment));
jtfTotalPayment.setText(String.format("%.2f", totalPayment));

}

}
public static void main(String[] args)

{
new RateanLoan();
}

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答

  • MonkeyBrothers 小猴子的技术笔记 4年前

    不要打击人家学习Java的信心呀!建议你还是把错误粘贴出来,代码规范还是要遵守的,这样别人也比较同意读你的代码,并且帮你找出错误,学习循循渐进,切不可放弃,要加油呀!

    点赞 评论 复制链接分享
  • qq_34701469 杨_5426 4年前

    现在没人用swing了

    点赞 评论 复制链接分享
  • caozhy 从今以后生命中的每一秒都属于我爱的人 4年前

    一看你贴出这么多代码,什么错误信息也没有提供的,就知道你是一窍不通,而且估计也没有兴趣去搞明白。所以建议你参考以下现成的代码:
    http://blog.csdn.net/u012794724/article/details/41863707
    你的现有的程序就可以扔了。

    点赞 评论 复制链接分享
  • qq_20766085 huhuhuHR 4年前

    少括号,还有swing不看也罢。去用javaweb实现吧。

    点赞 评论 复制链接分享

相关推荐