错误:非法启动表达式1错误

时间:2016-09-08 11:43:57

标签: java swing user-interface import

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;    
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.lang.*;
import java.util.*;
public class Life {
public static void main (String[] args){  
  JFrame frame = new JFrame("Interest Calculator");
  frame.setVisible(true);
  frame.setSize(300,100);
  frame.setBackground(Color.magenta);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  JPanel panel = new JPanel();
  frame.add(panel);


  JButton button = new JButton("Simple Interest");
  panel.add(button);
  button.addActionListener (new Action1());

  JButton button2 = new JButton("Compound Interest");
  panel.add(button2);
  button2.addActionListener (new Action2()); 
}
static class Action1 implements ActionListener {        
      public void actionPerformed (ActionEvent e) {     
    JFrame frame2 = new JFrame("Simple");
    frame2.setVisible(true);
    frame2.setSize(300,100);
    JPanel panel = new JPanel();
    frame2.add(panel);

    JButton button = new JButton("Ordinary"); 
        panel.add(button); 
    button.addActionListener (new Ordinary());


  JButton button2 = new JButton("Exact");
  panel.add(button2);     
  button2.addActionListener (new Exact());       
  }
}   
static class Action2 implements ActionListener {        
  public void actionPerformed (ActionEvent e) {     
    JFrame frame3 = new JFrame("Compound Interest");
    frame3.setVisible(true);
    frame3.setSize(300,100);

  JPanel panel = new JPanel();
  frame3.add(panel);

  JButton button = new JButton("Compounded");
  panel.add(button);

  JButton button2 = new JButton("Continously");
  panel.add(button2);
  }  
}
    static class Ordinary implements ActionListener {
    public void actionPerformed (ActionEvent e) { 
    JFrame frame4 = new JFrame("Simple");
    frame4.setVisible(true);
    frame4.setSize(300,100);

   JPanel panel = new JPanel();
   frame4.add(panel);

       JButton button = new JButton("Approximate");
  panel.add(button);
  button.addActionListener (new Approximate());

  JButton button2 = new JButton("Actual");
  panel.add(button2);
}   
}
static class Exact implements ActionListener {
public void actionPerformed (ActionEvent e) { 
JFrame frame5 = new JFrame("Exact");
frame5.setVisible(true);
frame5.setSize(300,100);

   JPanel panel = new JPanel();
  frame5.add(panel);

   JButton button = new JButton("Approximate");
  panel.add(button);



  JButton button2 = new JButton("Actual");
  panel.add(button2);
}   
}
static class Approximate implements ActionListener {
public void actionPerformed (ActionEvent e) {

JFrame frame6 = new JFrame("Simple");
frame6.setVisible(true);
frame6.setSize(300,100);
JPanel panel = new JPanel();
   frame6.add(panel);
   frame6.setVisible(true);
frame6.setSize(300,300);
frame6.setLayout(null);
frame6.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextField P = new JTextField();
panel.add(P);
P.setText("Enter Principal");
JTextField r = new JTextField();
panel.add(r);
r.setText("Enter Rate");
JTextField t = new JTextField();
panel.add(t);
t.setText("Enter Year");
JButton button = new JButton("Calculate");
panel.add(button);
button.addActionListener (new button());
 static class button implements ActionListener {
    public void actionPerformed (ActionEvent e) {

float P,r,t,result ;
P = Float.parseFloat(P.getText());
r = Float.parseFloat(r.getText());
t = Float.parseFloat(t.getText());
result = P*r/100*t/360;
button.setText(String.valueof(result));
}
}
}
}
}

所以这是我创建复合计算器的代码,但我有一个问题,那就是非法启动表达式。请帮助我完成我的项目。

1 个答案:

答案 0 :(得分:0)

下面是导致问题的代码的部分修复部分。下次包含完整的错误代码,并尝试仅显示代码的相关部分。

ActionListener listener = new ActionListener() {
    public void actionPerformed (ActionEvent e) {
        float P,r,t,result ;
        //P = Float.parseFloat(P.getText()); // P makes no sens here - its float !!! and uninitialized
        //r = Float.parseFloat(r.getText()); // same for r
        //t = Float.parseFloat(t.getText()); // same for t
        result = P*r/100*t/360;
        button.setText(String.valueOf(result));
    }
};
button.addActionListener (listener);