欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java實現(xiàn)簡易界面通訊錄

 更新時間:2022年04月26日 11:13:43   作者:Jivan2233  
這篇文章主要為大家詳細介紹了Java實現(xiàn)簡易界面通訊錄,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

前言

這個也是Java實驗課程的一個作業(yè),和Java實現(xiàn)簡單的圖形界面計算器一起做的,因為以前沒有做過GUI編程,所以做的非常簡陋,還有很多BUG,但是感覺當個作業(yè)也夠了。

程序功能和截圖

這里的添加是直接添加到文件中,為什么不用數(shù)據(jù)庫呢?因為我們老師根本就沒教,所以也不能用.。

通過輸入的名字在文件中查找是否有該用戶,如果用,就顯示到界面上。

大致的功能就是上面兩個。

代碼

一、文件讀寫工具

package Contacts;

import java.io.*;

/**
?* Created by Yifan Jia on 2018/6/10.
?*/
public class FileRW {
? ? private static FileWriter fileWriter;

? ? private static FileReader fileReader;

? ? private static BufferedReader bf;

? ? private static BufferedWriter bw;

? ? private static File file = new File("D:\\dest.txt");
? ? public static void fileWrite(String s) {
? ? ? ? try {
? ? ? ? ? ? fileWriter = new FileWriter(file, true);
? ? ? ? ? ? bw = new BufferedWriter(fileWriter);
? ? ? ? ? ? bw.write(s);
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? } finally {
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? bw.close();
? ? ? ? ? ? ? ? fileWriter.close();
? ? ? ? ? ? } catch (IOException e) {
? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? }
? ? ? ? }
? ? }

? ? public static String fileRead(String dest) {
? ? ? ? try {
? ? ? ? ? ? fileReader = new FileReader(file);
? ? ? ? ? ? bf = new BufferedReader(fileReader);
? ? ? ? ? ? String ss;
? ? ? ? ? ? while((ss = bf.readLine()) != null) {
? ? ? ? ? ? ? ? String[] temp = ss.split(",");
? ? ? ? ? ? ? ? if(temp[0].equals(dest)) {
? ? ? ? ? ? ? ? ? ? return ss;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? } catch (FileNotFoundException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? } finally {
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? bf.close();
? ? ? ? ? ? ? ? fileReader.close();
? ? ? ? ? ? } catch (IOException e) {
? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return null;
? ? }
}

二、界面程序

package Contacts;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

//定義自已的MyPanel,用于實現(xiàn)畫圖
class MyPanelone extends JPanel {
? ? private String ss;
? ? private int x;
? ? private int y;
? ? private int size;

? ? public MyPanelone(String ss, int x, int y, int size) {
? ? ? ? this.ss = ss;
? ? ? ? this.x = x;
? ? ? ? this.y = y;
? ? ? ? this.size = size;
? ? }

? ? //覆蓋JPanel的paint方法
? ? @Override
? ? public void paint(Graphics g) {
? ? ? ? super.paint(g);
? ? ? ? g.setColor(Color.BLACK);
? ? ? ? g.setFont(new Font("宋體", Font.BOLD, size));
? ? ? ? g.drawString(ss, x, y);
? ? }
}

public class MyContacts extends JFrame{
? ? private MyPanelone myPaneone;
? ? private JPanel[] jPanels = new JPanel[7];
? ? private JButton[] jButtons = new JButton[4];
? ? private JTextField[] jTextFields = new JTextField[6];
? ? private JLabel[] jLabels = new JLabel[6];
? ? private String[] texts = new String[6];

? ? private class MyActionListener implements ActionListener {

? ? ? ? @Override
? ? ? ? public void actionPerformed(ActionEvent e) {
? ? ? ? ? ? boolean flag = true;
? ? ? ? ? ? StringBuilder s = new StringBuilder();
? ? ? ? ? ? String actionCommand = e.getActionCommand();
? ? ? ? ? ? if(actionCommand == "添加") {
? ? ? ? ? ? ? ? for (int i = 0; i < 6; i++) {
? ? ? ? ? ? ? ? ? ? texts[i] = new String();
? ? ? ? ? ? ? ? ? ? texts[i] = jTextFields[i].getText();
? ? ? ? ? ? ? ? ? ? //System.out.println(texts[i]);
? ? ? ? ? ? ? ? ? ? if(texts[i].equals("") || texts[i] == null) {
? ? ? ? ? ? ? ? ? ? ? ? flag = false;
? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? if(i == 0) {
? ? ? ? ? ? ? ? ? ? ? ? s.append(texts[i]);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? else {
? ? ? ? ? ? ? ? ? ? ? ? s.append(",").append(texts[i]);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? if(flag) {
? ? ? ? ? ? ? ? ? ? s.append("\n");
? ? ? ? ? ? ? ? ? ? //將文本域中的內(nèi)容寫成一個字符串
? ? ? ? ? ? ? ? ? ? String ss = s.toString();
? ? ? ? ? ? ? ? ? ? //將字符串寫入文件
? ? ? ? ? ? ? ? ? ? FileRW.fileWrite(ss);
? ? ? ? ? ? ? ? ? ? for(int i=0;i<6;i++) {
? ? ? ? ? ? ? ? ? ? ? ? jTextFields[i].setText("");
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? //System.out.println(ss);
? ? ? ? ? ? ? ? ? ? JFrame jFrame = new JFrame();
? ? ? ? ? ? ? ? ? ? jFrame.setBounds(500, 300, 300, 300);
? ? ? ? ? ? ? ? ? ? MyPanelone myPanelone = new MyPanelone("添加成功", 100, 100, 20);
? ? ? ? ? ? ? ? ? ? jFrame.add(myPanelone);
? ? ? ? ? ? ? ? ? ? jFrame.addWindowListener(new WindowAdapter() {
? ? ? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? ? ? public void windowClosing(WindowEvent e) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? e.getWindow().dispose();
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? ? ? jFrame.setVisible(true);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else {
? ? ? ? ? ? ? ? ? ? JFrame jFrame = new JFrame();
? ? ? ? ? ? ? ? ? ? jFrame.setBounds(500, 300, 300, 300);
? ? ? ? ? ? ? ? ? ? MyPanelone myPanelone = new MyPanelone("請把所有內(nèi)容都填寫完整", 60, 100, 15);
? ? ? ? ? ? ? ? ? ? jFrame.add(myPanelone);
? ? ? ? ? ? ? ? ? ? jFrame.addWindowListener(new WindowAdapter() {
? ? ? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? ? ? public void windowClosing(WindowEvent e) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? e.getWindow().dispose();
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? ? ? jFrame.setVisible(true);
? ? ? ? ? ? ? ? }


? ? ? ? ? ? }
? ? ? ? ? ? else if(actionCommand == "清空") {
? ? ? ? ? ? ? ? for(int i=0;i<6;i++) {
? ? ? ? ? ? ? ? ? ? jTextFields[i].setText("");
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? else if(actionCommand == "退出") {
? ? ? ? ? ? ? ? System.exit(0);
? ? ? ? ? ? }
? ? ? ? ? ? else if(actionCommand == "查找") {
? ? ? ? ? ? ? ? JFrame frame = new JFrame("輸入");

? ? ? ? ? ? ? ? JPanel jPanel = new JPanel();
? ? ? ? ? ? ? ? JPanel jPanel1 = new JPanel();
? ? ? ? ? ? ? ? JLabel jLabel = new JLabel("輸入查找人的名字");
? ? ? ? ? ? ? ? JButton jButton = new JButton("確定");
? ? ? ? ? ? ? ? JTextField jTextField = new JTextField(30);
? ? ? ? ? ? ? ? jPanel.add(jLabel);
? ? ? ? ? ? ? ? jPanel.add(jTextField);
? ? ? ? ? ? ? ? jButton.addActionListener(new ActionListener() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void actionPerformed(ActionEvent e) {
? ? ? ? ? ? ? ? ? ? ? ? String actionCommand1 = e.getActionCommand();
? ? ? ? ? ? ? ? ? ? ? ? String dest = jTextField.getText();
? ? ? ? ? ? ? ? ? ? ? ? String findresult = FileRW.fileRead(dest);
? ? ? ? ? ? ? ? ? ? ? ? if(findresult == null) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? for(int i=0;i<6;i++) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? jTextFields[i].setText("");
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? JFrame jFrame = new JFrame();
? ? ? ? ? ? ? ? ? ? ? ? ? ? jFrame.setBounds(500, 300, 300, 300);
? ? ? ? ? ? ? ? ? ? ? ? ? ? MyPanelone myPanelone = new MyPanelone("未找到該用戶", 100, 100, 20);
? ? ? ? ? ? ? ? ? ? ? ? ? ? jFrame.add(myPanelone);
? ? ? ? ? ? ? ? ? ? ? ? ? ? jFrame.addWindowListener(new WindowAdapter() {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? public void windowClosing(WindowEvent e) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? e.getWindow().dispose();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? ? ? ? ? ? ? jFrame.setVisible(true);
? ? ? ? ? ? ? ? ? ? ? ? ? ? frame.dispose();
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? else {
? ? ? ? ? ? ? ? ? ? ? ? ? ? String[] tempdest = findresult.split(",");
? ? ? ? ? ? ? ? ? ? ? ? ? ? for(int i=0;i<6;i++) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? jTextFields[i].setText(tempdest[i]);
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? frame.dispose();
? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? jPanel1.add(jButton);
? ? ? ? ? ? ? ? frame.add(jPanel, BorderLayout.CENTER);
? ? ? ? ? ? ? ? frame.add(jPanel1, BorderLayout.SOUTH);
? ? ? ? ? ? ? ? frame.setBounds(500, 300, 400, 300);
? ? ? ? ? ? ? ? frame.addWindowListener(new WindowAdapter() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void windowClosing(WindowEvent e) {
? ? ? ? ? ? ? ? ? ? ? ? e.getWindow().dispose();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? frame.setVisible(true);
? ? ? ? ? ? }
? ? ? ? }
? ? }

? ? MyContacts() {
? ? ? ? myPaneone = new MyPanelone("communication", 250, 60, 60);
? ? ? ? //myPaneone.setSize(1000, 150);
? ? ? ? this.add(myPaneone);
? ? ? ? for(int i=0;i<7;i++) {
? ? ? ? ? ? jPanels[i] = new JPanel();
? ? ? ? }

? ? ? ? jLabels[0] = new JLabel("姓名");
? ? ? ? jLabels[1] = new JLabel("郵政編碼");
? ? ? ? jLabels[2] = new JLabel("通信地址");
? ? ? ? jLabels[3] = new JLabel("電話");
? ? ? ? jLabels[4] = new JLabel("手機");
? ? ? ? jLabels[5] = new JLabel("電子郵件");

? ? ? ? jButtons[0] = new JButton("添加");
? ? ? ? jButtons[1] = new JButton("查找");
? ? ? ? jButtons[2] = new JButton("清空");
? ? ? ? jButtons[3] = new JButton("退出");

? ? ? ? for(int i=0;i<6;i++) {
? ? ? ? ? ? jTextFields[i] = new JTextField(50);
? ? ? ? }

? ? ? ? //設置布局管理
? ? ? ? this.setLayout(new GridLayout(8, 1));

? ? ? ? //加入各個組件
? ? ? ? for(int i=0;i<6;i++) {
? ? ? ? ? ? jPanels[i].add(jLabels[i]);
? ? ? ? ? ? jPanels[i].add(jTextFields[i]);
? ? ? ? ? ? this.add(jPanels[i]);
? ? ? ? }
? ? ? ? for(int i=0;i<4;i++) {
? ? ? ? ? ? jButtons[i].addActionListener(new MyActionListener());
? ? ? ? ? ? jPanels[6].add(jButtons[i]);
? ? ? ? }
? ? ? ? this.add(jPanels[6]);
? ? }

? ? public static void main(String[] args) {
? ? ? ? JFrame f = new MyContacts();
? ? ? ? f.setTitle(f.getClass().getSimpleName());
? ? ? ? f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
? ? ? ? f.setBounds(400, 200, 1000, 600);
? ? ? ? f.setVisible(true);
? ? }
}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • MySQL安裝與idea的連接實現(xiàn)

    MySQL安裝與idea的連接實現(xiàn)

    本文主要介紹了MySQL安裝與idea的連接實現(xiàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • Java Process詳解及實例

    Java Process詳解及實例

    這篇文章主要介紹了Java Process詳解及實例的相關(guān)資料,需要的朋友可以參考下
    2017-07-07
  • IDEA配置tomcat服務器全過程

    IDEA配置tomcat服務器全過程

    這篇文章主要介紹了IDEA配置tomcat服務器全過程,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • Java  隊列 Queue 用法實例詳解

    Java 隊列 Queue 用法實例詳解

    本文實例講述了Java內(nèi)置隊列類Queue用法,分享給大家供大家參考
    2017-04-04
  • Java HttpClient技術(shù)詳解

    Java HttpClient技術(shù)詳解

    Http協(xié)議的重要性相信不用我多說了,HttpClient相比傳統(tǒng)JDK自帶的URLConnection,增加了易用和靈活性(具體區(qū)別,日后我們再討論),它不僅是客戶端發(fā)送Http請求變得容易,而且也方便了開發(fā)人員測試接口(基于Http協(xié)議的),即提高了開發(fā)的效率,也方便提高代碼的健壯性
    2021-10-10
  • spring mvc @PathVariable綁定URI模板變量值方式

    spring mvc @PathVariable綁定URI模板變量值方式

    這篇文章主要介紹了spring mvc @PathVariable綁定URI模板變量值方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • 完美解決idea光標變成了insert光標狀態(tài)的問題

    完美解決idea光標變成了insert光標狀態(tài)的問題

    這篇文章主要介紹了完美解決idea光標變成了insert光標狀態(tài)的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • JAVA 注解詳解及簡單實例

    JAVA 注解詳解及簡單實例

    這篇文章主要介紹了JAVA 注解詳解及簡單實例的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • Mybatis如何實現(xiàn)延遲加載及緩存

    Mybatis如何實現(xiàn)延遲加載及緩存

    這篇文章主要介紹了Mybatis如何實現(xiàn)延遲加載及緩存,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-10-10
  • JavaWeb JDBC + MySql 通訊錄實現(xiàn)簡單的增刪改查功能案例詳解

    JavaWeb JDBC + MySql 通訊錄實現(xiàn)簡單的增刪改查功能案例詳解

    這篇文章主要介紹了JavaWeb JDBC + MySql 通訊錄實現(xiàn)簡單的增刪改查功能,結(jié)合具體案例形式詳細分析了JavaWeb JDBC + MySql數(shù)據(jù)庫連接、增刪改查等相關(guān)操作技巧與注意事項,需要的朋友可以參考下
    2019-08-08

最新評論