java中如何将字符一个个显示在文本区上?求各位大侠指点~
各位大侠好,小弟正研究java,属于菜鸟级别,现有一问题想请教:
我想做一个计算器,监听器已经加上,但就是不懂如何将按钮上的字
显示在文本区上,像我们常用的计算器那样,输入一个就显示一个,计算
后将结果显示,求个为大侠指点指点啊~分数伺候着
[最优解释]
http://blog.csdn.net/zhaoming262350/article/details/8250807
这儿有一个简单的源代码:
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
[其他解释]
<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;
}
}
}
}
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();
}
}
[其他解释]
如果实在不能理解的话,就理解8楼的代码吧,虽然我也没看,但他是老手了,值得信任,如果1周之内楼主还解决不了的话,我的推荐是别弄这个程序了,先暂停下,把书中关于GUI的例题都打一遍,理解透了在回来继续编吧,因为编程这中东西,你要编出程序来,必须要能把知识融会贯通,不然再怎么死想也不会有太大的结果的,就算误打误撞的编出来,但是那样话费的时间精力值得吗?。。。。。
顺便教一下楼主发代码的消小方法,以前截的图,现在应该还适用。
[其他解释]
楼主,说句心里话,楼主对Java的态度很不扎实啊,这个都是一些很基本的东西,基本到只用一些构造方法和常用的方法,你要多看看书哈,我对java来说也是一个初学者,刚刚学了JAVA几周,我的方法就是不断写程序,不断的翻书,除非特别难的,一般不问的,这几周写的程序超过千行了,推荐楼主多加把劲儿啊!
[其他解释]
return data1*data2;
}
float divide(float data1,float data2) //除法
{
return data1/data2;
}
double sqrt(double data) //开方
{
return Math.sqrt(data);
}
double power(double data1,double data2) //平方
{
return Math.pow(data1, data2);
}
public void windowClosing(WindowEvent e) //关闭窗口
{
System.exit(0);
}
public void actionPerformed(ActionEvent e) //文本框的显示和计算
{
//do{
while(e.getSource()==b)
{
int j=0;
for(int i=0;i<10;i++)
if(e.getSource()==b[i])
{
st=st+""+i;
showtext.setText(st);
}
st1[j++]=st;
st="";
}
while(e.getSource()==a)
{
int i=0;
int j;
for( j=0;j<10;j++)
if(e.getSource()==a[j])
{
st=s[j];
showtext.setText(st);
break;
}
st2[i++]=""+st;
st="";
}
switch(j-1)
{
/*i=0;
if(st1[0]=="")
{
da1=0;
}
else
{
da1=Integer.parseInt(st1[i++]);
da2=Integer.parseInt(st1[i++]);
}*/
case 0:
showtext.setText(power(da1,da2));
break;
case 1:showtext.setText(sqrt(da1));
}
//}while(true);
}
public static void main(String[] args)
{
new Calculator();
}
}
[其他解释]
switch(j-1)
case 0:
showtext.setText(String.valueOf(power(da1,da2)));
break;
case 1:
showtext.setText(String.valueOf(sqrt(da1)));
public void actionPerformed(ActionEvent e)
for(int i=0;i<10;i++)
{
if(e.getSource()==b[i])
{
st=st+""+i;
showtext.setText(st);
}
}
public void actionPerformed(ActionEvent e)