用JAVA编写一个简单计算器????界面做出来了。
要求:1.点击下面的“=”按钮能进行简单的四则运算。2.如果除数为0弹出报错窗口。这个是界面:packagepackage1;importjava.awt.*;impor...
要求:1.点击下面的“=”按钮能进行简单的四则运算。2.如果除数为0弹出报错窗口。
这个是界面:
package package1;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JFrame;
import java.awt.FlowLayout;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.util.Scanner;
public class Test extends JFrame{
public Test(){
JPanel p1=new JPanel();
JPanel p2=new JPanel();
p1.setLayout(new FlowLayout(FlowLayout.LEFT,4,3));
p1.add(new JTextField("0",4));
p1.add(new JTextField(" + "));
p1.add(new JTextField("0",4));
p1.add(new JLabel("="));
p1.add(new JTextField(8));
p2.add(new JButton("="),BorderLayout.CENTER);
add(p1,BorderLayout.NORTH);
add(p2,BorderLayout.SOUTH);
}
public static void main(String[] args){
Test frame=new Test();
frame.setTitle("计算器");
frame.setSize(300,100);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
} 展开
这个是界面:
package package1;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JFrame;
import java.awt.FlowLayout;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.util.Scanner;
public class Test extends JFrame{
public Test(){
JPanel p1=new JPanel();
JPanel p2=new JPanel();
p1.setLayout(new FlowLayout(FlowLayout.LEFT,4,3));
p1.add(new JTextField("0",4));
p1.add(new JTextField(" + "));
p1.add(new JTextField("0",4));
p1.add(new JLabel("="));
p1.add(new JTextField(8));
p2.add(new JButton("="),BorderLayout.CENTER);
add(p1,BorderLayout.NORTH);
add(p2,BorderLayout.SOUTH);
}
public static void main(String[] args){
Test frame=new Test();
frame.setTitle("计算器");
frame.setSize(300,100);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
} 展开
2个回答
展开全部
import sun.plugin.security.Broken11ClassFixer;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JFrame;
import java.awt.FlowLayout;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
public class Test extends JFrame{
private JTextField t1;
private JTextField t2;
private JTextField t3;
private JButton b1;
public Test(){
JPanel p1=new JPanel();
JPanel p2=new JPanel();
p1.setLayout(new FlowLayout(FlowLayout.LEFT,4,3));
t1 = new JTextField("0",4);
t2 = new JTextField("0",4);
t3 = new JTextField(8);
b1 = new JButton("=");
p1.add(t1);
p1.add(new JTextField(" + "));
p1.add(t2);
p1.add(new JLabel("="));
p1.add(t3);
p2.add(b1,BorderLayout.CENTER);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
t3.setText(Integer.toString(Integer.parseInt(t1.getText())+Integer.parseInt(t2.getText())));
}
});
add(p1,BorderLayout.NORTH);
add(p2,BorderLayout.SOUTH);
}
public static void main(String[] args){
Test frame=new Test();
frame.setTitle("计算器");
frame.setSize(300,100);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
帮你完善了下,其他来不及写了都是一样的,稍微改一下就行
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JFrame;
import java.awt.FlowLayout;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
public class Test extends JFrame{
private JTextField t1;
private JTextField t2;
private JTextField t3;
private JButton b1;
public Test(){
JPanel p1=new JPanel();
JPanel p2=new JPanel();
p1.setLayout(new FlowLayout(FlowLayout.LEFT,4,3));
t1 = new JTextField("0",4);
t2 = new JTextField("0",4);
t3 = new JTextField(8);
b1 = new JButton("=");
p1.add(t1);
p1.add(new JTextField(" + "));
p1.add(t2);
p1.add(new JLabel("="));
p1.add(t3);
p2.add(b1,BorderLayout.CENTER);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
t3.setText(Integer.toString(Integer.parseInt(t1.getText())+Integer.parseInt(t2.getText())));
}
});
add(p1,BorderLayout.NORTH);
add(p2,BorderLayout.SOUTH);
}
public static void main(String[] args){
Test frame=new Test();
frame.setTitle("计算器");
frame.setSize(300,100);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
帮你完善了下,其他来不及写了都是一样的,稍微改一下就行
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询