BMI的计算器,需要帮助修复清除按钮

时间:2015-12-06 02:30:12

标签: java

我发布了一个关于如何实现actionlisteners的上一个问题。 香港专业教育学院完成了这个课程,我认为可能在阵列方面做得更好。 但这种方式对我有用。 问题是。当我点击清除按钮时它不会清除它,因为我的数字按钮上有更新变量,当按下它时存储数字。如果这是有道理的。

有没有办法可以解决它?

        <!DOCTYPE html>
    <html>
    <head>
    <title>Digital Goods</title>
    </head>
    <body>

    <form id="myContainer" method="post" action="paypal/checkout.php">
        <img src="https://www.vanishingincmagic.co.uk/gallery/photos/the-modern-con-man.jpg">
        <p>$4.99</p>
        <p>Buy my totally not fake book!</p>
    </form>

    <script>window.paypalCheckoutReady=function(){paypal.checkout.setup('EMAIL-HERE',{environment:'sandbox',container:'myContainer'})};</script>
    <script src="//www.paypalobjects.com/api/checkout.js" async></script>
    </body>
    </html>

// import packages
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


// class
public class Lab31Panel extends JPanel {

	// data declarations

	private JRadioButton k2pButton;
	private JRadioButton p2kButton;
	private ButtonGroup weight;
	private JPanel selectConversion;
	private JButton jb0, jb1, jb2, jb3, jb4, jb5, jb6, jb7, jb8, jb9, jbminus, jbclear, jbconvert;
	private JTextArea display;
	private String update = "";


	//constructor to initiate data and set up GUI
	public Lab31Panel() {
		setLayout(new BorderLayout());

		// organizing radio buttons and their behaviours
		k2pButton = new JRadioButton("Kilograms to Pounds");
		p2kButton = new JRadioButton("Pounds to Kilograms");
		weight = new ButtonGroup();
		weight.add(k2pButton);
		weight.add(p2kButton);


		// adding components to panel to be south of the GUI
		selectConversion = new JPanel();
		selectConversion.add(k2pButton);
		selectConversion.add(p2kButton);


		add(selectConversion, BorderLayout.SOUTH);

		//setting up west area of GUI
		JPanel westPanel = new JPanel();
		JPanel convert = new JPanel();

		// setting up components for the  west of the GUI
		westPanel.setLayout(new GridLayout(5, 3));
		westPanel.add(jb0 = new JButton("0"));
		westPanel.add(jb1 = new JButton("1"));
		westPanel.add(jb2 = new JButton("2"));
		westPanel.add(jb3 = new JButton("3"));
		westPanel.add(jb4 = new JButton("4"));
		westPanel.add(jb5 = new JButton("5"));
		westPanel.add(jb6 = new JButton("6"));
		westPanel.add(jb7 = new JButton("7"));
		westPanel.add(jb8 = new JButton("8"));
		westPanel.add(jb9 = new JButton("9"));
		westPanel.add(jbminus = new JButton("-"));
		westPanel.add(jbclear = new JButton("clear"));
		westPanel.add(jbconvert = new JButton("convert"));



		add(westPanel, BorderLayout.WEST);

		//setting up east components
		JPanel eastPanel = new JPanel();
		eastPanel.setLayout(new BorderLayout());
		display = new JTextArea();
		display.setEditable(false);

		eastPanel.add(display, BorderLayout.CENTER);
		add(eastPanel);


		jb0.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {


				Object source = e.getSource();
				if (source == jb0) update += 0;
				display.setText(update);

			}
		});

		jb1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				Object source = e.getSource();
				if (source == jb1) update += 1;
				display.setText(update);
			}
		});



		jb2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				Object source = e.getSource();
				if (source == jb2) update += 2;
				display.setText(update);


			}
		});

		jb3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				Object source = e.getSource();
				if (source == jb3) update += 3;
				display.setText(update);

			}
		});

		jb4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				Object source = e.getSource();
				if (source == jb4) update += 4;
				display.setText(update);

			}
		});

		jb5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				Object source = e.getSource();
				if (source == jb5) update += 5;
				display.setText(update);

			}
		});

		jb6.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				Object source = e.getSource();
				if (source == jb6) update += 6;
				display.setText(update);

			}
		});

		jb7.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				Object source = e.getSource();
				if (source == jb7) update += 7;
				display.setText(update);

			}
		});

		jb8.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				Object source = e.getSource();
				if (source == jb8) update += 8;
				display.setText(update);

			}
		});

		jb9.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				Object source = e.getSource();
				if (source == jb9) update += 9;
				display.setText(update);

			}
		});



		jbclear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				Object source = e.getSource();
				if (source == jbclear) display.setText(null);

			}
		});

		jbconvert.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Object source = e.getSource();

				double convert = Double.parseDouble(update);
				double totalp2k = convert / 2.24;
				double totalk2p = convert * 2.24;


				if (p2kButton.isSelected() == true) {
					if (source == jbconvert) {

						display.setText(update + " : " + totalp2k);
					}

				} else if (k2pButton.isSelected() == true) {

					display.setText(update + " : " + totalk2p);
				}

			}
		});




	} //end constructor


} // end class

1 个答案:

答案 0 :(得分:1)

在jbclear的ActionListener中,您还需要清除update:

jbclear.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        if (source == jbclear) {
            display.setText(null);
            update = "";
        }
    }
});