一直在网上找这样一个Frame,但都是控制台控制功能什么的,下面是源码,初学的。
import java.awt.Cursor;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import utils.TelnetUtil;
public class Comm extends javax.swing.JFrame {
private JList jList1;
private DefaultListModel listModel1;
private JButton jButton4;
private JButton jButton3;
private JTextField text;
private JButton jButton1;
private JButton jButton2;
private static String path1=null;
private static String filenameTemp;
private static String [] order=new String[100];
private static Map<String, String> map = new HashMap<String, String>();
public Comm(JTextField txt) {
super();
path1=this.getClass().getResource("/").getPath()+"comm.txt";
this.text = txt;
initGUI(readData(path1));
System.out.println(this.getClass().getResource("/").getPath());
}
{
// Set Look & Feel
try {
javax.swing.UIManager
.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
Comm inst = new Comm(null);
inst.setVisible(true);
}
private void initGUI(String[] name) {
setUndecorated(true);
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
getContentPane().setLayout(null);
listModel1 = new DefaultListModel();
for(int num=0;num<name.length;num++){
listModel1.addElement(name[num]);
}
jList1 = new JList(listModel1);
jList1.setFont(new java.awt.Font("微软雅黑", 0, 18));
JScrollPane scp = new JScrollPane(jList1);
scp.setBounds(5, 50, 120, 185);
getContentPane().add(scp);
jList1.setBounds(42, 28, 110, 2180);
{
jButton3 = new JButton();
getContentPane().add(jButton3);
jButton3.setText("增加");
jButton3.setBounds(2, 3, 62, 43);
jButton3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
}
{
jButton4 = new JButton();
getContentPane().add(jButton4);
jButton4.setText("删除");
jButton4.setBounds(65, 3, 62, 43);
jButton4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
}
{
jButton1 = new JButton();
getContentPane().add(jButton1);
jButton1.setText("编辑");
jButton1.setBounds(65, 240, 62, 45);
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
}
{
jButton2 = new JButton();
getContentPane().add(jButton2);
jButton2.setText("使用");
jButton2.setBounds(2, 240, 62, 45);
jButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
}
pack();
setBounds(470, 85, 130, 290);
// setLocationRelativeTo(null);
} catch (Exception e) {
e.printStackTrace();
}
}
protected void jButton2ActionPerformed(ActionEvent evt) {
readData(path1);
System.out.println(map.get(jList1.getSelectedValue()));
//text.setText(map.get(jList1.getSelectedValue()));
}
protected void jButton1ActionPerformed(ActionEvent evt) {
String oldd=map.get(jList1.getSelectedValue());
System.out.println(jList1.getSelectedValue().toString());
Fix test=new Fix(listModel1,jList1.getSelectedValue().toString(),oldd);
test.setVisible(true);
}
/** jList1增加项 */
private void jButton3ActionPerformed(ActionEvent evt) {
Contacts contact = new Contacts(listModel1);
contact.setVisible(true);
}
/** jList1删除项 */
private void jButton4ActionPerformed(ActionEvent evt) {
if (jList1.getSelectedIndex() != -1) {
System.out.println(jList1.getSelectedValue());
listModel1.remove(jList1.getSelectedIndex());
}
}
@SuppressWarnings("static-access")
/**
* 创建文件
*
* @throws IOException
*/
public static boolean creatTxt(String name) throws IOException {
boolean flag = false;
filenameTemp = path1;
File filename = new File(filenameTemp);
if (!filename.exists()) {
filename.createNewFile();
flag = true;
}
return flag;
}
/**
* 读取数据
*/
public static String [] readData(String pat) {
try {
FileReader read = new FileReader(pat);
BufferedReader br = new BufferedReader(read);
String row;
int ord=0;
while ((row = br.readLine()) != null) {
String[]sq = row.split(",");
order[ord]=sq[0];
System.out.println(sq[0]+":"+sq[1]);
map.put(sq[0], sq[1]);
ord++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}return order;
}
public String readDateall() {
String strs = "";
try {
FileReader read = new FileReader(new File(filenameTemp));
StringBuffer sb = new StringBuffer();
char ch[] = new char[1024];
int d = read.read(ch);
while (d != -1) {
String str = new String(ch, 0, d);
sb.append(str);
d = read.read(ch);
}
System.out.print(sb.toString());
String a = sb.toString().replaceAll("@@@@@", ",");
strs = a.substring(0, a.length() - 1);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return strs;
}
}
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、//第二个类,用于增加联系人
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.JTextField;
import javax.swing.JButton;
public class Contacts extends JFrame implements ActionListener {
private DefaultListModel list1;
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JLabel jLabel = null;
private JLabel jLabel1 = null;
private JTextField jTextField = null;
private JTextField jTextField1 = null;
private JButton jButton = null;
private JButton jButton1 = null;
private JButton jButton2 = null;
public Contacts(DefaultListModel list) {
super();
this.list1 = list;
initialize();
}
private void initialize() {
this.setBounds(180, 140, 290, 190);
this.setContentPane(getJContentPane());
this.setUndecorated(true);
}
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel1 = new JLabel();
jLabel1.setBounds(new Rectangle(12, 74, 85, 31));
jLabel1.setText(" 电话号码:");
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(12, 31, 85, 31));
jLabel.setText(" 姓 名:");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(jLabel, null);
jContentPane.add(jLabel1, null);
jContentPane.add(getJTextField(), null);
jContentPane.add(getJTextField1(), null);
jContentPane.add(getJButton(), null);
jContentPane.add(getJButton1(), null);
jContentPane.add(getJButton2(), null);
}
return jContentPane;
}
private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField();
jTextField.setBounds(new Rectangle(112, 29, 155, 33));
jTextField.setEditable(true);
}
return jTextField;
}
private JTextField getJTextField1() {
if (jTextField1 == null) {
jTextField1 = new JTextField();
jTextField1.setBounds(new Rectangle(112, 73, 155, 33));
jTextField1.setEditable(true);
}
return jTextField1;
}
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(20, 120, 80, 50));
jButton.setText("保存");
jButton.addActionListener(this);
}
return jButton;
}
private JButton getJButton1() {
if (jButton1 == null) {
jButton1 = new JButton();
jButton1.setBounds(new Rectangle(105, 120, 80, 50));
jButton1.setText("清除");
jButton1.addActionListener(this);
}
return jButton1;
}
private JButton getJButton2() {
if (jButton2 == null) {
jButton2 = new JButton();
jButton2.setBounds(new Rectangle(190, 120, 80, 50));
jButton2.setText("退出");
jButton2.addActionListener(this);
}
return jButton2;
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jButton) {
String str1 = jTextField.getText();
String str2 = jTextField1.getText();
if (!(str1.equals("") && str2.equals(""))) {
String str3 = str1 + "," + str2 + "\r\n";
try {
File f = new File(this.getClass().getResource("/").getPath()+"comm.txt");
FileWriter out = new FileWriter(f, true);
out.write(str3);
out.close();
} catch (IOException e1) {
e1.toString();
}
if (str1 != "") {
list1.addElement(str1);
} else {
list1.addElement(str2);
}
this.dispose();
} else {
JOptionPane.showMessageDialog(null, "null", "系统提示",
JOptionPane.WARNING_MESSAGE);
System.out.println("********************");
this.dispose();
}
}
if (e.getSource() == jButton1) {
jTextField.setText("");
jTextField1.setText("");
}
if (e.getSource() == jButton2) {
this.dispose();
}
}
}
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、第三个类,用于修改联系人
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Map;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Fix extends JFrame implements ActionListener {
private DefaultListModel list1;
private static String old;
private static String name;
private static String path1=null;
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JLabel jLabel = null;
private JLabel jLabel1 = null;
private JTextField jTextField = null;
private JTextField jTextField1 = null;
private JButton jButton = null;
private JButton jButton1 = null;
private JButton jButton2 = null;
public Fix(DefaultListModel list,String name,String old1) {
super();
this.list1 = list;
this.old=old1;
this.name=name;
path1=this.getClass().getResource("/").getPath()+"comm.txt";
initialize();
}
private void initialize() {
this.setBounds(180, 140, 290, 190);
this.setContentPane(getJContentPane());
this.setUndecorated(true);
// TODO Auto-generated method stub
}
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel1 = new JLabel();
jLabel1.setBounds(new Rectangle(12, 74, 85, 31));
jLabel1.setText(" 电话号码:");
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(12, 31, 85, 31));
jLabel.setText(" 姓 名:");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(jLabel, null);
jContentPane.add(jLabel1, null);
jContentPane.add(getJTextField(), null);
jContentPane.add(getJTextField1(), null);
jContentPane.add(getJButton(), null);
jContentPane.add(getJButton1(), null);
jContentPane.add(getJButton2(), null);
}
return jContentPane;
}
private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField(name);
jTextField.setBounds(new Rectangle(112, 29, 155, 33));
jTextField.setEditable(true);
}
return jTextField;
}
private JTextField getJTextField1() {
if (jTextField1 == null) {
jTextField1 = new JTextField(old);
jTextField1.setBounds(new Rectangle(112, 73, 155, 33));
jTextField1.setEditable(true);
}
return jTextField1;
}
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(20, 120, 80, 50));
jButton.setText("保存");
jButton.addActionListener(this);
}
return jButton;
}
private JButton getJButton1() {
if (jButton1 == null) {
jButton1 = new JButton();
jButton1.setBounds(new Rectangle(105, 120, 80, 50));
jButton1.setText("清除");
jButton1.addActionListener(this);
}
return jButton1;
}
private JButton getJButton2() {
if (jButton2 == null) {
jButton2 = new JButton();
jButton2.setBounds(new Rectangle(190, 120, 80, 50));
jButton2.setText("退出");
jButton2.addActionListener(this);
}
return jButton2;
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jButton) {
String str1 = jTextField.getText();
String str2 = jTextField1.getText();
if (!(str1.equals("") && str2.equals(""))) {
String str3 = str1 + "," + str2 + "\r\n";
replaceTxt(name+","+old+"\r\n",str3);
if (str1 != "") {
list1.addElement(str1);
} else {
list1.addElement(str2);
}
this.dispose();
} else {
JOptionPane.showMessageDialog(null, "undo", "系统提示",
JOptionPane.WARNING_MESSAGE);
System.out.println("********************");
this.dispose();
}
}
if (e.getSource() == jButton1) {
jTextField.setText("");
jTextField1.setText("");
}
if (e.getSource() == jButton2) {
this.dispose();
}
}
@SuppressWarnings("static-access")
/*更换txt文件内容
*
*/
public static void replaceTxt(String oldStr, String replaceStr) {
String temp = "";
try {
File file = new File(path1);
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
StringBuffer buf = new StringBuffer();
// 保存该行前面的内容
for (int j = 1; (temp = br.readLine()) != null
&& !temp.equals(oldStr); j++) {
buf = buf.append(temp);
buf = buf.append(System.getProperty("line.separator"));
}
// 将内容插入
buf = buf.append(replaceStr);
// 保存该行后面的内容
while ((temp = br.readLine()) != null) {
buf = buf.append(System.getProperty("line.separator"));
buf = buf.append(temp);
}
br.close();
FileOutputStream fos = new FileOutputStream(file);
PrintWriter pw = new PrintWriter(fos);
pw.write(buf.toString().toCharArray());
pw.flush();
pw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}