主题:一个简易的图形化通讯录。新手。错误多的可怕。。= =
主类Main代码:
import javax.swing.*;
public class Main{
public static void main(String[] args) {
JFrame ex=new JFrame("简易通讯录");
ex.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
ex.getContentPane().add (new Flame ());
ex.setSize(600, 500);
ex.setVisible(true);
}
}
图形化面板代码:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Flame extends JPanel{
Contact c=new Contact();
private JButton b1,b2;
private JTextField t1,t2,t3,t4;
private JTextArea show;
public Flame(){
setBackground(Color.PINK);
setLayout(new GridLayout(4,1));
JPanel p1=new JPanel();
p1.setPreferredSize(new Dimension(600,20));
JLabel l1=new JLabel("姓名:");
JTextField t1=new JTextField(8);
t1.setText("");
JLabel l2=new JLabel("电话:");
JTextField t2=new JTextField(16);
t2.setText("");
JButton b1=new JButton("增加");
b1.addActionListener(new ButtonListener1());
p1.add(l1);
p1.add(t1);
p1.add(l2);
p1.add(t2);
p1.add(b1);
JPanel s=new JPanel();
s.setPreferredSize(new Dimension(600,220));
JTextArea show=new JTextArea(600,200);
s.add(show);
JPanel p2=new JPanel();
p2.setPreferredSize(new Dimension(600,20));
JLabel l3=new JLabel("姓名:");
JTextField t3=new JTextField(8);
t3.setText("");
JLabel l4=new JLabel("电话:");
JTextField t4=new JTextField(16);
t4.setText("");
JButton b2=new JButton("删除");
b2.addActionListener(new ButtonListener2());
p2.add(l3);
p2.add(t3);
p2.add(l4);
p2.add(t4);
p2.add(b2);
JPanel help=new JPanel();
help.setPreferredSize(new Dimension(600,240));
JLabel h1=new JLabel("帮助:程序分顶层、中间、底层三部分。");
JLabel h2=new JLabel("顶层为向通讯录增加事项的工作区,填入姓名和电话点击增加即可。");
JLabel h3=new JLabel("中间为显示通讯录的工作区。");
JLabel h4=new JLabel("底层为删除通讯录中某一事项的工作区,填入姓名或电话(只可填一项,另一项应置空)后点击删除即可。");
JLabel h5=new JLabel("感谢使用此超级简陋的通讯录,祝你开心^_^");
help.add(h1);
help.add(h2);
help.add(h3);
help.add(h4);
help.add(h5);
add(p1);
add(s);
add(p2);
add(help);
}
private class ButtonListener1 implements ActionListener{
public void actionPerformed (ActionEvent e){
String s1=t1.getText();
String s2=t2.getText();
c.add(new Person(s1,s2));
for(int i=0;i<c.size();i++)
show.setText("姓名:"+c.get(i).getName()+" 电话:"+c.get(i).getTel()+"\n");
t1.setText("");
t2.setText("");
}
}
private class ButtonListener2 implements ActionListener{
public void actionPerformed (ActionEvent e){
String s="";
if(t4.getText().equals(s)){
String search=t3.getText();
for(int i=0;i<c.size();i++){
if(c.get(i).getName().equals(search)){
c.del(i);
break;
}
}
}
else{
String search=t4.getText();
for(int i=0;i<c.size();i++){
if(c.get(i).getTel().equals(search)){
c.del(i);
break;
}
}
}
for(int i=0;i<c.size();i++)
show.setText("姓名:"+c.get(i).getName()+" 电话:"+c.get(i).getTel()+"\n");
}
}
}
通讯录人员类代码:
public class Person {
private String name;
private String tel;
public Person(String name,String tel){
this.name=name;
this.tel=tel;
}
public void setName(String name){
this.name=name;
}
public void setTel(String tel){
this.tel=tel;
}
public String getName(){
return name;
}
public String getTel(){
return tel;
}
public String toString(){
return name+"/"+tel;
}
}
建立个Vector类管理成员代码:
import java.util.*;
public class Contact
{
private Vector<Person> p;
public Contact(){
p=new Vector<Person>();
}
public void add(Person someone){
p.addElement(someone);
}
public Person get(int index){
return p.get(index);
}
public void del(int index){
p.remove(index);
}
public int size(){
return p.size();
}
}
————————————————————
代码有些多。请大家耐心看看吧。初学JAVA。用NetBeans运行的时候出现了那一大堆错误。。怕了。。请高手指点哪里有错误。抑或是哪里还需要往更好更精简的方向修改。谢谢各位大大了。。
import javax.swing.*;
public class Main{
public static void main(String[] args) {
JFrame ex=new JFrame("简易通讯录");
ex.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
ex.getContentPane().add (new Flame ());
ex.setSize(600, 500);
ex.setVisible(true);
}
}
图形化面板代码:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Flame extends JPanel{
Contact c=new Contact();
private JButton b1,b2;
private JTextField t1,t2,t3,t4;
private JTextArea show;
public Flame(){
setBackground(Color.PINK);
setLayout(new GridLayout(4,1));
JPanel p1=new JPanel();
p1.setPreferredSize(new Dimension(600,20));
JLabel l1=new JLabel("姓名:");
JTextField t1=new JTextField(8);
t1.setText("");
JLabel l2=new JLabel("电话:");
JTextField t2=new JTextField(16);
t2.setText("");
JButton b1=new JButton("增加");
b1.addActionListener(new ButtonListener1());
p1.add(l1);
p1.add(t1);
p1.add(l2);
p1.add(t2);
p1.add(b1);
JPanel s=new JPanel();
s.setPreferredSize(new Dimension(600,220));
JTextArea show=new JTextArea(600,200);
s.add(show);
JPanel p2=new JPanel();
p2.setPreferredSize(new Dimension(600,20));
JLabel l3=new JLabel("姓名:");
JTextField t3=new JTextField(8);
t3.setText("");
JLabel l4=new JLabel("电话:");
JTextField t4=new JTextField(16);
t4.setText("");
JButton b2=new JButton("删除");
b2.addActionListener(new ButtonListener2());
p2.add(l3);
p2.add(t3);
p2.add(l4);
p2.add(t4);
p2.add(b2);
JPanel help=new JPanel();
help.setPreferredSize(new Dimension(600,240));
JLabel h1=new JLabel("帮助:程序分顶层、中间、底层三部分。");
JLabel h2=new JLabel("顶层为向通讯录增加事项的工作区,填入姓名和电话点击增加即可。");
JLabel h3=new JLabel("中间为显示通讯录的工作区。");
JLabel h4=new JLabel("底层为删除通讯录中某一事项的工作区,填入姓名或电话(只可填一项,另一项应置空)后点击删除即可。");
JLabel h5=new JLabel("感谢使用此超级简陋的通讯录,祝你开心^_^");
help.add(h1);
help.add(h2);
help.add(h3);
help.add(h4);
help.add(h5);
add(p1);
add(s);
add(p2);
add(help);
}
private class ButtonListener1 implements ActionListener{
public void actionPerformed (ActionEvent e){
String s1=t1.getText();
String s2=t2.getText();
c.add(new Person(s1,s2));
for(int i=0;i<c.size();i++)
show.setText("姓名:"+c.get(i).getName()+" 电话:"+c.get(i).getTel()+"\n");
t1.setText("");
t2.setText("");
}
}
private class ButtonListener2 implements ActionListener{
public void actionPerformed (ActionEvent e){
String s="";
if(t4.getText().equals(s)){
String search=t3.getText();
for(int i=0;i<c.size();i++){
if(c.get(i).getName().equals(search)){
c.del(i);
break;
}
}
}
else{
String search=t4.getText();
for(int i=0;i<c.size();i++){
if(c.get(i).getTel().equals(search)){
c.del(i);
break;
}
}
}
for(int i=0;i<c.size();i++)
show.setText("姓名:"+c.get(i).getName()+" 电话:"+c.get(i).getTel()+"\n");
}
}
}
通讯录人员类代码:
public class Person {
private String name;
private String tel;
public Person(String name,String tel){
this.name=name;
this.tel=tel;
}
public void setName(String name){
this.name=name;
}
public void setTel(String tel){
this.tel=tel;
}
public String getName(){
return name;
}
public String getTel(){
return tel;
}
public String toString(){
return name+"/"+tel;
}
}
建立个Vector类管理成员代码:
import java.util.*;
public class Contact
{
private Vector<Person> p;
public Contact(){
p=new Vector<Person>();
}
public void add(Person someone){
p.addElement(someone);
}
public Person get(int index){
return p.get(index);
}
public void del(int index){
p.remove(index);
}
public int size(){
return p.size();
}
}
————————————————————
代码有些多。请大家耐心看看吧。初学JAVA。用NetBeans运行的时候出现了那一大堆错误。。怕了。。请高手指点哪里有错误。抑或是哪里还需要往更好更精简的方向修改。谢谢各位大大了。。