求助!!!!在线等!!!

zhouyu1215 2008-04-24 03:24:25
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.TreeSet;
import javax.swing.*;

class UserNumData {
private String name;
private int telNum;

UserNumData(){}
UserNumData(String name, int telNum){
this.name = name;
this.telNum = telNum;
}

public String toString(){
return "姓名: " + name + " " + "电话号码: " + telNum;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getTelNum() {
return telNum;
}
public void setTelNum(int telNum) {
this.telNum = telNum;
}

}
class TelNumQuery extends JFrame implements ActionListener {
JPanel jpanel;
JLabel jlabel1,jlabel2,jlabel3;
JTextField jtext1,jtext2,jtext3;
JButton jbutton1,jbutton2,jbutton3;
TreeSet set;
Iterator it;
UserNumData und;
TelNumQuery(){
jpanel = new JPanel();
jlabel1 = new JLabel("姓名");
jlabel2 = new JLabel("电话");
jlabel3 = new JLabel("输入要查询的姓名");
jtext1 = new JTextField(15);
jtext2 = new JTextField(15);
jtext3 = new JTextField(15);
jbutton1 = new JButton("添加");
jbutton2 = new JButton("清除");
jbutton3 = new JButton("查询");
set = new TreeSet();
this.zuZhuang();
}
public void zuZhuang(){
jpanel.setLayout(null);
jlabel1.setBounds(50, 25, 110, 30);
jtext1.setBounds(170, 25, 110, 30);
jlabel2.setBounds(50, 70, 110, 30);
jtext2.setBounds(170, 70, 110, 30);
jbutton1.setBounds(50, 115, 80, 30);
jbutton2.setBounds(170, 115, 80, 30);
jlabel3.setBounds(50, 160, 110, 30);
jtext3.setBounds(170, 160, 110, 30);
jbutton3.setBounds(50, 205, 80, 30);
jbutton1.addActionListener(this);
jbutton2.addActionListener(this);
jbutton3.addActionListener(this);
jpanel.add(jlabel1);
jpanel.add(jlabel2);
jpanel.add(jlabel3);
jpanel.add(jtext1);
jpanel.add(jtext2);
jpanel.add(jtext3);
jpanel.add(jbutton1);
jpanel.add(jbutton2);
jpanel.add(jbutton3);
this.add(jpanel);
this.setSize(330,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == jbutton1){
if(jtext1.getText().compareTo("") != 0 && (jtext2.getText().compareTo("") != 0)){
set.add(new UserNumData(jtext1.getText().trim(), Integer.parseInt(jtext2.getText().trim())));
JOptionPane.showMessageDialog(this, "添加完成");
jtext1.setText("");
jtext2.setText("");
}else{
JOptionPane.showMessageDialog(this, "请输入姓名及电话");
jtext1.requestFocus();
}
}
if(e.getSource() == jbutton2){
jtext1.setText("");
jtext2.setText("");
jtext3.setText("");
jtext1.requestFocus();
}
if(e.getSource() == jbutton3){
if(jtext3.getText().compareTo("") != 0){
it = set.iterator();
while(it.hasNext()){
und = (UserNumData)it.next();
if(jtext3.getText().trim().compareTo(und.getName()) == 0){
JOptionPane.showMessageDialog(this, und);
}
}
}else{
JOptionPane.showMessageDialog(this, "请输入要查询的姓名");
jtext3.requestFocus();
}
}
}
public static void main(String[] args) {
new TelNumQuery();

}
}

运行之后当插入第2条记录时是就会出错,第一条没问题,求助各位dd
...全文
115 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhouyu1215 2008-04-24
  • 打赏
  • 举报
回复
3q 问题解决了
believefym 2008-04-24
  • 打赏
  • 举报
回复

set = new TreeSet(new java.util.Comparator(){
public int compare(Object o1, Object o2){
UserNumData u1 = (UserNumData)o1;
UserNumData u2 = (UserNumData)o2;
return u1.getTelNum()+u1.getName().hashCode() - (u2.getTelNum()+u2.getName().hashCode());
}
});
believefym 2008-04-24
  • 打赏
  • 举报
回复
This class guarantees that the sorted set will be in ascending element order, sorted according to the natural order of the elements (see Comparable), or by the comparator provided at set creation time, depending on which constructor is used.
simonn88 2008-04-24
  • 打赏
  • 举报
回复
将类UserNumData 实现 Comparable接口就行
wanghongjue 2008-04-24
  • 打赏
  • 举报
回复
treeSet必须加的对象实现Comparable接口
class UserNumData implements Comparable
并实现它的public int compareTo(Object o) {
return 0;
}
方法
Shine_Panda 2008-04-24
  • 打赏
  • 举报
回复
错误信息贴出来
看看 。。。。。。。。。。。。。。

62,623

社区成员

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

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