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¶m2=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 编程的知识。