我正在用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插入值,然后有薪酬计算器使用这些值来找到周刊支付
在main方法中声明这两个值并传递给'netPayRate'。 –
我无法正确理解问题?你想调用的函数是你想在另一个类中使用的公共方法吗? –
不要直接调用gui类的任何方法,而是将这些值存储在模型类中... – deHaar