2019Java for Week Course summary

About notepad codes have been written last week, and this time his stick over, as follows:

notebook

package jishiben;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.*;
import javax.swing.undo.UndoManager;

public class Jishiben implements ActionListener{

    JFrame frame;
    JTextArea text;
    JScrollPane sp;
    JMenuItem newItem,windomItem,openItem,saveItem,othersaveItem,pagesetupItem,printItem,closeItem;
    JMenuBar bar;
    JMenu fileMenu,editMenu,formatMenu,checkMenu,helpMenu;
    JFileChooser choose;
    File file;
    JTable table;
    JMenuItem blankItem;
    UndoManager undoManager;
    
    public void Wenjian() {
        
        frame=new JFrame("记事本");
        text=new JTextArea();
        sp=new JScrollPane(text);
        bar=new JMenuBar();
        
        fileMenu=new JMenu("文件(F)");
        editMenu=new JMenu("编辑(E)");
        formatMenu=new JMenu("格式(O)");
        checkMenu=new JMenu("查看(V)");
        helpMenu=new JMenu("帮助(H)");
        
        //关于文件的选项
        newItem=new JMenuItem("新建(N)");
        windomItem=new JMenuItem("新窗口(W)");
        openItem=new JMenuItem("打开(O)");
        saveItem=new JMenuItem("保存(S)");
        othersaveItem=new JMenuItem("另存为(A)");
        pagesetupItem=new JMenuItem("页面设置(U)");
        printItem=new JMenuItem("打印(P)");
        closeItem=new JMenuItem("退出(X)");       
        
        //关于文件助记符的设置
        newItem.setMnemonic('N');
        windomItem.setMnemonic('W');
        openItem.setMnemonic('O');
        saveItem.setMnemonic('S');
        othersaveItem.setMnemonic('A');
        pagesetupItem.setMnemonic('U');
        printItem.setMnemonic('P');
        closeItem.setMnemonic('X');
        
        //关于文件快捷键的设置
        newItem.setAccelerator(KeyStroke.getKeyStroke('N',KeyEvent.CTRL_DOWN_MASK));
        windomItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_DOWN_MASK|InputEvent.SHIFT_DOWN_MASK));
        openItem.setAccelerator(KeyStroke.getKeyStroke('O',KeyEvent.CTRL_DOWN_MASK));
        saveItem.setAccelerator(KeyStroke.getKeyStroke('S',KeyEvent.CTRL_DOWN_MASK));
        othersaveItem.setAccelerator(KeyStroke.getKeyStroke('S',KeyEvent.CTRL_DOWN_MASK|KeyEvent.SHIFT_DOWN_MASK));
        printItem.setAccelerator(KeyStroke.getKeyStroke('P',KeyEvent.CTRL_DOWN_MASK));
        
        
        //关于文件菜单的动作监听
        newItem.addActionListener(this);
        windomItem.addActionListener(this);
        openItem.addActionListener(this);
        saveItem.addActionListener(this);
        othersaveItem.addActionListener(this);
        pagesetupItem.addActionListener(this);
        printItem.addActionListener(this);
        closeItem.addActionListener(this);
        
        
        //向菜单栏添加
        bar.add(fileMenu);
        bar.add(editMenu);
        bar.add(formatMenu);
        bar.add(checkMenu);
        bar.add(helpMenu);
        
        
        frame.add(sp);
        frame.setJMenuBar(bar);
        frame.setSize(400,300);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        
        
    }
    
    public static void main(String[] args) {
        Jishiben shi=new Jishiben();
        shi.Wenjian();

    }
    public void actionPerformed(ActionEvent e){
        //关于文件下面选项的监听
        if(e.getSource()==newItem) {
            text.setText("");
        }else if(e.getSource()==windomItem) {
            Jishiben ben =new Jishiben();
            ben.Wenjian();
        }else if(e.getSource()==openItem) {
            choose=new JFileChooser();
            choose.showOpenDialog(null);
            file=choose.getSelectedFile();
            try {
                FileInputStream input=new FileInputStream(file);
                byte[] b=new byte[input.available()];
                input.read(b);
                text.append(new String(b));
                input.close();
                
            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }else if(e.getSource()==othersaveItem) {
            choose=new JFileChooser();
            choose.showOpenDialog(null);
            file=choose.getSelectedFile();
            try {
                if(!file.exists()) {
                  file.createNewFile();
                }
                FileOutputStream output=new FileOutputStream(file);
                byte[] b=text.getText().getBytes();
                output.write(b);
                output.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }else if(e.getSource()==saveItem) {
            choose=new JFileChooser();
            choose.showOpenDialog(null);
            file=choose.getSelectedFile();
            try {
                FileOutputStream output=new FileOutputStream(file);
                byte[] b=text.getText().getBytes();
                output.write(b);
                output.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }else if(e.getSource()==pagesetupItem) {
            
        }else if(e.getSource()==printItem) {
            
        }else if(e.getSource()==closeItem) {
            System.exit(1);
        }
    }

}

Run shot

Also this week, also learned about the database of knowledge
1. For an understanding of jdbc
main classes and interfaces common operations:
java.sql.DriverManager for managing jdbc driver
Java.sql.Connection to establish a connection with a specific database
Java. sql.Statement a statement object used to perform static sql statements and obtain consequences statement execution
Java.sql.PreparedStatement create a sql statement object can be compiled, the object can be run multiple times, in order to improve the efficiency, it is statement of sub-interfaces
result set Java.sql.ResultSet represent sql statement used to create the search results, the user complete access to the database through the result set
2.MySQL installation and configuration
commonly used commands 3.MySQL of
1) mysql -u user name -p password to connect mysql database
2) create a database: cREATE dATABASE database name;
3) delete the database: DROP dATABASE database name;
4) the name of the uSE database; use the database
5) create a database table

CREATE TABLE 表名称(
字段名称1    字段类型 [DEFAULT 默认值][约束],
字段名称2    字段类型 [DEFAULT 默认值][约束],
...,
字段名称n    字段类型 [DEFAULT 默认值][约束]
);

6) Remove the database table DROP TABLE table name;
7) See Table DESE name table structure;
8) view database information
All database: SHOW DATABASES;
see a full database tables: TABLES SHOW;
4. Configuration database driver mysql

package textshujuku;

public class Text {
        public static final String DBDRIVER="ora.gjt.mm.mysql.Driver";
    public static void main(String[] args) {
        try {
            Class.forName(DBDRIVER);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

}

5. Close the database connection and
use the database and the connection DriverManager class Connection interface
connection address

jdbc:mysql://IP地址:端口号/数据库名称

Guess you like

Origin www.cnblogs.com/H-Alice/p/11951811.html