如何利用Java实现在两个文本框中输入数据,计算两数和在第三个文本框中?
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Main {
private static JTextField a = new JTextField(4);
private static JTextField b = new JTextField(4);
private static JTextField c = new JTextField(4);
public static void main(String[] args) {
JFrame frame = new JFrame("加法计算");
frame.setLayout(new FlowLayout(FlowLayout.LEFT));
frame.add(a);
frame.add(new JLabel("+"));
frame.add(b);
JButton button = new JButton("=");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
c.setText(String.valueOf(Double.parseDouble(a.getText()) + Double.parseDouble(b.getText())));
}
});
frame.add(button);
frame.add(c);
frame.pack();
frame.setVisible(true);
}
}