我正在尝试创建这样的计算,在TextArea中输出到我的主框架,然后将其传递到不同的类/框架,只需单击按钮即可执行下一次计算。
所以,如果我点击' m10py'在支付类中,我在TextArea中的任何内容都将减少0.10。我尝试过所有的东西,但它似乎并没有起作用。它不会抛出任何错误,但TextArea中的总数仍然保持不变。
我执行基本计算的主要课程:
package coursework_resit;
public class Main extends JFrame implements ActionListener {
JLabel head = new JLabel("Cinema Ticket");
JLabel front = new JLabel("Front Stalls (£9.50)");
JLabel middle = new JLabel("Middle Stalls (£7.30)");
JLabel back = new JLabel("Back Stalls (£6.70)");
JLabel vip = new JLabel("VIP (£12.90)");
JLabel outputL = new JLabel("Total Amount :");
JButton cancel = new JButton("Cancel");
JButton payment = new JButton("Pay");
JButton calculate = new JButton("Calculate");
JTextField frontT = new JTextField(6);
JTextField middleT = new JTextField(6);
JTextField backT = new JTextField(6);
JTextField vipT = new JTextField(6);
JTextArea output = new JTextArea();
ButtonGroup buttonGroup = new ButtonGroup();
JRadioButton frontR = new JRadioButton("", false);
JRadioButton middleR = new JRadioButton("", false);
JRadioButton backR = new JRadioButton("", false);
JRadioButton vipR = new JRadioButton("", false);
DecimalFormat pounds = new DecimalFormat("£#,##0.00");
double total2;
public Main() {
super("Cinema Ticket");
setBounds(100, 100, 800, 350);
setLocationRelativeTo(null);
getContentPane().setLayout(null);
buttonGroup.add(frontR);
buttonGroup.add(middleR);
buttonGroup.add(backR);
buttonGroup.add(vipR);
add(frontR);
frontR.setBounds(60, 110, 22, 13);
frontR.setBackground(Color.GRAY);
add(middleR);
middleR.setBounds(270, 110, 22, 13);
middleR.setBackground(Color.GRAY);
add(backR);
backR.setBounds(270, 200, 22, 13);
backR.setBackground(Color.GRAY);
add(vipR);
vipR.setBounds(60, 200, 22, 13);
vipR.setBackground(Color.GRAY);
frontR.addActionListener(this);
middleR.addActionListener(this);
backR.addActionListener(this);
vipR.addActionListener(this);
head.setBounds(280, 10, 300, 40);
getContentPane().add(head);
head.setFont(new Font("Arial Rounded MT Bold", Font.BOLD, 30));
head.setForeground(Color.white);
front.setBounds(90, 70, 200, 50);
getContentPane().add(front);
front.setFont(new Font("Cambria", Font.BOLD, 20));
front.setForeground(Color.white);
frontT.setBounds(110, 110, 50, 25);
getContentPane().add(frontT);
frontT.setFont(new Font("Cambria", Font.BOLD, 20));
frontT.setText("1");
middle.setBounds(300, 70, 200, 50);
getContentPane().add(middle);
middle.setFont(new Font("Cambria", Font.BOLD, 20));
middle.setForeground(Color.WHITE);
middleT.setBounds(320, 110, 50, 25);
getContentPane().add(middleT);
middleT.setFont(new Font("Cambria", Font.BOLD, 20));
middleT.setText("1");
getContentPane().add(back);
back.setBounds(300, 150, 200, 80);
back.setFont(new Font("Cambria", Font.BOLD, 20));
back.setForeground(Color.WHITE);
backT.setBounds(320, 205, 50, 25);
getContentPane().add(backT);
backT.setFont(new Font("Cambria", Font.BOLD, 20));
backT.setText("1");
vip.setBounds(90, 150, 200, 80);
getContentPane().add(vip);
vip.setFont(new Font("Cambria", Font.BOLD, 20));
vip.setForeground(Color.WHITE);
vipT.setBounds(110, 205, 50, 25);
getContentPane().add(vipT);
vipT.setFont(new Font("Cambria", Font.BOLD, 20));
vipT.setText("1");
outputL.setBounds(570, 55, 200, 80);
getContentPane().add(outputL);
outputL.setFont(new Font("Cambria", Font.BOLD, 20));
outputL.setForeground(Color.WHITE);
output.setBounds(590, 110, 100, 25);
getContentPane().add(output);
output.setEditable(false);
output.setFont(new Font("Cambria", Font.BOLD, 20));
payment.setBounds(580, 210, 120, 40);
getContentPane().add(payment);
payment.setFont(new Font("Cambria", Font.BOLD, 16));
payment.setBackground(Color.darkGray);
payment.setForeground(Color.WHITE);
payment.addActionListener(this);
calculate.setBounds(580, 160, 120, 40);
getContentPane().add(calculate);
calculate.setFont(new Font("Cambria", Font.BOLD, 16));
calculate.setBackground(Color.darkGray);
calculate.setForeground(Color.WHITE);
calculate.addActionListener(this);
cancel.setBounds(580, 260, 120, 40);
getContentPane().add(cancel);
cancel.setFont(new Font("Cambria", Font.BOLD, 16));
cancel.setBackground(Color.darkGray);
cancel.setForeground(Color.WHITE);
cancel.addActionListener(this);
getContentPane().setBackground(Color.gray);
setResizable(false);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
double frontM = 9.50;
double middleM = 7.30;
double backM = 6.70;
double vipM = 12.90;
//double total1 = Double.parseDouble(output.getText());
if (frontR.isSelected() && e.getSource() == calculate) {
double total = Integer.parseInt(frontT.getText()) * frontM;
output.setText(pounds.format(total));
} else if (middleR.isSelected() && e.getSource() == calculate) {
double total = Integer.parseInt(middleT.getText()) * middleM;
String total2 = String.valueOf(total);
output.setText(total2);
} else if (backR.isSelected() && e.getSource() == calculate) {
double total = Integer.parseInt(backT.getText()) * backM;
output.setText(pounds.format(total));
} else if (vipR.isSelected() && e.getSource() == calculate) {
double total = Integer.parseInt(vipT.getText()) * vipM;
output.setText(pounds.format(total));
} else if (e.getSource() == cancel) {
frontT.setText("1");
middleT.setText("1");
backT.setText("1");
vipT.setText("1");
output.setText("");
}
if (e.getSource() == payment) {
Payment paymentJ = new Payment();
paymentJ.output.setText(output.getText());
}
}
public static void main(String[] args) {
Main main = new Main();
}
}
我的付款'用户单击按钮的类以及textArea包含的内容,将减少按下的按钮数量。
package coursework_resit;
public class Payment extends JFrame implements ActionListener {
JButton cancel = new JButton("Cancel");
JButton change = new JButton("Collect change");
JButton dispense = new JButton("Dispense Ticket");
JButton m10p = new JButton("10p");
JButton m20p = new JButton("20p");
JButton m50p = new JButton("50p");
JButton m1 = new JButton("£1");
JButton m2 = new JButton("£2");
JButton m5 = new JButton("£5");
JButton m10 = new JButton("£10");
JButton m20 = new JButton("£20");
JTextArea output = new JTextArea();
JLabel totalA = new JLabel("Total amount needed :");
DecimalFormat pounds = new DecimalFormat("£#,##0.00");
public Payment() {
super("Payment");
setBounds(100, 100, 450, 600);
setLocationRelativeTo(null);
getContentPane().setBackground(Color.gray);
setResizable(false);
setVisible(true);
getContentPane().setLayout(null);
totalA.setBounds(50, 340, 250, 50);
getContentPane().add(totalA);
totalA.setFont(new Font("Cambria", Font.BOLD, 20));
totalA.setForeground(Color.white);
output.setBounds(50, 390, 280, 30);
getContentPane().add(output);
output.setEditable(false);
output.setFont(new Font("Cambria", Font.BOLD, 20));
dispense.setBounds(50, 450, 200, 50);
getContentPane().add(dispense);
dispense.setFont(new Font("Cambria", Font.BOLD, 20));
dispense.setForeground(Color.white);
dispense.setBackground(Color.DARK_GRAY);
cancel.setBounds(50, 510, 100, 50);
getContentPane().add(cancel);
cancel.setFont(new Font("Cambria", Font.BOLD, 20));
cancel.setForeground(Color.white);
cancel.setBackground(Color.DARK_GRAY);
m10p.setBounds(50, 70, 100, 50);
getContentPane().add(m10p);
m10p.setFont(new Font("Cambria", Font.BOLD, 20));
m10p.setForeground(Color.white);
m10p.setBackground(Color.DARK_GRAY);
m20p.setBounds(180, 70, 100, 50);
getContentPane().add(m20p);
m20p.setFont(new Font("Cambria", Font.BOLD, 20));
m20p.setForeground(Color.white);
m20p.setBackground(Color.DARK_GRAY);
m50p.setBounds(50, 140, 100, 50);
getContentPane().add(m50p);
m50p.setFont(new Font("Cambria", Font.BOLD, 20));
m50p.setForeground(Color.white);
m50p.setBackground(Color.DARK_GRAY);
m1.setBounds(180, 140, 100, 50);
getContentPane().add(m1);
m1.setFont(new Font("Cambria", Font.BOLD, 20));
m1.setForeground(Color.white);
m1.setBackground(Color.DARK_GRAY);
m2.setBounds(50, 210, 100, 50);
getContentPane().add(m2);
m2.setFont(new Font("Cambria", Font.BOLD, 20));
m2.setForeground(Color.white);
m2.setBackground(Color.DARK_GRAY);
m5.setBounds(180, 210, 100, 50);
getContentPane().add(m5);
m5.setFont(new Font("Cambria", Font.BOLD, 20));
m5.setForeground(Color.white);
m5.setBackground(Color.DARK_GRAY);
m10.setBounds(50, 280, 100, 50);
getContentPane().add(m10);
m10.setFont(new Font("Cambria", Font.BOLD, 20));
m10.setForeground(Color.white);
m10.setBackground(Color.DARK_GRAY);
m20.setBounds(180, 280, 100, 50);
getContentPane().add(m20);
m20.setFont(new Font("Cambria", Font.BOLD, 20));
m20.setForeground(Color.white);
m20.setBackground(Color.DARK_GRAY);
}
@Override
public void actionPerformed(ActionEvent e) {
Main main = new Main();
// double totalR = Double.parseDouble(output.getText());
String cost = main.output.getText();
double cost2 = Double.parseDouble(cost);
double total;
total = cost2;
double m10py = 0.10;
double m20py = 0.20;
double m50py = 0.50;
double m1p = 1.00;
double m2p = 2.00;
double m5p = 5.00;
double m10po = 10.00;
double m20po = 20.00;
if (e.getSource() == m10p) {
total = total - m10py;
String total2 = String.valueOf(total);
output.setText(total2);
}
}
public static void main(String[] args) {
new Payment();
}
}
令我头疼的唯一部分是在Payment类的actionPerformed方法中,通过单击按钮可以减少textArea中的数量。
Main main = new Main();
// double totalR = Double.parseDouble(output.getText());
String cost = main.output.getText();
double cost2 = Double.parseDouble(cost);
double total;
total = cost2;
double m10py = 0.10;
double m20py = 0.20;
double m50py = 0.50;
double m1p = 1.00;
double m2p = 2.00;
double m5p = 5.00;
double m10po = 10.00;
double m20po = 20.00;
if (e.getSource() == m10p) {
total = total - m10py;
String total2 = String.valueOf(total);
output.setText(total2);
}
}