-1

我一直在尝试创建一个基于用户输入计算三角函数的 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();
   }
}
}

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

4

3 回答 3

0

您将变量创建为“双”,但定义了使用它们来接受“双”参数的函数。你至少可以从 3 个方向来解决这个问题;将变量定义更改为“double”或将函数定义更改为使用“Double”,或者不理会声明并使用 DoubleValue 的 doubleValue 调用函数。

ArcTrigCalcCon trC = new ArcTrigCalcCon(s.doubleValue(), cs.doubleValue(), t.doubleValue());
于 2013-10-28T21:59:57.453 回答
0

就在这儿:

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

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

于 2013-10-28T22:00:44.500 回答
0

你的构造函数ArcTrigCalcCon有六个参数,但你实际上只使用了其中的三个。最后三个参数在该构造函数中根本没有使用,因此您可以根据需要删除它们。

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

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

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

于 2013-10-28T23:00:13.510 回答