Closed. This question needs debugging details。它当前不接受答案。
想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
4年前关闭。
我发布了有关如何实施动作侦听器的先前问题。
香港专业教育学院完成了这堂课,也许我可以用数组做得更好。
但是这种方式对我有用。
问题是。当我单击清除按钮时,它不会清除,因为我的数字按钮上有update变量,按此按钮会将数字存储在其中。如果有道理。
有什么办法可以解决吗?
想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
4年前关闭。
我发布了有关如何实施动作侦听器的先前问题。
香港专业教育学院完成了这堂课,也许我可以用数组做得更好。
但是这种方式对我有用。
问题是。当我单击清除按钮时,它不会清除,因为我的数字按钮上有update变量,按此按钮会将数字存储在其中。如果有道理。
有什么办法可以解决吗?
// 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/
10-16 01:41