最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java實(shí)現(xiàn)簡(jiǎn)易界面通訊錄

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

前言

這個(gè)也是Java實(shí)驗(yàn)課程的一個(gè)作業(yè),和Java實(shí)現(xiàn)簡(jiǎn)單的圖形界面計(jì)算器一起做的,因?yàn)橐郧皼](méi)有做過(guò)GUI編程,所以做的非常簡(jiǎn)陋,還有很多BUG,但是感覺(jué)當(dāng)個(gè)作業(yè)也夠了。

程序功能和截圖

這里的添加是直接添加到文件中,為什么不用數(shù)據(jù)庫(kù)呢?因?yàn)槲覀兝蠋煾揪蜎](méi)教,所以也不能用.。

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

大致的功能就是上面兩個(gè)。

代碼

一、文件讀寫工具

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,用于實(shí)現(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)容寫成一個(gè)字符串
? ? ? ? ? ? ? ? ? ? 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("請(qǐng)把所有內(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("手機(jī)");
? ? ? ? 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);
? ? ? ? }

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

? ? ? ? //加入各個(gè)組件
? ? ? ? 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);
? ? }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spring Cloud 部署時(shí)使用 Kubernetes 作為注冊(cè)中心和配置中心的方法

    Spring Cloud 部署時(shí)使用 Kubernetes 作為注冊(cè)中心和配置中

    Spring Cloud Kubernetes提供了使用Kubernete本地服務(wù)的Spring Cloud通用接口實(shí)現(xiàn),這篇文章主要介紹了Spring Cloud 部署時(shí)如何使用 Kubernetes 作為注冊(cè)中心和配置中心,需要的朋友可以參考下
    2024-05-05
  • Spring Cloud Alibaba Nacos Config配置中心實(shí)現(xiàn)

    Spring Cloud Alibaba Nacos Config配置中心實(shí)現(xiàn)

    這篇文章主要介紹了Spring Cloud Alibaba Nacos Config配置中心實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04
  • Spring Cloud服務(wù)入口Gateway的介紹和使用問(wèn)題小結(jié)

    Spring Cloud服務(wù)入口Gateway的介紹和使用問(wèn)題小結(jié)

    Spring Cloud Gateway是Spring Cloud的?個(gè)全新的API?關(guān)項(xiàng)?, 基于Spring + SpringBoot等技術(shù)開(kāi)發(fā), ?的是為了替換掉Zuul,這篇文章主要介紹了Spring Cloud服務(wù)入口Gateway的介紹和使用問(wèn)題小結(jié),需要的朋友可以參考下
    2025-03-03
  • Idea使用插件實(shí)現(xiàn)逆向工程搭建SpringBoot項(xiàng)目的圖文教程

    Idea使用插件實(shí)現(xiàn)逆向工程搭建SpringBoot項(xiàng)目的圖文教程

    這篇文章主要介紹了Idea使用插件實(shí)現(xiàn)逆向工程搭建SpringBoot項(xiàng)目,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-06-06
  • SpringBoot-Admin實(shí)現(xiàn)微服務(wù)監(jiān)控+健康檢查+釘釘告警

    SpringBoot-Admin實(shí)現(xiàn)微服務(wù)監(jiān)控+健康檢查+釘釘告警

    本文主要介紹了SpringBoot-Admin實(shí)現(xiàn)微服務(wù)監(jiān)控+健康檢查+釘釘告警,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • Java反射機(jī)制如何解決數(shù)據(jù)傳值為空的問(wèn)題

    Java反射機(jī)制如何解決數(shù)據(jù)傳值為空的問(wèn)題

    這篇文章主要介紹了Java反射機(jī)制如何解決數(shù)據(jù)傳值為空的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • 使用Spring Security和JWT實(shí)現(xiàn)安全認(rèn)證機(jī)制

    使用Spring Security和JWT實(shí)現(xiàn)安全認(rèn)證機(jī)制

    在現(xiàn)代 Web 應(yīng)用中,安全認(rèn)證和授權(quán)是保障數(shù)據(jù)安全和用戶隱私的核心機(jī)制,Spring Security 是 Spring 框架下專為安全設(shè)計(jì)的模塊,具有高度的可配置性和擴(kuò)展性,而 JWT則是當(dāng)前流行的認(rèn)證解決方案,所以本文介紹了如何使用Spring Security和JWT實(shí)現(xiàn)安全認(rèn)證機(jī)制
    2024-11-11
  • Java中的Kotlin?內(nèi)部類原理

    Java中的Kotlin?內(nèi)部類原理

    這篇文章主要介紹了Java中的Kotlin?內(nèi)部類原理,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下
    2022-06-06
  • 微服務(wù)通過(guò)Feign調(diào)用進(jìn)行密碼安全認(rèn)證操作

    微服務(wù)通過(guò)Feign調(diào)用進(jìn)行密碼安全認(rèn)證操作

    這篇文章主要介紹了微服務(wù)通過(guò)Feign調(diào)用進(jìn)行密碼安全認(rèn)證操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • 淺談hibernate之映射文件VS映射注解

    淺談hibernate之映射文件VS映射注解

    下面小編就為大家?guī)?lái)一篇淺談hibernate之映射文件VS映射注解。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-07-07

最新評(píng)論

黄大仙区| 吉安县| 仁寿县| 铜川市| 山西省| 和林格尔县| 彰武县| 临城县| 西华县| 库车县| 平舆县| 长白| 宁乡县| 乌拉特中旗| 东平县| 乐都县| 咸丰县| 山丹县| 阿图什市| 盐源县| 日土县| 铜鼓县| 武穴市| 京山县| 齐河县| 明光市| 杭锦旗| 凌源市| 虎林市| 河北省| 榕江县| 肥乡县| 恩施市| 江都市| 天气| 大姚县| 南丹县| 赣榆县| 斗六市| 新田县| 黄龙县|