用JAVA 编写一个图形应用程序,可以是文本编辑器,计算器。。。。越简洁越好 最好有解释 谢谢
1个回答
展开全部
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calutor extends JFrame implements ActionListener
{
JTextField jtf=null;
JButton jbb=null;
JButton jbc=null;
JButton[][] jb=new JButton[4][5];
JPanel jp1,jp2,jp3=null;
String st[][]={
{"1","2","3","+","-"},
{"4","5","6","*","/"},
{"7","8","9","%","sqrt"},
{"0","(",")","1/x","="},
};
String str=null;//中间变量值
double num1=0,num2=0;//中间变量值存储
double sum=0;//中间变量结果存储
int mark=0;//符号标记
public Calutor(){
//文本框布局
jtf=new JTextField("0",25);
jtf.setHorizontalAlignment(JTextField.RIGHT);
jp1=new JPanel();
jp1.add(jtf);
this.add(jp1);
//backspac与clear按钮布局
jbb=new JButton("backspace");
jbb.addActionListener(this);
jbc=new JButton("clear");
jbc.addActionListener(this);
jp2=new JPanel();
jp2.setBounds(0, 40, 300, 40);
jp2.add(jbb);
jp2.add(jbc);
this.add(jp2);
//数字键按钮 与 运算符按钮布局
jp3=new JPanel();
jp3.setBounds(0, 80, 305,200 );
for(int i=0;i<jb.length;i++){
for(int j=0;j<jb[i].length;j++){
jb[i][j]=new JButton(st[i][j]);
jp3.add(jb[i][j]);
jb[i][j].addActionListener(this);
jb[i][j].setActionCommand(st[i][j]);
}
}
this.add(jp3);
//jframe框架
this.setTitle("计算器");
this.setSize(307, 300);
// this.setResizable(false);
this.setLayout(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args)
{
new Calutor();
}
@Override
public void actionPerformed(ActionEvent e)
{
String num=e.getActionCommand();
//backspace按钮
if(e.getSource().equals(jbb)){
str=jtf.getText();
if(str.equals("0")||str.equals("")){
JOptionPane.showMessageDialog(this, "非法操作");
}else{
str=str.substring(0,str.length()-1);
jtf.setText(str);
}
}
//clear按钮
if(e.getSource().equals(jbc)){
str=jtf.getText();
jtf.setText(" ");
}
//功能区域按钮
if(num=="+"){
str=jtf.getText();
num1=Double.parseDouble(str);
mark=1;
jtf.setText("");
}
if(num=="-"){
str=jtf.getText();
num1=Double.parseDouble(str);
mark=2;
jtf.setText("");
}
if(num=="*"){
str=jtf.getText();
num1=Double.parseDouble(str);
mark=3;
jtf.setText("");
}
if(num=="/"){
str=jtf.getText();
num1=Double.parseDouble(str);
mark=4;
jtf.setText("");
}
if(num=="%"){
str=jtf.getText();
num1=Double.parseDouble(str);
mark=5;
jtf.setText("");
}
if(num=="sqrt"){
str=jtf.getText();
num1=Double.parseDouble(str);
sum=Math.sqrt(num1);
str=""+sum;
jtf.setText(str);
}
if(num=="1/x"){
str=jtf.getText();
num1=Double.parseDouble(str);
sum=1/num1;
str=""+sum;
jtf.setText(str);
}
//数字区域按钮
if(num=="0"||num=="1"||num=="2"||num=="3"||num=="4"||num=="5"||num=="6"||num=="7"||num=="8"||num=="9"){
str=jtf.getText();
if(str.equals("0")){
jtf.setText(num);
}else{
str=str+num;
jtf.setText(str);
}
}
//等号查看结果功能
if(num=="="){
str=jtf.getText();
switch(mark){
case 1:
num2=Double.parseDouble(str);
sum=num1+num2;
str=""+sum;
jtf.setText(str);
break;
case 2:
num2=Double.parseDouble(str);
sum=num1-num2;
str=""+sum;
jtf.setText(str);
break;
case 3:
num2=Double.parseDouble(str);
sum=num1*num2;
str=""+sum;
jtf.setText(str);
break;
case 4:
num2=Double.parseDouble(str);
sum=num1/num2;
str=""+sum;
jtf.setText(str);
break;
case 5:
num2=Double.parseDouble(str);
sum=num1%num2;
str=""+sum;
jtf.setText(str);
break;
}
}
}
}
复制即可执行
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calutor extends JFrame implements ActionListener
{
JTextField jtf=null;
JButton jbb=null;
JButton jbc=null;
JButton[][] jb=new JButton[4][5];
JPanel jp1,jp2,jp3=null;
String st[][]={
{"1","2","3","+","-"},
{"4","5","6","*","/"},
{"7","8","9","%","sqrt"},
{"0","(",")","1/x","="},
};
String str=null;//中间变量值
double num1=0,num2=0;//中间变量值存储
double sum=0;//中间变量结果存储
int mark=0;//符号标记
public Calutor(){
//文本框布局
jtf=new JTextField("0",25);
jtf.setHorizontalAlignment(JTextField.RIGHT);
jp1=new JPanel();
jp1.add(jtf);
this.add(jp1);
//backspac与clear按钮布局
jbb=new JButton("backspace");
jbb.addActionListener(this);
jbc=new JButton("clear");
jbc.addActionListener(this);
jp2=new JPanel();
jp2.setBounds(0, 40, 300, 40);
jp2.add(jbb);
jp2.add(jbc);
this.add(jp2);
//数字键按钮 与 运算符按钮布局
jp3=new JPanel();
jp3.setBounds(0, 80, 305,200 );
for(int i=0;i<jb.length;i++){
for(int j=0;j<jb[i].length;j++){
jb[i][j]=new JButton(st[i][j]);
jp3.add(jb[i][j]);
jb[i][j].addActionListener(this);
jb[i][j].setActionCommand(st[i][j]);
}
}
this.add(jp3);
//jframe框架
this.setTitle("计算器");
this.setSize(307, 300);
// this.setResizable(false);
this.setLayout(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args)
{
new Calutor();
}
@Override
public void actionPerformed(ActionEvent e)
{
String num=e.getActionCommand();
//backspace按钮
if(e.getSource().equals(jbb)){
str=jtf.getText();
if(str.equals("0")||str.equals("")){
JOptionPane.showMessageDialog(this, "非法操作");
}else{
str=str.substring(0,str.length()-1);
jtf.setText(str);
}
}
//clear按钮
if(e.getSource().equals(jbc)){
str=jtf.getText();
jtf.setText(" ");
}
//功能区域按钮
if(num=="+"){
str=jtf.getText();
num1=Double.parseDouble(str);
mark=1;
jtf.setText("");
}
if(num=="-"){
str=jtf.getText();
num1=Double.parseDouble(str);
mark=2;
jtf.setText("");
}
if(num=="*"){
str=jtf.getText();
num1=Double.parseDouble(str);
mark=3;
jtf.setText("");
}
if(num=="/"){
str=jtf.getText();
num1=Double.parseDouble(str);
mark=4;
jtf.setText("");
}
if(num=="%"){
str=jtf.getText();
num1=Double.parseDouble(str);
mark=5;
jtf.setText("");
}
if(num=="sqrt"){
str=jtf.getText();
num1=Double.parseDouble(str);
sum=Math.sqrt(num1);
str=""+sum;
jtf.setText(str);
}
if(num=="1/x"){
str=jtf.getText();
num1=Double.parseDouble(str);
sum=1/num1;
str=""+sum;
jtf.setText(str);
}
//数字区域按钮
if(num=="0"||num=="1"||num=="2"||num=="3"||num=="4"||num=="5"||num=="6"||num=="7"||num=="8"||num=="9"){
str=jtf.getText();
if(str.equals("0")){
jtf.setText(num);
}else{
str=str+num;
jtf.setText(str);
}
}
//等号查看结果功能
if(num=="="){
str=jtf.getText();
switch(mark){
case 1:
num2=Double.parseDouble(str);
sum=num1+num2;
str=""+sum;
jtf.setText(str);
break;
case 2:
num2=Double.parseDouble(str);
sum=num1-num2;
str=""+sum;
jtf.setText(str);
break;
case 3:
num2=Double.parseDouble(str);
sum=num1*num2;
str=""+sum;
jtf.setText(str);
break;
case 4:
num2=Double.parseDouble(str);
sum=num1/num2;
str=""+sum;
jtf.setText(str);
break;
case 5:
num2=Double.parseDouble(str);
sum=num1%num2;
str=""+sum;
jtf.setText(str);
break;
}
}
}
}
复制即可执行
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询