gpt4 book ai didi

java - GetChangeGui 手册 JFrame

转载 作者:行者123 更新时间:2023-12-01 09:58:56 28 4
gpt4 key购买 nike

我正在尝试为我的 CS 1 类(class)编写代码。

代码的要点是编写一个代码,您可以在其中从一百美元的钞票中进行任何金额的找零,我需要归还适当的钞票和硬币。

我必须手动编写 JFrame

如果有人能告诉我计算中哪里出错了,那将会很有帮助。

/**
*
* @author esamayoa
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class GetChange extends JFrame {
//Declare variables

JButton bCompute, bReset;
JTextField tAmount, tQuarters, tDimes, tNickels, tPennies, tTwenty, tTen, tFive, tOne;
JLabel lAmount, lQuarters, lDimes, lNickels, lPennies, lTwenty, lTen, lFive, lOne;
double amount, diff, totalPaid, quarter, dime, nickel, penny, twenty, ten, five, one;


public GetChange (){
//Set the attributes of the Jframe
setTitle("Eric");
setLocation(500,10);
setSize(450,1000);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);

//Create your Gui components

lAmount = new JLabel("Amount");
lQuarters = new JLabel("Quarters:");
lDimes = new JLabel("Dimes:");
lNickels = new JLabel("Nickels:");
lPennies = new JLabel("Pennies:");
lTwenty = new JLabel("Twenties");
lTen = new JLabel("Tens");
lFive = new JLabel("Fives");
lOne = new JLabel("Ones");

tAmount = new JTextField();
tQuarters = new JTextField();
tDimes = new JTextField();
tNickels = new JTextField();
tPennies = new JTextField();
tTwenty = new JTextField();
tTen = new JTextField();
tFive = new JTextField();
tOne = new JTextField();
bReset = new JButton("Reset");
bCompute = new JButton("Compute");

//Add you Gui components to the Jframe
setLayout(new GridLayout(10,2));

add(lAmount);
add(tAmount);
add(lQuarters);
add(tQuarters);
add(lDimes);
add(tDimes);
add(lNickels);
add(tNickels);
add(lPennies);
add(tPennies);
add(lTwenty);
add(tTwenty);
add(lTen);
add(tTen);
add(lFive);
add(tFive);
add(lOne);
add(tOne);
add(bCompute);
add(bReset);

//Updates frame
this.validate();

//Add Action Listeners to buttons
bReset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {

tAmount.setText("");
tQuarters.setText("");
tDimes.setText("");
tNickels.setText("");
tPennies.setText("");
tTwenty.setText("");
tTen.setText("");
tFive.setText("");
tOne.setText("");
tAmount.setText("");
}
});

//Create computation for compute button
bCompute.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
totalPaid = 100;

amount = Double.parseDouble(tAmount.getText());

diff = totalPaid-amount;

twenty = diff/20;
diff = diff%20;
tTwenty.setText(""+ twenty);

ten = diff/10;
diff = diff%10;
tTen.setText(""+ ten);

five = diff/5;
diff = diff%5;
tFive.setText(""+ five);

one = diff/1;
diff = diff%1;
tOne.setText(""+ one);

quarter = diff/.25;
diff = diff%.25;
tQuarters.setText(""+ quarter);

dime = diff/.1;
diff = diff%.1;
tDimes.setText(""+ dime);

nickel = diff/.05;
diff = diff%.05;
tNickels.setText(""+ nickel);

penny = diff/.01;
diff = diff%.01;
tPennies.setText(""+ penny);



}
});

}
//Main Method
public static void main(String[] args) {
// TODO code application logic here
GetChange myApp = new GetChange();
}

}

最佳答案

要解决第一个问题(框架未显示),请在添加所有组件后插入以下内容,

this.validate(); // updates frame

要添加重置按钮的实现,只需使用 .setText("");

 bReset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
tAmount.setText("");
tQuarters.setText("");
tDimes.setText("");
tNickels.setText("");
tPennies.setText("");
tTwenty.setText("");
tTen.setText("");
tFive.setText("");
tOne.setText("");
tResult.setText("");
}
});

向计算按钮添加实现...

         bCompute.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {

double Amount = Double.parseDouble(tAmount.getText());
double q = (double) Integer.parseInt(tQuarters.getText()) *.25;
double d = (double) Integer.parseInt(tDimes.getText()) *.10;
double n = (double) Integer.parseInt(tNickels.getText()) *.05;
double p = (double) Integer.parseInt(tPennies.getText()) *.01;
double T = (double) Integer.parseInt(tTwenty.getText()) *20;
double Ten = (double) Integer.parseInt(tTen.getText()) *10;
double Five = (double) Integer.parseInt(tFive.getText()) *5;
double one = (double) Integer.parseInt(tOne.getText()) *1;


double TotalPaid = q+d+n+p+T+Ten+Five+1;
double diff = TotalPaid-Amount;

//Heres an example to create how many twenties you need
int totalTwenties = (int)diff /20;
diff = diff%20;
tTwenty.setText("" + totalTwenties);


}
});

基本上,我得到了支付金额和成本金额之间的差异。

A) 然后,通过/20 找到 20 可以容纳差异的次数。接下来,我取模或求出与二十之差的余数。

B) 要查找您需要找零的其他硬币/纸币的数量,请重复 A 中描述的过程。

关于java - GetChangeGui 手册 JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36959720/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com