在下面的程序中怎么样设置文本框不能直接写入数据啊?(附加:文本域呢?)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.Math.*;
public class Jsq extends JFrame implements ActionListener
{
String preTxt="",curTxt="",preOp="";
boolean isFloat=false,isNum=false,isDce=false;
private JTextField W=new JTextField("0.");
W.setEnabled(false);
W.setHorizontalAlignment(JTextField.RIGHT);
W.setPreferredSize(new Dimension(20,5));
private JButton bk=new JButton("Backapace");
private JButton ce=new JButton("CE");
private JButton c=new JButton("C");
private JButton kong=new JButton("");
private JButton mc=new JButton("MC");
private JButton qi=new JButton("7");
private JButton ba=new JButton("8");
private JButton jiu=new JButton("9");
private JButton chu=new JButton("/");
private JButton kf=new JButton("sqrt");
private JButton mr=new JButton("MR");
private JButton si=new JButton("4");
private JButton wu=new JButton("5");
private JButton liu=new JButton("6");
private JButton cheng=new JButton("*");
private JButton yu=new JButton("%");
private JButton ms=new JButton("MS");
private JButton yi=new JButton("1");
private JButton er=new JButton("2");
private JButton san=new JButton("3");
private JButton jian=new JButton("-");
private JButton ds=new JButton("1/x");
private JButton mj=new JButton("M+");
private JButton ling=new JButton("0");
private JButton jhj=new JButton("+/-");
private JButton dian=new JButton(".");
private JButton add=new JButton("+");
private JButton dy=new JButton("=");
private JMenuItem editcopy=new JMenuItem("复制(C) ctrl+c");
private JMenuItem editpaste=new JMenuItem("粘贴(P) ctrl+v");
private JMenuItem lookBZ=new JMenuItem("标准性(T)");
private JMenuItem lookKX=new JMenuItem("科学型(s)");
private JMenuItem lookFZ=new JMenuItem("科学分组(I)");
private JMenuItem helpZT=new JMenuItem("帮助主题(H)");
private JMenuItem helpGY=new JMenuItem("关于帮助主题(A)");
//W.setEditable(false);
private class WindowCloser extends WindowAdapter{
public void windowClosing(WindowEvent we){
System.exit(0);
}
}
public Jsq()
{
super("计算器");
JMenu edit=new JMenu("编辑(E)");
edit.add(editcopy); editcopy.setEnabled(true);
edit.add(editpaste);editpaste.setEnabled(true);
JMenu look=new JMenu("查看(V)");
look.add(lookBZ);lookBZ.setEnabled(true);
look.add(lookKX);lookKX.setEnabled(true);
look.addSeparator();
look.add(lookFZ);lookFZ.setEnabled(true);
JMenu help=new JMenu("帮助(H)");
help.add(helpZT);helpZT.setEnabled(true);
help.addSeparator();
help.add(helpGY);helpGY.setEnabled(true);
JMenuBar bar=new JMenuBar();
bar.add(edit);bar.add(look);
bar.add(help);
setJMenuBar(bar);
//bk.addActionListener(this);ce.addActionListener(this);
c.addActionListener(this);//mr.addActionListener(this);
ling.addActionListener(this);//ms.addActionListener(this);
yi.addActionListener(this);//mj.addActionListener(this);
er.addActionListener(this);kf.addActionListener(this);
san.addActionListener(this);cheng.addActionListener(this);
si.addActionListener(this);yu.addActionListener(this);
wu.addActionListener(this);//ds.addActionListener(this);
liu.addActionListener(this);add.addActionListener(this);
qi.addActionListener(this);dy.addActionListener(this);
ba.addActionListener(this);chu.addActionListener(this);
jiu.addActionListener(this);jian.addActionListener(this);
//dian.addActionListener(this);//jhj.addActionListener(this);
//mc.addActionListener(this);
addWindowListener(new WindowCloser());
setResizable(false);
setup();
setSize(400,248);
setVisible(true);
}
private void setup()
{
JPanel buttons1=new JPanel();
buttons1.setLayout(new GridLayout());
buttons1.add(kong);buttons1.add(bk);
buttons1.add(ce);buttons1.add(c);
JPanel buttons2=new JPanel();
buttons2.setLayout(new GridLayout(4,6));
buttons2.add(mc);buttons2.add(qi);
buttons2.add(ba);buttons2.add(jiu);
buttons2.add(chu);buttons2.add(kf);
buttons2.add(mr);buttons2.add(si);
buttons2.add(wu);buttons2.add(liu);
buttons2.add(cheng);buttons2.add(yu);
buttons2.add(ms);buttons2.add(yi);
buttons2.add(er);buttons2.add(san);
buttons2.add(jian);buttons2.add(ds);
buttons2.add(mj);buttons2.add(ling);
buttons2.add(jhj);buttons2.add(dian);
buttons2.add(add);buttons2.add(dy);
JPanel buttons3=new JPanel();
/*buttons3.setLayout(new GridLayout(2,1));
add(W);add(buttons1);*/
buttons3.setLayout(new BorderLayout());
buttons3.add("North",W);buttons3.add("South",buttons1);
//setLayout(new BorderLayout());
///*getContentPane().add("North",);*/getContentPane().add("North",buttons3);
//getContentPane().add("South",buttons2);
setLayout(new GridLayout(2,1));
getContentPane().add(buttons3);
getContentPane().add(buttons2);
}
public void actionPerformed(ActionEvent e)
{
JButton opBtn=(JButton)e.getSource();
String str;
str=opBtn.getText();
if(opBtn==c)
{
doCls();
return;
}
/*if(opBtn==dian)
{
if(!isDce)
{
disPretxt(str);
isFloat=true;
isDce=true;
return;
}
else
return;
}*/
if((opBtn==ling)||(opBtn==yi)||(opBtn==er)||(opBtn==san)||(opBtn==si)||(opBtn==wu)||(opBtn==liu)||(opBtn==qi)||(opBtn==ba)||(opBtn==jiu))
{
if(!isNum)
{
disPretxt(str);
return;
}
else
{
curTxt="";
disPretxt(str);
return;
}
}
if((opBtn==add)||(opBtn==jian)||(opBtn==cheng)||(opBtn==chu)||(opBtn==yu))
{
if(preTxt.equals(""))
{
preTxt=curTxt;
curTxt="";
W.setText(preTxt);
}
else if(!curTxt.equals(""))
{
if(!isNum)
{
doCalcul();
W.setText(preTxt);
curTxt="";
}
else
{
W.setText(preTxt);
isNum=false;
curTxt="";
}
}
/*else
{
W.setText(preTxt);
isNum=false;
}*/
disCurtxt(str);
}
if(opBtn==kf)//求平方根
{
double i;
i=Double.parseDouble(curTxt);
if(i<0)
preTxt="输入不正确!";
else
i=Math.sqrt(i);
preTxt=String.valueOf(i);
W.setText(preTxt);
curTxt="";
}
if(opBtn==dy)//等于
{
if(!preTxt.equals("")&&!curTxt.equals(""))
{
doCalcul();
W.setText(preTxt);
isNum=true;
}
}
}
public void disPretxt(String s)//当前数
{
if(s.equals(".")&&curTxt.equals(""))
{
curTxt+="0.";
}
else if(!preOp.equals(""))
{
//curTxt="";
curTxt+=s;
}
else
{
curTxt+=s;
}
//if(s.equals(".")&&!curTxt.equals(""))//浮点数输入
//curTxt+=s;
W.setText(curTxt);
//isNum=false;
}
public void doCls()//清屏
{
curTxt="";
preTxt="";
preOp="";
isNum=false;
W.setText(" 0.");
return;
}
public void disCurtxt(String s)//前一操作符
{
preOp=s;
W.setText(preTxt);
}
public void doCalcul()//计算
{
long i,j;
i=Long.parseLong(preTxt);
j=Long.parseLong(curTxt);
if(preOp.equals("+"))
{
i+=j;
}
else if(preOp.equals("-"))
{
i-=j;
}
else if(preOp.equals("*"))
{
i*=j;
}
else if(preOp.equals("%"))//求余
{
i%=j;
}
else if(preOp.equals("/"))
{
if(j==0)
{
//W.setText("除数不能为零");
curTxt="";
preTxt="除数不能为零!";
return;
}
i/=j;
}
preTxt=String.valueOf(i);
}
public static void main(String []args)
{
Jsq my=new Jsq();
}
}
[解决办法]
太长了。不易阅读。
一般情况下控件会有,disable和readonly两个属性。你都试试吧
[解决办法]
是啊,楼上说的对