展开全部
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CalcTest extends JFrame implements ActionListener{
JTextField txt=new JTextField(15);
JButton delete=new JButton("DE");
String [] str={"7","8","9","+",
"4","5","6","-",
"1","2","3","*",
"0","CE","=","/"};
JButton [] btn=new JButton[16];
JPanel pan1=new JPanel();
JPanel pan2=new JPanel();
CalcTest(){
super("计算器");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pan1.add(txt); pan1.add(delete);
delete.addActionListener(this);
pan2.setLayout(new GridLayout(4,4));
for(int i=0;i<16;i++){
btn[i]=new JButton(str[i]);
pan2.add(btn[i]);btn[i].addActionListener(this);
}
getContentPane().add(pan1,"North");
getContentPane().add(pan2,"Center");
pack();
setVisible(true);
setResizable(false);
}
public static void main(String[] args) {
new CalcTest();
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btn[0])
txt.setText(txt.getText()+"7");
if(e.getSource()==btn[1])
txt.setText(txt.getText()+"8");
if(e.getSource()==btn[2])
txt.setText(txt.getText()+"9");
if(e.getSource()==btn[3])
txt.setText(txt.getText()+"+");
if(e.getSource()==btn[4])
txt.setText(txt.getText()+"4");
if(e.getSource()==btn[5])
txt.setText(txt.getText()+"5");
if(e.getSource()==btn[6])
txt.setText(txt.getText()+"6");
if(e.getSource()==btn[7])
txt.setText(txt.getText()+"-");
if(e.getSource()==btn[8])
txt.setText(txt.getText()+"1");
if(e.getSource()==btn[9])
txt.setText(txt.getText()+"2");
if(e.getSource()==btn[10])
txt.setText(txt.getText()+"3");
if(e.getSource()==btn[11])
txt.setText(txt.getText()+"*");
if(e.getSource()==btn[12])
txt.setText(txt.getText()+"0");
if(e.getSource()==btn[13])
txt.setText("");
if(e.getSource()==btn[14]){
String str=txt.getText();
int n=str.length(),i;
double result1,result2;
char ch='0';
for( i=0;i<n-1;i++){
if(str.substring(i, i+1).equals("+"))
{
if(i!=0){ch = '+';break;}
}
if(str.substring(i, i+1).equals("-"))
{
if(i!=0){ch = '-';break;}
}
if(str.substring(i, i+1).equals("*"))
{
if(i!=0){ch = '*';break;}
}
if(str.substring(i, i+1).equals("/"))
{
if(i!=0){ ch = '/'; break;}
}
}
result1=Double.parseDouble(str.substring(0, i));
result2=Double.parseDouble(str.substring(i+1, n));
switch(ch){
case '+':result1=result1+result2;break;
case '-':result1=result1-result2;break;
case '*':result1=result1*result2;break;
case '/':result1=result1/result2;break;
}
txt.setText(""+result1);
}
if(e.getSource()==btn[15])
txt.setText(txt.getText()+"/");
if(e.getSource()==delete)
txt.setText(txt.getText().substring(0,txt.getText().length()-1));
}
}//简单的计算器,只能做加减乘除运算。你运行下看可行
import java.awt.*;
import java.awt.event.*;
public class CalcTest extends JFrame implements ActionListener{
JTextField txt=new JTextField(15);
JButton delete=new JButton("DE");
String [] str={"7","8","9","+",
"4","5","6","-",
"1","2","3","*",
"0","CE","=","/"};
JButton [] btn=new JButton[16];
JPanel pan1=new JPanel();
JPanel pan2=new JPanel();
CalcTest(){
super("计算器");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pan1.add(txt); pan1.add(delete);
delete.addActionListener(this);
pan2.setLayout(new GridLayout(4,4));
for(int i=0;i<16;i++){
btn[i]=new JButton(str[i]);
pan2.add(btn[i]);btn[i].addActionListener(this);
}
getContentPane().add(pan1,"North");
getContentPane().add(pan2,"Center");
pack();
setVisible(true);
setResizable(false);
}
public static void main(String[] args) {
new CalcTest();
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btn[0])
txt.setText(txt.getText()+"7");
if(e.getSource()==btn[1])
txt.setText(txt.getText()+"8");
if(e.getSource()==btn[2])
txt.setText(txt.getText()+"9");
if(e.getSource()==btn[3])
txt.setText(txt.getText()+"+");
if(e.getSource()==btn[4])
txt.setText(txt.getText()+"4");
if(e.getSource()==btn[5])
txt.setText(txt.getText()+"5");
if(e.getSource()==btn[6])
txt.setText(txt.getText()+"6");
if(e.getSource()==btn[7])
txt.setText(txt.getText()+"-");
if(e.getSource()==btn[8])
txt.setText(txt.getText()+"1");
if(e.getSource()==btn[9])
txt.setText(txt.getText()+"2");
if(e.getSource()==btn[10])
txt.setText(txt.getText()+"3");
if(e.getSource()==btn[11])
txt.setText(txt.getText()+"*");
if(e.getSource()==btn[12])
txt.setText(txt.getText()+"0");
if(e.getSource()==btn[13])
txt.setText("");
if(e.getSource()==btn[14]){
String str=txt.getText();
int n=str.length(),i;
double result1,result2;
char ch='0';
for( i=0;i<n-1;i++){
if(str.substring(i, i+1).equals("+"))
{
if(i!=0){ch = '+';break;}
}
if(str.substring(i, i+1).equals("-"))
{
if(i!=0){ch = '-';break;}
}
if(str.substring(i, i+1).equals("*"))
{
if(i!=0){ch = '*';break;}
}
if(str.substring(i, i+1).equals("/"))
{
if(i!=0){ ch = '/'; break;}
}
}
result1=Double.parseDouble(str.substring(0, i));
result2=Double.parseDouble(str.substring(i+1, n));
switch(ch){
case '+':result1=result1+result2;break;
case '-':result1=result1-result2;break;
case '*':result1=result1*result2;break;
case '/':result1=result1/result2;break;
}
txt.setText(""+result1);
}
if(e.getSource()==btn[15])
txt.setText(txt.getText()+"/");
if(e.getSource()==delete)
txt.setText(txt.getText().substring(0,txt.getText().length()-1));
}
}//简单的计算器,只能做加减乘除运算。你运行下看可行
更多追问追答
追问
谢谢 不过我们班很多人做了计算机了 我不想做这个 能帮我做个 有5个按钮的 四个按钮分别写有红色,黑色,白色,灰色的。另一个写退出 当我按红色按钮的时候整个界面变成红色,以此内推。当我按退出时,程序结束(退出)。谢谢
追答
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class ColorTest extends JFrame implements ActionListener{
JPanel pan=new JPanel();
JButton bRed=new JButton("红色");
JButton bWhite=new JButton("白色");
JButton bGray=new JButton("灰色");
JButton bBlack=new JButton("黑色");
JButton bExit=new JButton("退出");
ColorTest(){
super("颜色测试");
pan.add(bRed);bRed.addActionListener(this);
pan.add(bWhite);bWhite.addActionListener(this);
pan.add(bGray);bGray.addActionListener(this);
pan.add(bBlack);bBlack.addActionListener(this);
pan.add(bExit);bExit.addActionListener(this);
setContentPane(pan);
setSize(400,400);
setResizable(false);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==bRed)
pan.setBackground(Color.red);
if(e.getSource()==bWhite)
pan.setBackground(Color.white);
if(e.getSource()==bGray)
pan.setBackground(Color.gray);
if(e.getSource()==bBlack)
pan.setBackground(Color.black);
if(e.getSource()==bExit)
System.exit(0);
}
public static void main(String[] args) {
new ColorTest();
}
}
//按照你的要求了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询