java中如何将字符一个个显示在文本区上?求各位大侠指点~
各位大侠好,小弟正研究java,属于菜鸟级别,现有一问题想请教:
我想做一个计算器,监听器已经加上,但就是不懂如何将按钮上的字
显示在文本区上,像我们常用的计算器那样,输入一个就显示一个,计算
后将结果显示,求个为大侠指点指点啊~分数伺候着
[解决办法]
<html>
<body>
<input id="allSum" value="0"></input>
<input type="button" value="1" width="60px" id="button1" onclick="one()"></input>
<input type="button" value="2" width="60px" id="button2"></input>
<input type="button" value="+" width="60px" id="button3"></input>
<input type="button" value="=" width="60px" id="button4"></input>
</body>
<script>
function one(){
var one=document.getElementById("button1").value;
var value=document.getElementById("allSum").value;
if(value==0){
document.getElementById("allSum").value=one ;
}else{
var result=value+one;
document.getElementById("allSum").value=result;
}
alert(one);
}
</script>
</html>
f()
{
one = new JButton("1");
two = new JButton("2");
t = new JTextField(10);
one.addActionListener(this);
two.addActionListener(this);
t.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
int result = 0;
if(e.getSource() == one)
{
t.setText("1");
}
else if(e.getSource() == two)
{
t.setText("2");
}
else if(e.getSource() == add)
{
result = Integer.parseInt(t.getText()) + result;
}
}
class f extends JFrame implements ActionListener
{
JButton add = new JButton("加");
JButton one, two;
JTextField t;
f()
{
one = new JButton("1");
two = new JButton("2");
t = new JTextField(10);
one.addActionListener(this);
two.addActionListener(this);
t.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
int result = 0;
if(e.getSource() == one)
{
t.setText("1");
}
else if(e.getSource() == two)
{
t.setText("2");
}
else if(e.getSource() == add)
{
result = Integer.parseInt(t.getText()) + result;
}
}
}
这儿有一个简单的源代码:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
public class Calculator1 extends JFrame implements ActionListener
{
private JFrame jf;
private JButton[] allButtons;
private JButton clearButton;
private JButton sinButton;
private JButton cosButton;
private JButton tanButton;
// private JButton cotButton;
private JTextField jtf;
private boolean isNew = true;
private String recentOperation = null;
private String recentNum = null;
public Calculator1()
{
/**
* 设置窗口点击关闭按钮能关闭
*/
jf=new JFrame("计算器1.0:JAVA");
jf.addWindowListener(new WindowAdapter()
{
public void windowClosing(){
System.exit(0);
}
});
// 实例化所有的按钮以及编辑栏
allButtons = new JButton[16];
clearButton = new JButton("DEL");
sinButton = new JButton("sin");
cosButton = new JButton("cos");
tanButton = new JButton("tan");
jtf=new JTextField(25);
jtf.setEditable(false);
// jtf.setHorizontalAlignment(alignment)
//给CanterPanel中的按钮添加标识符
String str="123+456-789*0.=/";
for(int i=0;i<allButtons.length;i++)
{
allButtons[i]=new JButton(str.substring(i,i+1));
}
}
public void init(){
//设置布局
jf.setLayout(new BorderLayout());
JPanel northPanel=new JPanel();
JPanel centerPanel=new JPanel();
JPanel southPanel=new JPanel();
northPanel.setLayout(new FlowLayout());
centerPanel.setLayout(new GridLayout(4,4));
southPanel.setLayout(new GridLayout(1,4));
northPanel.add(jtf);
for(int i=0;i<16;i++){
centerPanel.add(allButtons[i]);
}
southPanel.add(sinButton);
southPanel.add(cosButton);
southPanel.add(tanButton);
//southPanel.add(cotButton);
southPanel.add(clearButton);
jf.add(northPanel,BorderLayout.NORTH);
jf.add(centerPanel,BorderLayout.CENTER);
jf.add(southPanel,BorderLayout.SOUTH);
addEventHandler();
}
//添加事件监听
public void addEventHandler()
{
jtf.addActionListener(this);
for(int i=0;i<allButtons.length;i++)
{
allButtons[i].addActionListener(this);
}
/**
* 给DEL注册
*/
clearButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
Calculator1.this.jtf.setText("");
}
});
/***
* 实现sin,cos,tan 以及 allButton事件驱动按钮
*/
sinButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
double temp = Math.sin(Double.parseDouble(jtf.getText()));
jtf.setText(String.valueOf(temp));
return;
}
});
cosButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
double temp = Math.cos(Double.parseDouble(jtf.getText()));
jtf.setText(String.valueOf(temp));
return;
}
});
tanButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
double temp = Math.tan(Double.parseDouble(jtf.getText()));
jtf.setText(String.valueOf(temp));
return;
}
});
/*cotButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
double temp =- Double.parseDouble(jtf.getText());
jtf.setText(String.valueOf(temp));
return;
}
});*/
}
//对输入数字的事件处理
public void actionPerformed(ActionEvent e)
{
String s= e.getActionCommand();
if(s.charAt(0)>='0'&&s.charAt(0)<='9')
{
if(!isNew)
jtf.setText(jtf.getText()+s);
else
jtf.setText(s);
isNew = false;
}
/**
* 对“.”的事件处理
*/
else if (s.equals("."))
{
if(jtf.getText().indexOf(".")!=-1)
return;
if(!isNew &&jtf.getText()!="")
jtf.setText(jtf.getText()+".");
else
jtf.setText("0.");
isNew = false;
}
//对"="的事件处理
else if(s.equals("="))
{
equalaction(e);
}
else
{
if((jtf.getText()).equals(""))
return;
if(recentOperation !=null)equalaction(e);
recentOperation = s;
recentNum = jtf.getText();
isNew = true;
}
}
/**
* 对运算符及计算过程的事件处理
* @param e
*/
private void equalaction(ActionEvent e) {
// TODO Auto-generated method stub
if(recentOperation == null
[解决办法]
recentNum==null
[解决办法]
jtf.getText().equals(""))
return;
double last = 0,now = 0;
try
{
last = Double.parseDouble(recentNum);
now = Double.parseDouble(jtf.getText());
}
catch(NumberFormatException en)
{
recentOperation = null;
recentNum = null;
jtf.setText("数据输入不合法");
System.out.println("数据输入不合法");
isNew = true;
return;
}
if(recentOperation.equals("+"))
{
last+=now;
}
if(recentOperation.equals("-"))
{
last-=now;
}
if(recentOperation.equals("*"))
{
last*=now;
}
if(recentOperation.equals("/"))
{
last/=now;
}
jtf.setText(""+last);
recentNum = jtf.getText();
recentOperation=null;
isNew = true;
}
//对CenterPanel设置
public void setFontAndColor(){
Font f=new Font("宋体",Font.BOLD,24);
jtf.setFont(f);
jtf.setBackground(new Color(0x8f,0xa0,0xfb));
for(int i=0;i<16;i++)
{
allButtons[i].setFont(f);
allButtons[i].setForeground(Color.RED);
}
}
public void showMe(){
init();
setFontAndColor();
jf.pack();
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new Calculator1().showMe();
}
}
[解决办法]
switch(j-1)
case 0:
showtext.setText(String.valueOf(power(da1,da2)));
break;
case 1:
showtext.setText(String.valueOf(sqrt(da1)));