我正在创建一个带有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 :(得分:0)
我假设你想要netpay,因为这似乎是代码和计算按钮中最符合逻辑的。如果是这样,您只需要从NetPay类调用该方法,传递参数并将返回值放入GUI中的正确位置。因此,假设工资是每小时工资,并且你将薪水和技巧提高到双打,那么代码就像是:
NetPay netPayRate= new NetPay();
double netpay= netPayRate(salary, tips);
然后将netpay cast作为字符串放入gui上的正确对象。假设薪水是hallyrate的变量,我会考虑重新命名,因为薪水往往表示年度金额。同样,提示会建议价值而不是价格,因此值得重命名。
答案 1 :(得分: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);
}
});