2017-08-04 97 views
-2

我正在用gui創建一個計算器,該計算器返回包含在練習中的小費費率的週薪,並遇到問題。如何從另一個班級調用雙人班?

 public class NetPay { 

     public static double netPayRate(double hourlyPayRate, double tipRate){ 
     double netPayRate=(hourlyPayRate*tipRate)+hourlyPayRate; 

     return netPayRate*40; 
    } 
    public static void main(String[] args) { 
     // TODO Auto-generated method stub 

    } 

} 

我想知道如何在我所做的gui類中調用hourlyPayRate和tipRate?

謝謝!

GUI:

public class DiffGui { 

NetPay netPayRate= new NetPay(); 

public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    new DiffGui(); 
} 


public DiffGui(){ 
EventQueue.invokeLater(new Runnable(){ 

    @Override 
    public void run() { 
     // TODO Auto-generated method stub 
     try{ 
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 

     }catch(ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex){ 
      ex.printStackTrace(); 

     } 
     //the frame everything is built on 
     JFrame mainFrame= new JFrame("Testing"); 
      mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      mainFrame.add(new Test()); 
      mainFrame.pack(); 
      mainFrame.setLocationRelativeTo(null); 
      mainFrame.setVisible(true); 

     //code for the textboxes+button & panel below 
      JPanel southPanel= new JPanel(); 
      JTextField salary= new JTextField(); 
      JTextField tips= new JTextField(); 
        southPanel.add(salary); 
        southPanel.add(tips); 
      JButton calculateButton= new JButton("Calculate!"); 
        southPanel.add(calculateButton); 
        mainFrame.getContentPane().add(southPanel , BorderLayout.SOUTH); 
        calculateButton.addActionListener(new ActionListener(){ 
// calculate button action listener 
         @Override 
         public void actionPerformed(ActionEvent e) { 




          // TODO Auto-generated method stub 

         } 

        }); 

     } 


    }); 
} 
public class Test extends JPanel{ 
public Test(){ 
    setLayout(new BorderLayout()); 
    BackgroundPane backPane= new BackgroundPane(); 
    backPane.setLayout(new GridBagLayout()); 
    add(backPane); 


    try { 
     BufferedImage tryCatch = ImageIO.read(new File("pictures/background.gif")); 
     backPane.setbackgroundImage(tryCatch); 
    } catch (IOException ex) { 
     ex.printStackTrace(); 


    } 
    JLabel viewing= new JLabel("Pay Calculator"); 
    viewing.setOpaque(true); 
    viewing.setForeground(Color.BLACK); 
    viewing.setBackground(Color.YELLOW); 
    viewing.setBorder(new EmptyBorder(25,25,25,25)); 
    backPane.add(viewing); 
} 
public class BackgroundPane extends JPanel{ 
    private BufferedImage image; 
     @Override 
      public Dimension getPreferredSize(){ 
       BufferedImage image = getBackgroundImage(); 
       Dimension size= super.getPreferredSize(); 
        if(image != null){ 
         size.width = Math.max(size.width, image.getWidth()); 
         size.height = Math.max(size.height, image.getHeight()); 
        } 
        return size; 

    } 
     public BufferedImage getBackgroundImage(){ 
      return image; 
     } 
     public void setbackgroundImage(BufferedImage x){ 
      if(image!=x){ 
       BufferedImage prevous= image; 
       image=x; 
       firePropertyChange("background" , prevous , image); 
       revalidate(); 
       repaint(); 

      } 
     } 
     @Override 
     protected void paintComponent(Graphics graphs){ 
      super.paintComponent(graphs); 
      BufferedImage backpane= getBackgroundImage(); 
      if(backpane != null){ 
       int x = (getWidth()-backpane.getWidth())/2; 
       int y = (getHeight()-backpane.getHeight())/2; 
       graphs.drawImage(backpane,x,y,this); 

      } 


     } 
} 

} 
} 

計算按鈕的動作偵聽器,特別是在變量工資和提示,我想在我的GUI插入值,然後有薪酬計算器使用這些值來找到週刊支付

+0

在main方法中聲明這兩個值並傳遞給'netPayRate'。 –

+2

我無法正確理解問題?你想調用的函數是你想在另一個類中使用的公共方法嗎? –

+0

不要直接調用gui類的任何方法,而是將這些值存儲在模型類中... – deHaar

回答

0

如果我正確理解您的問題,您想將DiffGUI的值傳遞給PayNet。你可以在你的按鈕中點擊這個按鈕點擊

calculateButton.addActionListener(new ActionListener() { 
    // calculate button action listener 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      // TODO Auto-generated method stub 
       double salaryValue = Double.valueOf(salary.getText()); // get value from JTextField 
       double tipsValue = Double.valueOf(tips.getText()); 
       NetPay net = new NetPay(); 
       double netpayValue = net.netPayRate(salaryValue, tipsValue); 

      } 
     }); 
+0

是的,謝謝!一旦我通過它,但我怎麼會在gui中從NetPay類中顯示netPayrate * 40? –

+0

'SomeJTextField.setText(Double.toString(netpayValue));' – BusinessPlanQuickBuilder

0

我假設你想netpay,因爲這似乎從代碼和caculate按鈕最合理。如果是這樣,你只需要從NetPay類中調用方法,傳遞參數並將返回值放入GUI中的正確位置。因此,假設工資hourlyrate和你投的工資和提示,以雙打那麼代碼將不會停,如:

NetPay netPayRate= new NetPay(); 

double netpay= netPayRate(salary, tips); 

然後把netpay塑像作爲字符串轉換成正確的對象上的GUI。假設薪水是侯利的變量,我會考慮重新命名它,因爲薪水往往表示年度金額。同樣,提示會建議價值而不是比率,所以值得重新命名。

相關問題