gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 10:47:05 28 4
gpt4 key购买 nike

我之前发布了一个关于如何实现 Action 监听器的问题。我完成了这门课,我认为使用数组可能可以做得更好。但这种方式对我有用。问题是。当我单击清除按钮时,它不会清除它,因为我的数字按钮上有更新变量,按下时会将数字存储在其中。如果这是有道理的。

有什么办法可以解决这个问题吗?

// 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

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

public class Lab31Frame
{

public static void main( String[] args )
{
JFrame myFrame = new JFrame( "Lab 3: Question 2" );

// create an instance of Panel and add to frame
Lab31Panel myPanel = new Lab31Panel();
myFrame.add( myPanel );

// set up functionality of frame
myFrame.setSize( 500, 310 );
myFrame.setVisible( true );
myFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

}//end main
} // end class

最佳答案

在jbclear的ActionListener中,还需要清除更新:

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

关于java - BMI 计算器,需要帮助修复清除按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34113310/

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