java17

GUI

    Graph user interface

    javax.Swing:在AWT的基础上,建立了一套图形界面系统,其中提供了更多的组件,完全由java实现,增强了移植性,属于轻量级控件。

    控件

  • Button              //按钮
  • Checkbox        //复选框
  • RadioBox      //单选框
  • Text                  //文本框
  • Group         //成组框
  • Tree                 //树形视图
  • Select              //下拉框
  • TextArea             //文本域,编辑多行
  • Menu                  //菜单
  • MenuItem           //菜单项
  • MenuBar     //菜单条
  • ToolBar
  • Progressbar           //进度条
  • Window                  //窗口
  • JFrame                   //窗口
  • Label                      //标签
  • Tab                         //选项卡
  • Dialog                     //对话框
  • LayoutManager      //控制控件在容器中的排列方式

MyEditor Demo

// MainFrame.java

  1 import java.awt.FileDialog;
  2 import java.awt.Font;
  3 import java.awt.event.ActionEvent;
  4 import java.awt.event.ActionListener;
  5 import java.awt.event.WindowEvent;
  6 import java.awt.event.WindowListener;
  7 import java.io.File;
  8 import java.io.FileReader;
  9 import java.io.FileWriter;
 10 import javax.swing.JButton;
 11 import javax.swing.JFrame;
 12 import javax.swing.JMenu;
 13 import javax.swing.JMenuBar;
 14 import javax.swing.JMenuItem;
 15 import javax.swing.JScrollPane;
 16 import javax.swing.JTextArea;
 17 
 18 public class MainFrame extends JFrame implements ActionListener{
 19 
 20     /**
 21      * 
 22      */
 23     private static final long serialVersionUID = 4253714983535650834L;
 24     private JButton btnSave;
 25     private JButton btnCancel;
 26     private JTextArea txtArea;
 27     private JMenuItem miOpen;
 28     private JMenuItem miExit;
 29     
 30     public MainFrame() {
 31         initFrame();
 32         this.setVisible(true);
 33     }
 34 
 35     private void initFrame() {
 36         // 标题
 37         this.setTitle("MainFrame");
 38         // 边界
 39         this.setBounds(100, 100, 800, 700);
 40         // 绝对布局
 41         this.setLayout(null);
 42         Font font = new Font("宋体", Font.BOLD, 16);
 43         
 44         
 45         
 46         
 47         txtArea = new JTextArea();
 48         //txtArea.setBounds(0, 0, 800, 500);
 49         //txtArea.setFont(font);
 50         //txtArea.setBackground(Color.gray);
 51         //txtArea.setForeground(Color.red);
 52         // 将文本域添加到滚动面板
 53         // 滚动面板
 54         JScrollPane scrollPane = new JScrollPane(txtArea);
 55         scrollPane.setBounds(0, 0, 850, 500);
 56         this.add(scrollPane);
 57         
 58         btnSave = new JButton("保存");
 59         btnSave.setBounds(500, 510, 100, 50);
 60         btnSave.setFont(font);
 61         btnSave.addActionListener(this);
 62         this.add(btnSave);
 63         
 64         btnCancel = new JButton("取消");
 65         btnCancel.setBounds(610, 510, 100, 50);
 66         btnCancel.setFont(font);
 67         btnCancel.addActionListener(this);
 68         this.add(btnCancel);
 69         
 70         // 添加窗口事件处理程序,使用适配器
 71         this.addWindowListener(new WindowListener() {
 72             
 73             @Override
 74             public void windowOpened(WindowEvent e) {
 75                 
 76             }
 77             
 78             @Override
 79             public void windowIconified(WindowEvent e) {
 80                 // TODO Auto-generated method stub
 81                 
 82             }
 83             
 84             @Override
 85             public void windowDeiconified(WindowEvent e) {
 86                 // TODO Auto-generated method stub
 87                 
 88             }
 89             
 90             @Override
 91             public void windowDeactivated(WindowEvent e) {
 92                 // TODO Auto-generated method stub
 93                 
 94             }
 95             
 96             @Override
 97             public void windowClosing(WindowEvent e) {
 98                 System.out.println("关闭");
 99                 System.exit(-1);
100                 
101             }
102             
103             @Override
104             public void windowClosed(WindowEvent e) {
105                 // TODO Auto-generated method stub
106                 
107             }
108             
109             @Override
110             public void windowActivated(WindowEvent e) {
111                 // TODO Auto-generated method stub
112                 
113             }
114         });
115         
116         // 添加菜单栏
117         JMenuBar menuBar = new JMenuBar();
118         
119         JMenu menu = new JMenu("文件");
120         miOpen = new JMenuItem("打开");
121         // 添加监听器
122         miOpen.addActionListener(this);
123         // 添加菜单项
124         menu.add(miOpen);
125 
126         miExit = new JMenuItem("退出");
127         menu.add(miExit);
128         miExit.addActionListener(this);
129         // 添加菜单
130         menuBar.add(menu);
131         this.setJMenuBar(menuBar);
132     }
133 
134     /**
135      * 动作处理程序
136      */
137     @Override
138     public void actionPerformed(ActionEvent e) {
139         // 获得事件源
140         Object source = e.getSource();
141         if (source == btnSave) {
142             String str = txtArea.getText();
143             try {
144                 // 打开保存对话框,定位保存文件位置
145                 FileDialog d = new FileDialog(this, "保存", FileDialog.SAVE);
146                 d.setVisible(true);
147                 File f = new File(d.getDirectory() , d.getFile());
148                 System.out.println(d.getDirectory()+d.getFile());
149                 FileWriter writer = new FileWriter(f);
150                 writer.write(str);
151                 writer.close();
152             } catch (Exception e1) {
153                 e1.printStackTrace();
154             }
155             
156         } else if (source == btnCancel) {
157             this.dispose();
158         } else if (source == miOpen) {
159             FileDialog fileDialog = new FileDialog(this, "打开", FileDialog.LOAD);
160             fileDialog.setVisible(true);
161             String dir = fileDialog.getDirectory();
162             String f = fileDialog.getFile();
163             if (dir != null && f != null) {
164                 try {
165                     txtArea.setText(null);
166                     FileReader reader = new FileReader(new File(dir, f));
167                     char[] cbuf = new char[1024];
168                     int len = -1;
169                     while((len = reader.read(cbuf)) != -1) {
170                         txtArea.setText(txtArea.getText() + new String(cbuf, 0, len));
171                     }
172                     reader.close();
173                 } catch (Exception e1) {
174                     e1.printStackTrace();
175                 }
176             }
177         } else if (source == miExit) {
178             System.exit(-1);
179         }
180     }
181 }

// MyEditor.java

1 public class MyEditor {
2 
3     public static void main(String[] args) {
4         MainFrame frame = new MainFrame();
5     }
6 
7 }

 Java网络通信


Socket

  1. 套接字
  2. Stream

C/S

  1. Client / Server
  2. 客户端 / Server(DB Server)
  3. Rich Client
    1. UI

B/S

  1. Browser / Server
  2. 浏览器 / 服务器(web server)
  3. 瘦客户端程序

TCP(transfer control protocol)

面向连接,基于流

UDP(user datagrame protocal)

无连接,无顺序,不安全

TCP三次握手

双方确认的过程

// syn:同步信号

A发送信号x给B

B接收A的同步信号x, x+1发送给A, 同时发送y给A

A接收B发送的x+1确认, 并将y+1再发给B

URL

统一资源定位符

 1 import java.io.FileOutputStream;
 2 import java.io.InputStream;
 3 import java.io.OutputStream;
 4 import java.net.HttpURLConnection;
 5 import java.net.URL;
 6 
 7 public class HttpIODemo2 {
 8 
 9     public static void main(String[] args) throws Exception {
10         String urlStr = "http://localhost:8000/1.mp4";
11         URL url = new URL(urlStr);
12         HttpURLConnection connection = (HttpURLConnection) url.openConnection();
13         int length = connection.getContentLength();
14         InputStream is = connection.getInputStream();
15         FileOutputStream fos = new FileOutputStream("h:/1.mp4");
16         byte[] buffer = new byte[1024];
17         int len = -1;
18         while ((len = is.read(buffer)) != -1) {
19             fos.write(buffer, 0, len);
20         }
21         fos.close();
22     }
23 
24 }
原文地址:https://www.cnblogs.com/8386blogs/p/7536691.html