求助,一个计算器的括号功能怎么写啊。
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class computer1 extends JFrame implements ActionListener{
Double r1,r2,total,c1,c2;
int i,j,j1=10,j2=7,c;
JButton[] Button1,Button2;
String str1=new String();
String str2=new String();
String str=new String();
String strr=new String();
boolean flag=true;
Container cp;
JLabel output=new JLabel("输出");
JTextField outarea=new JTextField(10);
JButton Button3=new JButton("=");
JButton clear=new JButton("清除");
JButton exit=new JButton("退出");
JMenuBar menubar=new JMenuBar();
JMenu now =new JMenu("文件(F)");
JMenu edit=new JMenu("编辑(E)");
JMenu help=new JMenu("帮助(H)");
JMenu format=new JMenu("格式(O)");
JMenuItem properties=new JMenuItem("属性",'P');
JMenuItem editer=new JMenuItem("退出",'X');
JMenuItem naw =new JMenuItem("新建",'N');
JMenuItem open=new JMenuItem("打开",'O');
JMenuItem paste=new JMenuItem("复制",'C');
JMenuItem cut=new JMenuItem("剪切",'X');
JMenuItem delet=new JMenuItem("删除",'D');
JMenuItem Auok=new JMenuItem("自动换行",'W');
JMenuItem font=new JMenuItem("字体",'F');
JMenuItem about=new JMenuItem("关于..",'A');
public computer1(){
super("祥祥的简单的计算器");
super.setJMenuBar(menubar);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300,300);
now .add(naw);naw.setEnabled(false);
now.add(open);open.setEnabled(false);
now.add(properties); properties.setEnabled(false);
now.add(editer); editer.addActionListener(new exit());
edit.add(paste); paste.setEnabled(false);
edit.add(cut);cut.setEnabled(false);
edit.add(delet);delet.setEnabled(false);
format.add(Auok);Auok.setEnabled(false);
format.add(font);font.setEnabled(false);
help.add(about);about.addActionListener(new message());
now.setMnemonic('F');
edit.setMnemonic('E');
format.setMnemonic('F');
help.setMnemonic('H');
menubar.add(now);
menubar.add(edit);
menubar.add(format);
menubar.add(help);
BorderLayout layout1=new BorderLayout();
GridLayout layout2=new GridLayout(4,4,6,6);
cp=getContentPane();
JPanel panel1=new JPanel(new GridLayout(2,1));
panel1.add(output);
panel1.add(outarea);
cp.add("North",panel1);
JPanel panel2=new JPanel(layout2);
Button1=new JButton[j1];
Button2=new JButton[j2];
Button2[2]=new JButton("*");
Button2[3]=new JButton("%");
Button2[4]=new JButton("(");
Button2[5]=new JButton(")");
Button1[1]=new JButton("1");
Button1[2]=new JButton("2");
Button1[3]=new JButton("3");
Button2[0]=new JButton("+");
Button1[4]=new JButton("4");
Button1[5]=new JButton("5");
Button1[6]=new JButton("6");
Button2[1]=new JButton("-");
Button1[7]=new JButton("7");
Button1[8]=new JButton("8");
Button1[9]=new JButton("9");
Button1[0]= new JButton("0");
for (i=0 ;i<10;i++) {
panel2.add(Button1[i]);
Button1[i].addActionListener(this);
};
for (j=0 ;j<6;j++) {
panel2.add(Button2[j]);
Button2[j].addActionListener(new operation());
};
Button3=new JButton("=");
JPanel panel3=new JPanel(new GridLayout());
panel3.add(Button3);
panel3.add(clear);
panel3.add(exit);
cp.add("Center",panel2);
cp.add("South",panel3);
Button3.addActionListener(this);
clear.addActionListener(this);
exit.addActionListener(new exit());
}
public void clear(){
this.total=0.0 ;
this.str=this.str1=" ";
this.str1=" ";
this.str2=" ";
outarea.setText(" ");
flag=true;
}
class exit implements ActionListener{
public void actionPerformed(ActionEvent e)
{System.exit(0);}
}
public void actionPerformed(ActionEvent e){
for(i=0;i<10;i++){
if(e.getSource()==Button1[i]){
if(flag){
str=str.valueOf(i);
str1+=str;
outarea.setText(str1);
r1=Double.parseDouble(str1);
}
else{
str=str.valueOf(i);
str2+=str;
outarea.setText(str2);
r2=Double.parseDouble(str2);
}
}
}
if(e.getSource()==clear) clear();
if(e.getSource()==Button3){
total=0.00;
switch(c){
case 0:total=r1+r2;outarea.setText(strr.valueOf(total));strr=strr.valueOf(total);break;
case 1:total=r1-r2;outarea.setText(strr.valueOf(total));strr=strr.valueOf(total);break;
case 2:total=r1*r2;outarea.setText(strr.valueOf(total));strr=strr.valueOf(total);break;
case 3:total=r1/r2;outarea.setText(strr.valueOf(total));strr=strr.valueOf(total);break;
}
}
}
class operation implements ActionListener{
public void actionPerformed(ActionEvent a){
for(j=0;j<4;j++)
if(a.getSource()==Button2[j]){
c=j;
char aa[ ]=new char[4];
aa[0]='+';aa[1]='-';aa[2]='*';aa[3]='/';
str=str.valueOf(aa[j]);
outarea.setText(str);
flag=false;
}
}
}
class message extends JFrame implements ActionListener{
public void actionPerformed(ActionEvent e){
JFrame frame=new JFrame("信息...");
frame.setSize(60,250);
frame.getContentPane().setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JLabel label1=new JLabel("关于作者信息:" );
JLabel label2=new JLabel("祥。");
frame.getContentPane().add(label1);
frame.getContentPane().add(label2);
}
}
public static void main(String[] args){
computer1 fr= new computer1();
fr.setVisible(true);
fr.pack();
}
}
我就是实现不了括号功能,请同道友人指点一下,谢谢。
还有我觉得我的计算器还有一些不好的地方,请大家指点一下。修正一下。