2016-06-06 100 views
1

您好Java專家, 我想學習Java。所以我正在練習一些基本問題。在這種情況下,我的想法是從用戶那裏獲取兩個數字,然後根據用戶需求(加/減)計算這些數字。我可以輕鬆做到這一點,但我試圖在JFrame中實現這個問題,並未能實現我想要做的事情!當我試圖通過window.number1和window.number2傳遞這些數字時,出現錯誤。 錯誤消息:從一個類調用靜態方法到jframe類

window cannot be resolved to a variable 

因此,任何人可以幫我找出我需要改變我的計劃。

Calculate.java

package day1.examples; 

public class Calculate { 
    double number1; 
    double number2; 
    public double[] calculateNumber() { 
     double plus = number1+number2; 
     double minus = number1-number2; 
     return new double[] {plus,minus}; 

    } 

} 

TestCalculate.java

package day1.examples; 

import java.awt.EventQueue; 
import java.awt.Window; 

import javax.swing.JFrame; 
import javax.swing.JOptionPane; 
import javax.swing.JTextField; 
import javax.swing.JButton; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 
import javax.swing.JLabel; 
import java.awt.Font; 
import javax.swing.JComboBox; 
import javax.swing.DefaultComboBoxModel; 

public class TestCalculate { 

    private JFrame frame; 
    private JTextField textFieldNum1; 
    private JTextField textFieldNum2; 
    private JTextField textFieldTotRes; 
    private JLabel lblNewTlabelTotNum; 
    private JLabel lblNewLabelNum1; 
    private JLabel lblNewLabelNum2; 
    private JComboBox<Object> comboBoxNum; 
    private double totalres; 
    private String itemselect; 

    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        TestCalculate window = new TestCalculate(); 
        window.frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the application. 
    */ 
    public TestCalculate() { 
     initialize(); 

    } 

    /** 
    * Initialize the contents of the frame. 
    */ 
    private void initialize() { 
     frame = new JFrame(); 
     frame.setBounds(100, 100, 450, 300); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().setLayout(null); 

     textFieldNum1 = new JTextField(); 
     textFieldNum1.setBounds(10, 37, 106, 25); 
     frame.getContentPane().add(textFieldNum1); 
     textFieldNum1.setColumns(10); 

     textFieldNum2 = new JTextField(); 
     textFieldNum2.setBounds(194, 37, 106, 25); 
     frame.getContentPane().add(textFieldNum2); 
     textFieldNum2.setColumns(10); 

     JButton btnNewsButtonEnter = new JButton("Enter"); 
     btnNewsButtonEnter.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       final double num1; 
       final double num2; 
       try { 
        num1 = Double.parseDouble(textFieldNum1.getText()); 
        num2 =Double.parseDouble(textFieldNum2.getText()); 
        window.number1 = num1; 
        window.number2 = num2; 
        comboBoxNum.addActionListener(new ActionListener() { 

        if ((comboBoxNum.getSelectedItem()) == "Plus") { 
         totalres = window.calculateTotalPay()[0]; 
         itemselect ="Plus"; 
        } else if ((comboBoxNum.getSelectedItem()) == "Minus") { 
         totalres = window.calculateTotalPay()[1]; 
         itemselect ="Minus"; 
        } 
        if (itemselect == "Plus" || itemselect == "Minus"){ 
         textFieldTotRes.setText(Double.toString(totalres)); 
        } else { 
         JOptionPane.showMessageDialog(null, "Enter valid option"); 
        } 



       } catch (Exception ex) { 
        JOptionPane.showMessageDialog(null, "Enter valid number"); 
       } 
      } 
     }); 

     JButton btnNewButtonClear = new JButton("Clear"); 
     btnNewButtonClear.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       reset(); 
      } 

     }); 

     btnNewsButtonEnter.setBounds(20, 74, 84, 25); 
     frame.getContentPane().add(btnNewsButtonEnter); 
     btnNewButtonClear.setBounds(204, 74, 84, 25); 
     frame.getContentPane().add(btnNewButtonClear); 

     textFieldTotRes = new JTextField(); 
     textFieldTotRes.setBounds(198, 155, 200, 50); 
     frame.getContentPane().add(textFieldTotRes); 
     textFieldTotRes.setColumns(10); 

     lblNewTlabelTotNum = new JLabel("Result"); 
     lblNewTlabelTotNum.setFont(new Font("Garuda", Font.BOLD | Font.ITALIC, 14)); 
     lblNewTlabelTotNum.setBounds(29, 162, 106, 34); 
     frame.getContentPane().add(lblNewTlabelTotNum); 

     lblNewLabelNum1 = new JLabel("Enter 1st Number"); 
     lblNewLabelNum1.setBounds(10, 0, 106, 34); 
     frame.getContentPane().add(lblNewLabelNum1); 

     lblNewLabelNum2 = new JLabel("Enter 2nd Number"); 
     lblNewLabelNum2.setBounds(194, 0, 97, 40); 
     frame.getContentPane().add(lblNewLabelNum2); 

     comboBoxNum = new JComboBox<Object>(); 
     comboBoxNum.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       System.out.println(comboBoxNum.getSelectedItem()); 
      } 
     }); 
     comboBoxNum.setModel(new DefaultComboBoxModel<Object>(new String[] {"--------------", "Plus", "Minus"})); 
     comboBoxNum.setBounds(331, 37, 58, 40); 
     frame.getContentPane().add(comboBoxNum); 



    } 

    public void reset() { 
     textFieldNum1.setText(""); 
     textFieldNum2.setText(""); 
     textFieldTotRes.setText(""); 

    } 
} 
+0

「*未能實現*」沒有足夠的信息量。你有什麼錯誤嗎?輸出和/或行爲是否與預期不同?任何其他細節? –

回答

3
  • 你從來沒有實例化的計算類。
  • 您將您的TestCalculate實例命名爲「窗口」,但隨後在計算(例如,window.number1)中引用不在TestCalculate中的成員變量。
  • 您正在爲btnNewsButtonEnter ActionListener的actionPerformed方法內部添加一個ActionListener到comboBoxNum。將嵌套的ActionListener移出該方法。
+0

感謝您的回覆,並解決了我的問題。 :) – Paul85