几段问题代码,请求仙人指点

woshiliupingping 2010-08-02 02:28:41
import java.awt.*;//得不到我想要的结果,这程序好像将空格也作为数字了,好像我写的分析器没有起作用;
import java.awt.event.*;
import java.util.*;
public class love
{public static void main(String args[])
{ WindowFrame s=new WindowFrame("计算的窗口");
}}
class WindowFrame extends Frame implements TextListener,ActionListener
{float aver,sum=0;
TextArea a,b;
Button button;
WindowFrame(String s)
{super(s);
a=new TextArea(15,16);
b=new TextArea(15,45);
button=new Button("退出");
add(button);
setLayout(new FlowLayout());
add(a);
add(b);
button.addActionListener(this);
a.addTextListener(this);
setVisible(true);
validate();
setBounds(0,0,345,600);
}
public void actionPerformed(ActionEvent e)
{System.exit(0);}

public void textValueChanged(TextEvent e)
{try{String s;
s=a.getText();
StringTokenizer fenxi=new StringTokenizer(s," ,'\n'");
int n=fenxi.countTokens();
String c[]=new String[n];
for(int i=0;i<=n-1;i++)
{String temp=fenxi.nextToken();
c[i]=temp;
}
for(int j=0;j<n;j++)
{sum=sum+Float.parseFloat(c[j]);
aver=sum/n;
b.setText("你输入的数的和sum="+sum+'\n'+"你输入的数的平均数aver="+aver);
}
}

catch(NumberFormatException ee){}
}
}



import java.awt.*;//我输入了正确的Email格式,为什么老是提示我不合法呢,是不是我的正则表达式写错了啊!
import java.awt.event.*;
import javax.swing.border.*;
import javax.swing.*;
public class Boy
{public static void main(String args[])
{WindowBox s=new WindowBox();
}}

class WindowBox extends Frame implements ActionListener
{Box baseBox,boxv1,boxv2;
String regex="\\w{1,}@\\{1,}\56\\{1,}",s;
TextArea text;
Button button;
TextField a,b,d;

WindowBox()
{boxv1=Box.createVerticalBox();
boxv2=Box.createVerticalBox();
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(0);}});
d=new TextField(10);
a=new TextField(10);
b=new TextField(20);
boxv1.add(new Label("姓名"));
boxv1.add(Box.createVerticalStrut(8));//是Strut,不是Struct
boxv1.add(new Label("职业"));
boxv1.add(Box.createVerticalStrut(8));
boxv1.add(new Label("email"));
boxv2.add(a);
boxv2.add(Box.createVerticalStrut(8));
boxv2.add(b);
boxv2.add(Box.createVerticalStrut(8));
boxv2.add(d);
baseBox=Box.createHorizontalBox();
baseBox.add(boxv1);
baseBox.add(Box.createHorizontalStrut(8));
baseBox.add(boxv2);
text=new TextArea(12,12);
button=new Button("确定");
add(text);
add(button);
button.addActionListener(this);
setLayout(new FlowLayout());
add(baseBox);
setBounds(120,125,500,500);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{ s=d.getText();
if(s.matches(regex))
{text.append("姓名:"+a.getText()+'\n'+"职业:"+b.getText()+'\n'+"email:"+d.getText());}
else {d.setText("你输入了不合法的Email格式");}
}}



import java.awt.*;//问题程序,不能将文本框中的内容+入按钮上,为什么?
import java.awt.event.*;
public class hua
{public static void main(String args[])
{new WindowPanel();
}}
class MyPanel extends Panel implements ActionListener
{TextField text;
Button button;
MyPanel()
{text=new TextField(8);
button=new Button("按钮");
add(text);
add(button);
setBounds(0,0,200,200);
validate();
}
public void actionPerformed(ActionEvent e)
{String s;
s=text.getText();
button.setLabel(s);
}

}
class WindowPanel extends Frame
{MyPanel p1,p2;
WindowPanel()
{p1=new MyPanel();
p2=new MyPanel();
Button d=new Button("退出");
d.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent e)
{System.exit(0);}});
add(d,BorderLayout.NORTH);
add(p1,BorderLayout.EAST);
add(p2,BorderLayout.WEST);
setVisible(true);
validate();
setBounds(0,0,400,400);
}}

...全文
246 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
woshiliupingping 2010-08-27
  • 打赏
  • 举报
回复
我已经找到答案了,虽然不是楼上说的缘由
woshiliupingping 2010-08-02
  • 打赏
  • 举报
回复
可是我是用记事本写的啊
怎么贴标签啊
woshiliupingping 2010-08-02
  • 打赏
  • 举报
回复
可是我是用记事本写的啊
怎么贴标签啊
avalon 2010-08-02
  • 打赏
  • 举报
回复
最怕就是不贴标签的代码。呵呵。
染指黄昏 2010-08-02
  • 打赏
  • 举报
回复
这代码贴的 看着就乱心
bwpf_wh 2010-08-02
  • 打赏
  • 举报
回复
路过,学习学习
woshiliupingping 2010-08-02
  • 打赏
  • 举报
回复
仙人指路啊,小子急求答案中。。
woshiliupingping 2010-08-02
  • 打赏
  • 举报
回复
求解中
binangel 2010-08-02
  • 打赏
  • 举报
回复
首先第一个分析的题是有问题的,你用TextListener是不行的,这样每次你输入一个东西,不管是符号还是数字它都会有反应,这样就起不到你分析的效果。
我改了一下,你可以看一下,如下:

import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class love {
public static void main(String args[]) {
WindowFrame s = new WindowFrame("计算的窗口");
}
}

class WindowFrame extends Frame implements TextListener, ActionListener {
float aver, sum = 0;
TextArea a, b;
Button button1,button2;

WindowFrame(String s) {
super(s);
a = new TextArea(15, 16);
b = new TextArea(15, 45);
button1 = new Button("退出");
button2 = new Button("分析");
add(button1);
add(button2);
setLayout(new FlowLayout());
add(a);
add(b);
button1.addActionListener(this);
button2.addActionListener(this);
//a.addTextListener(this);
setVisible(true);
validate();
setBounds(0, 0, 345, 600);
}

public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(button2)){
try {
String s;
s = a.getText();
sum = 0;
StringTokenizer fenxi = new StringTokenizer(s, " ,'\n'");
int n = fenxi.countTokens();
String c[] = new String[n];
for (int i = 0; i <= n - 1; i++) {
String temp = fenxi.nextToken();
System.out.println(temp);
c[i] = temp;
}
for (int j = 0; j < n; j++) {
sum = sum + Float.parseFloat(c[j]);
aver = sum / n;
b.setText("你输入的数的和sum=" + sum + '\n' + "你输入的数的平均数aver=" + aver);
}
}

catch (NumberFormatException ee) {
}
}
else if (e.getSource().equals(button1)){
System.exit(0);
}
}

public void textValueChanged(TextEvent e) {
try {
String s;
s = a.getText();
StringTokenizer fenxi = new StringTokenizer(s, " ,'\n'");
int n = fenxi.countTokens();
String c[] = new String[n];
for (int i = 0; i <= n - 1; i++) {
String temp = fenxi.nextToken();
System.out.println(temp);
c[i] = temp;
}
for (int j = 0; j < n; j++) {
sum = sum + Float.parseFloat(c[j]);
aver = sum / n;
b.setText("你输入的数的和sum=" + sum + '\n' + "你输入的数的平均数aver=" + aver);
}
}

catch (NumberFormatException ee) {
}
}
}
lodachi 2010-08-02
  • 打赏
  • 举报
回复
你自己问问题都这么没耐心 谁有耐心回答你呢
快乐的2 2010-08-02
  • 打赏
  • 举报
回复
乱糟糟

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧