编写一个Java程序
编写一个Java程序,在程序中建立一个窗口,有四个文本框、两个按钮,单击“求和”按钮,能把第一个和第二个文本框的整数相加后结果显示在第三个文本框中,点击“复制”按钮,能将...
编写一个Java程序,在程序中建立一个窗口,有四个文本框、两个按钮,单击“求和”按钮,能把第一个和第二个文本框的整数相加后结果显示在第三个文本框中,点击“复制”按钮,能将第三个文本框的内容复制到第四个文本框中,最后还可以正常关闭窗口。运行效果如下图所示:
展开
2个回答
展开全部
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.JTextField;
public class Test {
public static void main(String[] args) {
JFrame f = new JFrame("Calculator");
final JTextField num1 = new JTextField(15);
final JTextField num2 = new JTextField(15);
final JTextField sum = new JTextField(15);
final JTextField copy = new JTextField(15);
JButton calc = new JButton("Sum");
JButton copyBtn = new JButton("Copy");
f.add(num1);
f.add(num2);
f.add(sum);
f.add(copy);
f.add(calc);
f.add(copyBtn);
calc.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
sum.setText(String.valueOf(Integer.parseInt(num1.getText()) + Integer.parseInt(num2.getText())));
}
});
copyBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
copy.setText(sum.getText());
}
});
f.setSize(300, 200);
f.setLayout(new FlowLayout());
f.setVisible(true);
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class Test {
public static void main(String[] args) {
JFrame f = new JFrame("Calculator");
final JTextField num1 = new JTextField(15);
final JTextField num2 = new JTextField(15);
final JTextField sum = new JTextField(15);
final JTextField copy = new JTextField(15);
JButton calc = new JButton("Sum");
JButton copyBtn = new JButton("Copy");
f.add(num1);
f.add(num2);
f.add(sum);
f.add(copy);
f.add(calc);
f.add(copyBtn);
calc.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
sum.setText(String.valueOf(Integer.parseInt(num1.getText()) + Integer.parseInt(num2.getText())));
}
});
copyBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
copy.setText(sum.getText());
}
});
f.setSize(300, 200);
f.setLayout(new FlowLayout());
f.setVisible(true);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
package test;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class Test {
public static void main(String[] args) {
new Test();
}
private Test() {
final JFrame jf = new JFrame("求和复制");
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(400, 150);
jf.setLayout(new FlowLayout());
final JTextField jtf1 = new JTextField(8);
jf.add(jtf1);
final JTextField jtf2 = new JTextField(8);
jf.add(jtf2);
final JTextField jtf3 = new JTextField(8);
jf.add(jtf3);
final JTextField jtf4 = new JTextField(8);
jf.add(jtf4);
JButton btn1 = new JButton("求和");
jf.add(btn1);
JButton btn2 = new JButton("复制");
jf.add(btn2);
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if ("".equals(jtf1.getText().trim()) || "".equals(jtf1.getText().trim())) {
JOptionPane.showMessageDialog(jf, "请在输入框中输入数字");
} else {
try {
jtf3.setText("" + (new BigDecimal(jtf1.getText().trim())).add(new BigDecimal(jtf2.getText().trim())) );
} catch (Exception e1) {
JOptionPane.showMessageDialog(jf, "请在输入框中输入数字");
}
}
}
});
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jtf4.setText(jtf3.getText());
}
});
jf.setVisible(true);
}
}
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class Test {
public static void main(String[] args) {
new Test();
}
private Test() {
final JFrame jf = new JFrame("求和复制");
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(400, 150);
jf.setLayout(new FlowLayout());
final JTextField jtf1 = new JTextField(8);
jf.add(jtf1);
final JTextField jtf2 = new JTextField(8);
jf.add(jtf2);
final JTextField jtf3 = new JTextField(8);
jf.add(jtf3);
final JTextField jtf4 = new JTextField(8);
jf.add(jtf4);
JButton btn1 = new JButton("求和");
jf.add(btn1);
JButton btn2 = new JButton("复制");
jf.add(btn2);
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if ("".equals(jtf1.getText().trim()) || "".equals(jtf1.getText().trim())) {
JOptionPane.showMessageDialog(jf, "请在输入框中输入数字");
} else {
try {
jtf3.setText("" + (new BigDecimal(jtf1.getText().trim())).add(new BigDecimal(jtf2.getText().trim())) );
} catch (Exception e1) {
JOptionPane.showMessageDialog(jf, "请在输入框中输入数字");
}
}
}
});
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jtf4.setText(jtf3.getText());
}
});
jf.setVisible(true);
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |