java学习笔记-第五课

1. 图形用户界面 (GUI) 编程
1.1 Swing 简介
  • Swing:Java 的图形用户界面工具包,提供了一套丰富的组件用于创建桌面应用程序。
1.2 创建基本窗口
  • 示例
    import javax.swing.JFrame;
    
    public class Main {
        public static void main(String[] args) {
            JFrame frame = new JFrame("My First GUI");
            frame.setSize(400, 300);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
    }
    

1.3 常用组件
  • 按钮 (JButton)

    import javax.swing.JButton;
    import javax.swing.JFrame;
    
    public class Main {
        public static void main(String[] args) {
            JFrame frame = new JFrame("Button Example");
            JButton button = new JButton("Click Me");
            button.setBounds(50, 100, 95, 30);
            frame.add(button);
            frame.setSize(400, 400);
            frame.setLayout(null);
            frame.setVisible(true);
        }
    }
    
  • 文本框 (JTextField)

    import javax.swing.JTextField;
    import javax.swing.JFrame;
    
    public class Main {
        public static void main(String[] args) {
            JFrame frame = new JFrame("TextField Example");
            JTextField textField = new JTextField();
            textField.setBounds(50, 50, 150, 20);
            frame.add(textField);
            frame.setSize(400, 400);
            frame.setLayout(null);
            frame.setVisible(true);
        }
    }
    

  • 标签 (JLabel)

    import javax.swing.JLabel;
    import javax.swing.JFrame;
    
    public class Main {
        public static void main(String[] args) {
            JFrame frame = new JFrame("Label Example");
            JLabel label = new JLabel("Hello, World!");
            label.setBounds(50, 50, 100, 30);
            frame.add(label);
            frame.setSize(400, 400);
            frame.setLayout(null);
            frame.setVisible(true);
        }
    }
    

1.4 事件处理
  • 按钮点击事件
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    
    public class Main {
        public static void main(String[] args) {
            JFrame frame = new JFrame("Button Example");
            JButton button = new JButton("Click Me");
            button.setBounds(50, 100, 95, 30);
            button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.out.println("Button clicked");
                }
            });
            frame.add(button);
            frame.setSize(400, 400);
            frame.setLayout(null);
            frame.setVisible(true);
        }
    }
    

2. 高级网络编程
2.1 URL 处理
  • 读取网页内容
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.URL;
    
    public class Main {
        public static void main(String[] args) {
            try {
                URL url = new URL("http://www.example.com");
                BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
                String line;
                while ((line = reader.readLine()) != null) {
                    System.out.println(line);
                }
                reader.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    

2.2 HTTP 请求
  • 发送 GET 请求

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    public class Main {
        public static void main(String[] args) {
            try {
                URL url = new URL("http://www.example.com");
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String line;
                StringBuilder response = new StringBuilder();
                while ((line = reader.readLine()) != null) {
                    response.append(line);
                }
                reader.close();
                System.out.println(response.toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
  • 发送 POST 请求

    import java.io.OutputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    public class Main {
        public static void main(String[] args) {
            try {
                URL url = new URL("http://www.example.com");
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("POST");
                connection.setDoOutput(true);
                String postData = "param1=value1&param2=value2";
                OutputStream os = connection.getOutputStream();
                os.write(postData.getBytes());
                os.flush();
                os.close();
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String line;
                StringBuilder response = new StringBuilder();
                while ((line = reader.readLine()) != null) {
                    response.append(line);
                }
                reader.close();
                System.out.println(response.toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    

3. Swing 应用程序示例
  • 登录界面
    import javax.swing.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class Login {
        public static void main(String[] args) {
            JFrame frame = new JFrame("Login");
            JLabel userLabel = new JLabel("User:");
            userLabel.setBounds(50, 50, 100, 30);
            JLabel passLabel = new JLabel("Password:");
            passLabel.setBounds(50, 100, 100, 30);
            JTextField userField = new JTextField();
            userField.setBounds(150, 50, 150, 30);
            JPasswordField passField = new JPasswordField();
            passField.setBounds(150, 100, 150, 30);
            JButton loginButton = new JButton("Login");
            loginButton.setBounds(150, 150, 95, 30);
            loginButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    String user = userField.getText();
                    String pass = new String(passField.getPassword());
                    if (user.equals("admin") && pass.equals("password")) {
                        JOptionPane.showMessageDialog(frame, "Login Successful");
                    } else {
                        JOptionPane.showMessageDialog(frame, "Login Failed");
                    }
                }
            });
            frame.add(userLabel);
            frame.add(passLabel);
            frame.add(userField);
            frame.add(passField);
            frame.add(loginButton);
            frame.setSize(400, 400);
            frame.setLayout(null);
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    }
    

小结

  • 本课学习了 Java 的图形用户界面编程,包括 Swing 框架的使用和事件处理。
  • 探讨了高级网络编程技术,包括 URL 处理和 HTTP 请求。
  • 通过一个完整的 Swing 应用程序示例,巩固了 GUI 编程的知识。
  • 8
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值