我正在用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