当前位置: 代码迷 >> Java相关 >> 请教高手,高手进!!!!!
  详细解决方案

请教高手,高手进!!!!!

热度:207   发布时间:2005-12-20 09:12:00.0
请教高手,高手进!!!!!

该程序的作用是,点击按钮,把 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  英文  

----------------解决方案--------------------------------------------------------
String Eng=new String[15];
String Chi=new String[15];

没有事先定义!!怎么就能实现呢??
----------------解决方案--------------------------------------------------------

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

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有这个变量吗?

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


----------------解决方案--------------------------------------------------------
谢谢斑竹!!
不过,我运行的时候还是有很多错啊 !!!

----------------解决方案--------------------------------------------------------

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


----------------解决方案--------------------------------------------------------

我写了一个这样的程序……不知道你们能不能运行,我的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());
}
}


----------------解决方案--------------------------------------------------------
好像运行时候有错误啊,runtime 的错误啊,过个了编译了啊,斑竹!!!
不过,我不知道怎么改,因为swing 类我还没学啊,呵呵!!!!
----------------解决方案--------------------------------------------------------
我错了,我的jdk 是,1.4怪不的啊,谢谢斑竹!!!
----------------解决方案--------------------------------------------------------
  相关解决方案