急,java题目(swing界面),求和,在第一个文本框中输入第一个加数,第二个文本框中输入第二个加数
当单击“=”按钮时,在第三个文本框中显示前两个加数的和(用getText()和setText())...
当单击“=”按钮时,在第三个文本框中显示前两个加数的和(用getText()和setText())
展开
2个回答
展开全部
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class JavaCalum extends JFrame implements ActionListener{
private JButton jb1 = new JButton("=");
private JTextField jt1 = new JTextField(10);
private JTextField jt2 = new JTextField(10);
private JTextField jt3 = new JTextField(10);
private JLabel jl = new JLabel("+");
public JavaCalum() {
super.setTitle("简易计算器");
super.setBounds(200,200,500,500);
super.getContentPane().setLayout(new FlowLayout());
super.getContentPane().add(jt1);
super.getContentPane().add(jl);
super.getContentPane().add(jt2);
super.getContentPane().add(jb1);
super.getContentPane().add(jt3);
jb1.addActionListener(this);
super.setVisible(true);
}
public static void main(String args[]){
new JavaCalum();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==jb1){
double a = Double.parseDouble(jt1.getText());
double b = Double.parseDouble(jt2.getText());
double c = a+b;
String string= Double.toString(c);
jt3.setText(string);
}
}
}
//调试过了可以用,可以计算带小数点的加法。
import java.awt.event.*;
import javax.swing.*;
class JavaCalum extends JFrame implements ActionListener{
private JButton jb1 = new JButton("=");
private JTextField jt1 = new JTextField(10);
private JTextField jt2 = new JTextField(10);
private JTextField jt3 = new JTextField(10);
private JLabel jl = new JLabel("+");
public JavaCalum() {
super.setTitle("简易计算器");
super.setBounds(200,200,500,500);
super.getContentPane().setLayout(new FlowLayout());
super.getContentPane().add(jt1);
super.getContentPane().add(jl);
super.getContentPane().add(jt2);
super.getContentPane().add(jb1);
super.getContentPane().add(jt3);
jb1.addActionListener(this);
super.setVisible(true);
}
public static void main(String args[]){
new JavaCalum();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==jb1){
double a = Double.parseDouble(jt1.getText());
double b = Double.parseDouble(jt2.getText());
double c = a+b;
String string= Double.toString(c);
jt3.setText(string);
}
}
}
//调试过了可以用,可以计算带小数点的加法。
展开全部
//下面的代码已经通过调试 可以用
import java.awt.BorderLayout;
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 CountAB extends JFrame{
private JTextField a = new JTextField(7);
private JTextField b = new JTextField(7);
private JLabel c = new JLabel();
private JButton button = new JButton("=");
public CountAB(){
init();
}
public void showView(){
setVisible(true);
}
public void init(){
a.setSize(70,50);
setSize(400,100);
JPanel pane = new JPanel(new FlowLayout());
pane.add(a);
pane.add(new JLabel("+"));
pane.add(b);
pane.add(button);
pane.add(c);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
c.setText(Integer.parseInt(a.getText()) + Integer.parseInt(b.getText())+"");
}
});
setContentPane(pane);
}
public static void main(String[] args) {
new CountAB().showView();
}
}
import java.awt.BorderLayout;
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 CountAB extends JFrame{
private JTextField a = new JTextField(7);
private JTextField b = new JTextField(7);
private JLabel c = new JLabel();
private JButton button = new JButton("=");
public CountAB(){
init();
}
public void showView(){
setVisible(true);
}
public void init(){
a.setSize(70,50);
setSize(400,100);
JPanel pane = new JPanel(new FlowLayout());
pane.add(a);
pane.add(new JLabel("+"));
pane.add(b);
pane.add(button);
pane.add(c);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
c.setText(Integer.parseInt(a.getText()) + Integer.parseInt(b.getText())+"");
}
});
setContentPane(pane);
}
public static void main(String[] args) {
new CountAB().showView();
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询