用java.swing,使得按下按钮可以使前两个文本框中的数字相加显示在第三个文本框总
2个回答
展开全部
package test;
import java.awt.Color;
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.JPanel;
import javax.swing.JTextField;
public class Sum extends JFrame implements ActionListener{
JPanel panel;
JLabel oneLabel;
JLabel twoLabel;
JButton sumButton;
JTextField oneTextField;
JTextField twoTextField;
JTextField threeTextField;
public Sum() {
super("求和");
panel = new JPanel();
panel.setBackground(new Color(192, 192, 192));
panel.setLayout(new FlowLayout());
this.add(panel);
oneLabel = new JLabel("输入第一个数");
twoLabel = new JLabel("输入第二个数");
oneTextField = new JTextField(15);
twoTextField = new JTextField(15);
threeTextField = new JTextField(15);
sumButton = new JButton("求和");
oneTextField.addActionListener(this);
twoTextField.addActionListener(this);
sumButton.addActionListener(this);
panel.add(oneLabel);
panel.add(oneTextField);
panel.add(twoLabel);
panel.add(twoTextField);
panel.add(sumButton);
panel.add(threeTextField);
this.setBounds(200, 100, 200, 240);
this.setResizable(true);
this.setVisible(true);
}
public static void main(String[] args) {
new Sum();
}
@Override
public void actionPerformed(ActionEvent e) {
String one = oneTextField.getText().trim();
String two = twoTextField.getText().trim();
int sum = Integer.parseInt(one)+Integer.parseInt(two);
threeTextField.setText(String.valueOf(sum));
}
}
还需要 自己 添加一些 判断 验证 数据的 合法性等
import java.awt.Color;
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.JPanel;
import javax.swing.JTextField;
public class Sum extends JFrame implements ActionListener{
JPanel panel;
JLabel oneLabel;
JLabel twoLabel;
JButton sumButton;
JTextField oneTextField;
JTextField twoTextField;
JTextField threeTextField;
public Sum() {
super("求和");
panel = new JPanel();
panel.setBackground(new Color(192, 192, 192));
panel.setLayout(new FlowLayout());
this.add(panel);
oneLabel = new JLabel("输入第一个数");
twoLabel = new JLabel("输入第二个数");
oneTextField = new JTextField(15);
twoTextField = new JTextField(15);
threeTextField = new JTextField(15);
sumButton = new JButton("求和");
oneTextField.addActionListener(this);
twoTextField.addActionListener(this);
sumButton.addActionListener(this);
panel.add(oneLabel);
panel.add(oneTextField);
panel.add(twoLabel);
panel.add(twoTextField);
panel.add(sumButton);
panel.add(threeTextField);
this.setBounds(200, 100, 200, 240);
this.setResizable(true);
this.setVisible(true);
}
public static void main(String[] args) {
new Sum();
}
@Override
public void actionPerformed(ActionEvent e) {
String one = oneTextField.getText().trim();
String two = twoTextField.getText().trim();
int sum = Integer.parseInt(one)+Integer.parseInt(two);
threeTextField.setText(String.valueOf(sum));
}
}
还需要 自己 添加一些 判断 验证 数据的 合法性等
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
按键.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
String s = Double.parseDouble(文本框1.getText()) + Double.parseDouble(文本框2.getText()) + "";
文本框3.setText(s);
}
});
@Override
public void actionPerformed(ActionEvent event) {
String s = Double.parseDouble(文本框1.getText()) + Double.parseDouble(文本框2.getText()) + "";
文本框3.setText(s);
}
});
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询