| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 825 人关注过本帖
标题:请教高手,高手进!!!!!
只看楼主 加入收藏
wsjmt2334031
Rank: 1
等 级:新手上路
帖 子:35
专家分:0
注 册:2004-11-17
收藏
 问题点数:0 回复次数:7 
请教高手,高手进!!!!!

该程序的作用是,点击按钮,把 textfield 里的内容的英文部分添加到choice的项目,如果选择下拉菜单中的英文单词,把对应的中文在标签上 (lab2) 显示出来. 在textfield中输入的英文和中文,之间用#间隔!!
望指教!!!!!!!!!!!!!


12,13行有错误,我个人认为是创建字符串对象数组的时候,出错,可是就找不出来,望指教!!!!!!!!!!!!!


import java.awt.*;
import java.awt.event.*;
public class dictionary extends Frame implements ActionListener,ItemListener,TextListener
{
static dictionary frm=new dictionary();
static Label lab1=new Label("中文释义为:");
static Label lab2=new Label();
static TextField tex=new TextField();
static Button btn=new Button("添加");
static Choice cho=new Choice();
String Eng[];String Chi[];
Eng=new String[15];
Chi=new String[15];


public static void main(String args[])
{
frm.setLayout(null);
frm.setTitle("应用与维护");
frm.setBounds(100,100,700,700);
lab1.setBounds(100,100,300,100);
lab2.setBounds(100,250,400,100);
tex.setBounds(100,400,600,100);
btn.setBounds(100,550,100,100);
cho.setBounds(250,550,350,100);
cho.add("英文单词");
tex.addTextListener(frm);
btn.addActionListener(frm);
cho.addItemListener(frm);
frm.add(lab1);
frm.add(lab2);
frm.add(tex);
frm.add(btn);
frm.add(cho);
frm.setViseble(true);
}
public void textValueChanged(TextEvent e)
{
for(int a=1;a<=15;a++)
System.out.println(a+"次处理");
}
public void actionPerformed(ActionEvent e)
{
String str;
str=tex.getText();
inti=1;
char c=str.charAt(i);
while(c!='#')
{
i++;
c=str.charAt(i);
}
int j=0;
while(j<15)
{
Eng[j]=new String();
Chi[j]=new String();
Eng[j]=str.substring(0,i);
CHi[j]=set.substring(i);
if(cho.getItemCount()<=15)
cho.add(Eng[j]);
j++;
}
tex.setText(" ");
}
public void itemStateChanged(ItemEvent e)
{
int dex;
dex=cho.getSelectedIndex();
if(dex>=1)
lab2.setText(Chi[dex-1]);
}
}

搜索更多相关主题的帖子: choice public dictionary 英文 
2005-12-20 09:12
新坏男人
Rank: 1
等 级:新手上路
威 望:1
帖 子:38
专家分:0
注 册:2005-12-7
收藏
得分:0 
String Eng=new String[15];
String Chi=new String[15];

没有事先定义!!怎么就能实现呢??

/UploadFile/2005-11/2005112317153975.gif" border="0" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://www./UploadFile/2005-11/2005112317153975.gif');}" onmousewheel="return imgzoom(this);" alt="" />
2005-12-20 18:00
飘飘叶子
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:34
帖 子:597
专家分:10
注 册:2005-8-17
收藏
得分:0 

如果只是要改为无错的话,可以这样写

public class dictionary extends Frame implements ActionListener,ItemListener,TextListener
{
static dictionary frm=new dictionary();
static Label lab1=new Label("中文释义为:");
static Label lab2=new Label();
static TextField tex=new TextField();
static Button btn=new Button("添加");
static Choice cho=new Choice();
String Eng[]=new String[15];String Chi[]=new String[15];

public static void main(String args[])
{
frm.setLayout(null);
frm.setTitle("应用与维护");
frm.setBounds(100,100,700,700);
lab1.setBounds(100,100,300,100);
lab2.setBounds(100,250,400,100);
tex.setBounds(100,400,600,100);
btn.setBounds(100,550,100,100);
cho.setBounds(250,550,350,100);
cho.add("英文单词");
tex.addTextListener(frm);
btn.addActionListener(frm);
cho.addItemListener(frm);
frm.add(lab1);
frm.add(lab2);
frm.add(tex);
frm.add(btn);
frm.add(cho);
frm.setVisible(true);
}
public void textValueChanged(TextEvent e)
{
for(int a=1;a<=15;a++)
System.out.println(a+"次处理");
}
public void actionPerformed(ActionEvent e)
{
String str;
str=tex.getText();
int i=1;
char c=str.charAt(i);
while(c!='#')
{
i++;
c=str.charAt(i);
}
int j=0;
while(j<15)
{
Eng[j]=new String();
Chi[j]=new String();
Eng[j]=str.substring(0,i);
Chi[j]=str.substring(i);
if(cho.getItemCount()<=15)
cho.add(Eng[j]);
j++;
}
tex.setText(" ");
}
public void itemStateChanged(ItemEvent e)
{
int dex;
dex=cho.getSelectedIndex();
if(dex>=1)
lab2.setText(Chi[dex-1]);
}
}

请注意缩进和格式。还有你的程序里面最多的错误居然是拼写错误,
inti = 1;空格不见了。
set.substring str变成了set
CHi[j]=set.substring(i); CHi有这个变量吗?

你的程序虽然可以运行了,但仍然在运行时有很多错误,请继续努力吧!


向着软件工程师的目标前进!
2005-12-20 18:26
wsjmt2334031
Rank: 1
等 级:新手上路
帖 子:35
专家分:0
注 册:2004-11-17
收藏
得分:0 
谢谢斑竹!!
不过,我运行的时候还是有很多错啊 !!!
2005-12-21 12:25
shuxia104
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2005-12-21
收藏
得分:0 

那样修改过以后好像也不对.错误也很多

2005-12-22 14:45
飘飘叶子
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:34
帖 子:597
专家分:10
注 册:2005-8-17
收藏
得分:0 

我写了一个这样的程序……不知道你们能不能运行,我的jdk是1.5的

程序代码:

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

import javax.swing.*;

public class DisplayDemo extends JFrame implements ActionListener, ItemListener
{
private JButton btn;

private JTextField t;

private JLabel lbltext, lbldisplay;

private JComboBox combox;

private Vector vect;

private JPanel p;

private String str, strEn = \"\", strCn = \"\", s;

public DisplayDemo(String title)
{
super(title);

s = \"Please Choise an English\";

vect = new Vector();
vect.addElement(s);

p = new JPanel();

t = new JTextField(20);

lbltext = new JLabel(\"中文释义为: \");
lbldisplay = new JLabel();

btn = new JButton(\"添加\");
btn.addActionListener(this);


combox = new JComboBox();
combox.addItem(s);
combox.addItemListener(this);

p.add(lbltext);
p.add(lbldisplay);
p.add(t);
p.add(btn);
p.add(combox);

getContentPane().add(p);

getRootPane().setDefaultButton(btn);
}

public static void main(String[] args)
{
DisplayDemo f = new DisplayDemo(\"Test\");
f.setSize(500, 300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}

public void actionPerformed(ActionEvent e)
{
str = t.getText();
int i = 0;
char ch;
strEn = \"\";
strCn = \"\";

if (str.equals(\"\"))
{
JOptionPane.showMessageDialog(this, \"你什么都没填!\", \"填写错误\",
JOptionPane.ERROR_MESSAGE);
return;
}
else
{
ch = str.charAt(i);
try
{
while (ch != '#')
{
strEn = strEn + String.valueOf(ch);
i++;
ch = str.charAt(i);
}
}
catch (StringIndexOutOfBoundsException strException)
{
JOptionPane.showMessageDialog(this,
\"必须包含英文和中文并且使用#隔开!\", \"填写错误\",
JOptionPane.ERROR_MESSAGE);
return;
}
}
if (i == 0)
{
JOptionPane.showMessageDialog(this, \"英文不能为空!\", \"填写错误\",
JOptionPane.ERROR_MESSAGE);
return;
}
strCn = str.substring(strEn.length() + 1, str.length());

if (strCn.equals(\"\"))
{
JOptionPane.showMessageDialog(this, \"中文解释不能为空!\",
\"填写错误\", JOptionPane.ERROR_MESSAGE);
return;
}

combox.addItem(strEn);

vect.addElement(strCn);

t.setText(\"\");
}

public void itemStateChanged(ItemEvent e)
{
int i;
i = combox.getSelectedIndex();
if(i != 0)
lbldisplay.setText(vect.elementAt(i).toString());
}
}


向着软件工程师的目标前进!
2005-12-23 22:42
wsjmt2334031
Rank: 1
等 级:新手上路
帖 子:35
专家分:0
注 册:2004-11-17
收藏
得分:0 
好像运行时候有错误啊,runtime 的错误啊,过个了编译了啊,斑竹!!!
不过,我不知道怎么改,因为swing 类我还没学啊,呵呵!!!!
2005-12-31 10:37
wsjmt2334031
Rank: 1
等 级:新手上路
帖 子:35
专家分:0
注 册:2004-11-17
收藏
得分:0 
我错了,我的jdk 是,1.4怪不的啊,谢谢斑竹!!!
2005-12-31 10:38
快速回复:请教高手,高手进!!!!!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012523 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved