-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

変数を「Double」として作成していますが、変数を使用して「double」のパラメーターを受け入れる関数を定義しています。この問題を解決するには、少なくとも 3 つの方向に進むことができます。変数定義を「double」に変更するか、関数定義を「Double」を使用するように変更するか、宣言をそのままにして、Double の 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 に答える