“Trig”计算类

时间:2013-10-28 21:05:09

标签: java class inheritance

我一直在努力创建一个根据用户输入计算三角函数的gui。我在GUI部分取得了成功,但是我写的用于使用继承来保存信息的类似乎搞砸了,因为当我运行它时会出现错误说:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:181)
at TrigCalc.TrigCalcGUI$CalcButtonListener.actionPerformed(TrigCalcGUI.java:191)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at  javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6288)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6053)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4651)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4481)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4481)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:643)
at java.awt.EventQueue.access$000(EventQueue.java:84)
at java.awt.EventQueue$1.run(EventQueue.java:602)
at java.awt.EventQueue$1.run(EventQueue.java:600)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$2.run(EventQueue.java:616)
at java.awt.EventQueue$2.run(EventQueue.java:614)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:613)
at     java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

我更改了程序的顺序以便于阅读:

public class TrigCalcCon
{
public double sine;
public double cosine;
public double tangent;

public TrigCalcCon(double sin, double cos, double tan)
{
    sine = sin;
    cosine = cos;
    tangent = tan;
}

public void setSin(double s)
{
    sine = s;
}

public void setCos(double cs)
{
    cosine = cs;
}

public void setTan(double t)
{
    tangent = t;
}

public void set(double s, double cs, double t)
{
    sine = s;
    cosine = cs;
    tangent = t;
}

public double getSin()
{
    return Math.sin(sine);
}

public double getCos()
{
    return Math.cos(cosine);
}

public double getTan()
{
    return Math.tan(tangent);
}
}

这是继承类。

public class ArcTrigCalcCon extends TrigCalcCon
{
// Instance Variables
public double cosecant;
public double secant;
public double cotangent;

public ArcTrigCalcCon(double s, double cs, double t)
{
    // Inherit from the Trig Calc class
    super(s, cs, t);
    cosecant = 1/s;
    secant = 1/cs;
    cotangent = 1/t;
}

public void setCsc(double csc)
{
    cosecant = csc;
}

public void setSec(double sc)
{
    secant = sc;
}

public void setCot(double ct)
{
    cotangent = ct;
}

}

这是运行gui的演示类:

public class TrigCalcGUI extends JFrame implements ActionListener
{
// Instance Variables
private String input;
private double s = 0, cs = 0, t = 0;
private JPanel mainPanel, sinPanel, cosPanel, tanPanel, cscPanel, secPanel, 
        cotPanel, buttonPanel, inputPanel, displayPanel; // Panel Display
private JLabel inputLabel;
private JTextField sinTF, cosTF, tanTF, secTF,
        cscTF, cotTF, inputTF; //Text Fields for sin, cos, and tan, and inverse
private JButton calcButton, clearButton; // Calculate and Exit Buttons

// Object
ArcTrigCalcCon trC = new ArcTrigCalcCon(s, cs, t);

public TrigCalcGUI()
{
    // title bar text.
    super("Trig Calculator");
    // Corner exit button action.
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Create main panel to add each panel to
    mainPanel = new JPanel();
    mainPanel.setLayout(new GridLayout(3,2));
    displayPanel = new JPanel();
    displayPanel.setLayout(new GridLayout(3,2));

    // Assign Panel to each variable
    inputPanel = new JPanel();
    sinPanel = new JPanel();
    cosPanel = new JPanel();
    tanPanel = new JPanel();
    cscPanel = new JPanel();
    secPanel = new JPanel();
    cotPanel = new JPanel();
    buttonPanel = new JPanel();

    // Call each constructor
    buildInputPanel();
    buildSinCosTanPanels();
    buildCscSecCotPanels();
    buildButtonPanel();

    // Add each panel to content pane
    displayPanel.add(sinPanel);
    displayPanel.add(cscPanel);
    displayPanel.add(cosPanel);
    displayPanel.add(secPanel);
    displayPanel.add(tanPanel);
    displayPanel.add(cotPanel);

    // Add three content panes to GUI
    mainPanel.add(inputPanel, BorderLayout.NORTH);
    mainPanel.add(displayPanel, BorderLayout.CENTER);
    mainPanel.add(buttonPanel, BorderLayout.SOUTH);


    //add mainPanel
    this.add(mainPanel);
    // size of window to content
    this.pack();

    // display window
    setVisible(true);
}

public static void main(String[] args)
{
    new TrigCalcGUI();
}

private void buildInputPanel()
{
    inputLabel = new JLabel("Enter a Value: ");
    inputTF = new JTextField(5);

    inputPanel.add(inputLabel);
    inputPanel.add(inputTF);
}

// Building Constructor for sinPanel cosPanel, and tanPanel
private void buildSinCosTanPanels()
{
    // Set layout and border for sinPanel
    sinPanel.setLayout(new GridLayout(2,2));
    sinPanel.setBorder(BorderFactory.createTitledBorder("Sine"));

    // 
    sinTF = new JTextField(5);
    sinTF.setEditable(false);

    sinPanel.add(sinTF);

    // Set layout and border for cosPanel
    cosPanel.setLayout(new GridLayout(2,2));
    cosPanel.setBorder(BorderFactory.createTitledBorder("Cosine"));

    cosTF = new JTextField(5);
    cosTF.setEditable(false);

    cosPanel.add(cosTF);

    // Set layout and border for tanPanel
    tanPanel.setLayout(new GridLayout(2,2));
    tanPanel.setBorder(BorderFactory.createTitledBorder("Tangent"));

    tanTF = new JTextField(5);
    tanTF.setEditable(false);

    tanPanel.add(tanTF);
}

// Building Constructor for cscPanel secPanel, and cotPanel
private void buildCscSecCotPanels()
{
    // Set layout and border for cscPanel
    cscPanel.setLayout(new GridLayout(2,2));
    cscPanel.setBorder(BorderFactory.createTitledBorder("Cosecant"));

    // 
    cscTF = new JTextField(5);
    cscTF.setEditable(false);

    cscPanel.add(cscTF);

    // Set layout and border for secPanel
    secPanel.setLayout(new GridLayout(2,2));
    secPanel.setBorder(BorderFactory.createTitledBorder("Secant"));

    secTF = new JTextField(5);
    secTF.setEditable(false);

    secPanel.add(secTF);

    // Set layout and border for cotPanel
    cotPanel.setLayout(new GridLayout(2,2));
    cotPanel.setBorder(BorderFactory.createTitledBorder("Cotangent"));

    cotTF = new JTextField(5);
    cotTF.setEditable(false);

    cotPanel.add(cotTF);
}

private void buildButtonPanel()
{
    // Create buttons and add events
    calcButton = new JButton("Calculate");
    calcButton.addActionListener(new CalcButtonListener());
    clearButton = new JButton("Clear");
    clearButton.addActionListener(new ClearButtonListener());

    buttonPanel.add(calcButton);
    buttonPanel.add(clearButton);
}

@Override
public void actionPerformed(ActionEvent e)
{

}

private class CalcButtonListener implements ActionListener
{
      public void actionPerformed(ActionEvent ae)
      {
          // Declare boolean variable
          boolean incorrect = true;

          // Set input variable to input text field text
          input = inputTF.getText();

          ImageIcon newIcon;
          ImageIcon frowny = new 
                          ImageIcon(TrigCalcGUI.class.getResource("/Sad_Face.png"));
                  Image gm = frowny.getImage();
                  Image newFrowny = gm.getScaledInstance(100, 100,
                          java.awt.Image.SCALE_FAST);
                  newIcon = new ImageIcon(newFrowny); 

          // If boolean is true, throw exception
          if(incorrect)
          {
              try{Double.parseDouble(input); incorrect = false;}

              catch(NumberFormatException nfe)
              {
                  String s = "Invalid Input "
                          + "/n Input Must Be a Numerical value."
                          + "/nPlease Press Ok and Try Again";

                  JOptionPane.showMessageDialog(null, s, "Invalid",
                      JOptionPane.ERROR_MESSAGE, newIcon);

                  inputTF.setText("");
                  inputTF.requestFocus();
              }
          }

          // If boolean is not true, proceed with output
          if (incorrect != true)
          {

              /* Set each text field's output to the String double value 
               * of inputTF
               */
              sinTF.setText(input);
              cosTF.setText(input);
              tanTF.setText(input);
              cscTF.setText(input);
              secTF.setText(input);
              cotTF.setText(input);
          }
      }
}

/**
 *  Private inner class that handles the event when
 *  the user clicks the Exit button. 
 */

private class ClearButtonListener implements ActionListener
{
  public void actionPerformed(ActionEvent ae)
  {
      // Clear field
      sinTF.setText("");
      cosTF.setText("");
      tanTF.setText("");
      cscTF.setText("");
      secTF.setText("");
      cotTF.setText("");

        // Clear textfield and set cursor focus to field
        inputTF.setText("");
        inputTF.requestFocus();
   }
}
}

如果可能的话,我可能需要一些帮助,因为我是一个视觉学习者。我知道这可能很简单,因为我是初学者,所以有点难以理解。

3 个答案:

答案 0 :(得分:0)

您将变量创建为“Double”,但定义了使用它们接受“double”参数的函数。您可以至少在3个方向上解决此问题;将变量定义更改为“double”或将函数定义更改为使用“Double”,或单独保留声明并使用双精度的doubleValue调用函数。

ArcTrigCalcCon trC = new ArcTrigCalcCon(s.doubleValue(), cs.doubleValue(), t.doubleValue());

答案 1 :(得分:0)

就在这里:

ArcTrigCalcCon trC = new ArcTrigCalcCon(s, cs, t);

您正在尝试使用TrigCalcCon构造函数创建新的ArcTrigCalcCon对象,并且构造函数不会在Java中继承。您必须创建一个构造函数,其参数与超类构造函数相同,并将它们与super(s, cs, t);一起传递,或者使用您提供的6个双构造函数。

答案 2 :(得分:0)

ArcTrigCalcCon的构造函数有六个参数,但实际上只使用其中三个参数。在该构造函数中根本不使用最后三个参数,因此如果您愿意,可以删除它们。

现在,当您尝试在ArcTrigCalcCon类中创建TrigCalcGui对象时,您尝试仅将三个参数传递给构造函数,而不是全部六个;这就是编译器给你一个错误的原因。你可以做两件事之一

  • 更改您正在创建对象的行,传递所有六个参数而不是三个,
  • 或者按照我在上面第一段中的建议更改构造函数,这样它只需要三个参数。

任何一种行动都会使错误消失。