2017-08-04 66 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。假设薪水是侯利的变量,我会考虑重新命名它,因为薪水往往表示年度金额。同样,提示会建议价值而不是比率,所以值得重新命名。

相关问题