From 0f6ab9f93c90733c7e3adedadf8edac211818ddc Mon Sep 17 00:00:00 2001 From: dev_xulongjin Date: Mon, 19 May 2025 11:26:26 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E5=AE=9E=E9=AA=8C5):=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E9=93=B6=E8=A1=8C=E7=B3=BB=E7=BB=9F=E5=92=8C=E5=9B=BE=E4=B9=A6?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E7=B3=BB=E7=BB=9F=E7=95=8C=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 BankManagerSystem 和 BookManagerSystem 类 - 使用 JFormDesigner 设计并生成了相应的界面 - 添加了基本的事件处理方法 - 优化了界面布局和组件样式 --- .../BankSystem/BankManagerSystem.java | 255 +++++++++++++++ .../BankSystem/BankManagerSystem.jfd | 268 ++++++++++++++++ .../BookSystem/BookManagerSystem.java | 298 ++++++++++++++++++ .../BookSystem/BookManagerSystem.jfd | 203 ++++++++++++ .../vscoder/experiment_5/BookSystem/task.java | 7 + .../{ => Sport_Register}/SportRegister.java | 85 +++-- .../{ => Sport_Register}/SportRegister.jfd | 26 +- .../{ => Sport_Register}/task.java | 2 +- spark-lesson/pom.xml | 7 + .../main/scala/date_20250513/MyConsumer.java | 47 +++ .../main/scala/date_20250513/MyProducer.java | 48 +++ 11 files changed, 1210 insertions(+), 36 deletions(-) create mode 100644 java-lesson/src/main/java/cn/vscoder/experiment_5/BankSystem/BankManagerSystem.java create mode 100644 java-lesson/src/main/java/cn/vscoder/experiment_5/BankSystem/BankManagerSystem.jfd create mode 100644 java-lesson/src/main/java/cn/vscoder/experiment_5/BookSystem/BookManagerSystem.java create mode 100644 java-lesson/src/main/java/cn/vscoder/experiment_5/BookSystem/BookManagerSystem.jfd create mode 100644 java-lesson/src/main/java/cn/vscoder/experiment_5/BookSystem/task.java rename java-lesson/src/main/java/cn/vscoder/experiment_5/{ => Sport_Register}/SportRegister.java (79%) rename java-lesson/src/main/java/cn/vscoder/experiment_5/{ => Sport_Register}/SportRegister.jfd (95%) rename java-lesson/src/main/java/cn/vscoder/experiment_5/{ => Sport_Register}/task.java (71%) create mode 100644 spark-lesson/src/main/scala/date_20250513/MyConsumer.java create mode 100644 spark-lesson/src/main/scala/date_20250513/MyProducer.java diff --git a/java-lesson/src/main/java/cn/vscoder/experiment_5/BankSystem/BankManagerSystem.java b/java-lesson/src/main/java/cn/vscoder/experiment_5/BankSystem/BankManagerSystem.java new file mode 100644 index 0000000..c14d596 --- /dev/null +++ b/java-lesson/src/main/java/cn/vscoder/experiment_5/BankSystem/BankManagerSystem.java @@ -0,0 +1,255 @@ +/* + * Created by JFormDesigner on Wed May 14 09:34:54 CST 2025 + */ + +package cn.vscoder.experiment_5.BankSystem; + +import java.awt.*; +import javax.swing.*; +import javax.swing.table.*; + +/** + * @author xulongjin + */ +public class BankManagerSystem extends JFrame { + public BankManagerSystem() { + initComponents(); + } + + private void initComponents() { + // JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents @formatter:off + panel1 = new JPanel(); + label1 = new JLabel(); + label2 = new JLabel(); + label3 = new JLabel(); + label4 = new JLabel(); + label5 = new JLabel(); + label6 = new JLabel(); + textField1 = new JTextField(); + textField2 = new JTextField(); + textField3 = new JTextField(); + textField4 = new JTextField(); + textField5 = new JTextField(); + textField6 = new JTextField(); + panel2 = new JPanel(); + label7 = new JLabel(); + radioButton1 = new JRadioButton(); + radioButton2 = new JRadioButton(); + radioButton3 = new JRadioButton(); + checkBox1 = new JCheckBox(); + button1 = new JButton(); + button2 = new JButton(); + button3 = new JButton(); + button4 = new JButton(); + button5 = new JButton(); + button6 = new JButton(); + button7 = new JButton(); + button8 = new JButton(); + scrollPane1 = new JScrollPane(); + table1 = new JTable(); + + //======== this ======== + var contentPane = getContentPane(); + contentPane.setLayout(new GridLayout(3, 0)); + + //======== panel1 ======== + { + panel1.setLayout(null); + + //---- label1 ---- + label1.setText("\u94f6\u884c\u5361\u53f7\uff1a"); + panel1.add(label1); + label1.setBounds(25, 25, 70, 40); + + //---- label2 ---- + label2.setText("\u7528\u6237\u59d3\u540d\uff1a"); + panel1.add(label2); + label2.setBounds(25, 79, 75, 40); + + //---- label3 ---- + label3.setText("\u4f59 \u989d\uff1a"); + panel1.add(label3); + label3.setBounds(25, 129, 75, 40); + + //---- label4 ---- + label4.setText("\u5b58/\u53d6\u6b3e \uff1a"); + panel1.add(label4); + label4.setBounds(355, 125, 75, 40); + + //---- label5 ---- + label5.setText("\u5bc6 \u7801\uff1a"); + panel1.add(label5); + label5.setBounds(355, 75, 75, 40); + + //---- label6 ---- + label6.setText("\u8eab\u4efd\u8bc1\u53f7\uff1a"); + panel1.add(label6); + label6.setBounds(355, 20, 75, 40); + panel1.add(textField1); + textField1.setBounds(90, 25, 205, 39); + panel1.add(textField2); + textField2.setBounds(90, 80, 205, 39); + panel1.add(textField3); + textField3.setBounds(90, 130, 205, 39); + panel1.add(textField4); + textField4.setBounds(420, 75, 205, 39); + panel1.add(textField5); + textField5.setBounds(420, 20, 205, 39); + panel1.add(textField6); + textField6.setBounds(420, 125, 205, 39); + + { + // compute preferred size + Dimension preferredSize = new Dimension(); + for(int i = 0; i < panel1.getComponentCount(); i++) { + Rectangle bounds = panel1.getComponent(i).getBounds(); + preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width); + preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height); + } + Insets insets = panel1.getInsets(); + preferredSize.width += insets.right; + preferredSize.height += insets.bottom; + panel1.setMinimumSize(preferredSize); + panel1.setPreferredSize(preferredSize); + } + } + contentPane.add(panel1); + + //======== panel2 ======== + { + panel2.setLayout(null); + + //---- label7 ---- + label7.setText("\u5b58\u6b3e\u671f\u9650\uff1a"); + panel2.add(label7); + label7.setBounds(50, 10, 75, 35); + + //---- radioButton1 ---- + radioButton1.setText("1\u5e74"); + panel2.add(radioButton1); + radioButton1.setBounds(new Rectangle(new Point(120, 15), radioButton1.getPreferredSize())); + + //---- radioButton2 ---- + radioButton2.setText("3\u5e74"); + panel2.add(radioButton2); + radioButton2.setBounds(285, 15, 60, 24); + + //---- radioButton3 ---- + radioButton3.setText("\u6d3b\u671f"); + panel2.add(radioButton3); + radioButton3.setBounds(440, 15, 65, 24); + + //---- checkBox1 ---- + checkBox1.setText("\u529e\u65b0\u5361\u65f6\u5b58\u6b3e"); + panel2.add(checkBox1); + checkBox1.setBounds(new Rectangle(new Point(85, 50), checkBox1.getPreferredSize())); + + //---- button1 ---- + button1.setText("\u529e\u65b0\u5361"); + panel2.add(button1); + button1.setBounds(105, 85, 90, 45); + + //---- button2 ---- + button2.setText("\u5f00\u7a7a\u5361"); + panel2.add(button2); + button2.setBounds(105, 135, 90, 45); + + //---- button3 ---- + button3.setText("\u5b58\u6b3e"); + panel2.add(button3); + button3.setBounds(210, 85, 90, 45); + + //---- button4 ---- + button4.setText("\u67e5\u8be2"); + panel2.add(button4); + button4.setBounds(210, 135, 90, 45); + + //---- button5 ---- + button5.setText("\u53d6\u6b3e"); + panel2.add(button5); + button5.setBounds(320, 85, 90, 45); + + //---- button6 ---- + button6.setText("\u5220\u9664"); + panel2.add(button6); + button6.setBounds(320, 135, 90, 45); + + //---- button7 ---- + button7.setText("\u6539\u5bc6\u7801"); + panel2.add(button7); + button7.setBounds(430, 85, 90, 45); + + //---- button8 ---- + button8.setText("\u6e05\u7a7a"); + panel2.add(button8); + button8.setBounds(430, 135, 90, 45); + + { + // compute preferred size + Dimension preferredSize = new Dimension(); + for(int i = 0; i < panel2.getComponentCount(); i++) { + Rectangle bounds = panel2.getComponent(i).getBounds(); + preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width); + preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height); + } + Insets insets = panel2.getInsets(); + preferredSize.width += insets.right; + preferredSize.height += insets.bottom; + panel2.setMinimumSize(preferredSize); + panel2.setPreferredSize(preferredSize); + } + } + contentPane.add(panel2); + + //======== scrollPane1 ======== + { + + //---- table1 ---- + table1.setModel(new DefaultTableModel( + new Object[][] { + }, + new String[] { + "\u5361\u53f7", "\u59d3\u540d", "\u8eab\u4efd\u8bc1\u53f7", "\u5bc6\u7801", "\u4f59\u989d" + } + )); + table1.setBorder(null); + scrollPane1.setViewportView(table1); + } + contentPane.add(scrollPane1); + pack(); + setLocationRelativeTo(getOwner()); + // JFormDesigner - End of component initialization //GEN-END:initComponents @formatter:on + } + + // JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables @formatter:off + private JPanel panel1; + private JLabel label1; + private JLabel label2; + private JLabel label3; + private JLabel label4; + private JLabel label5; + private JLabel label6; + private JTextField textField1; + private JTextField textField2; + private JTextField textField3; + private JTextField textField4; + private JTextField textField5; + private JTextField textField6; + private JPanel panel2; + private JLabel label7; + private JRadioButton radioButton1; + private JRadioButton radioButton2; + private JRadioButton radioButton3; + private JCheckBox checkBox1; + private JButton button1; + private JButton button2; + private JButton button3; + private JButton button4; + private JButton button5; + private JButton button6; + private JButton button7; + private JButton button8; + private JScrollPane scrollPane1; + private JTable table1; + // JFormDesigner - End of variables declaration //GEN-END:variables @formatter:on +} diff --git a/java-lesson/src/main/java/cn/vscoder/experiment_5/BankSystem/BankManagerSystem.jfd b/java-lesson/src/main/java/cn/vscoder/experiment_5/BankSystem/BankManagerSystem.jfd new file mode 100644 index 0000000..f8ff0ac --- /dev/null +++ b/java-lesson/src/main/java/cn/vscoder/experiment_5/BankSystem/BankManagerSystem.jfd @@ -0,0 +1,268 @@ +JFDML JFormDesigner: "8.2.4.0.393" Java: "21.0.6" encoding: "UTF-8" + +new FormModel { + contentType: "form/swing" + root: new FormRoot { + add( new FormWindow( "javax.swing.JFrame", new FormLayoutManager( class java.awt.GridLayout ) { + "rows": 3 + } ) { + name: "this" + add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class com.jformdesigner.runtime.NullLayout ) ) { + name: "panel1" + add( new FormComponent( "javax.swing.JLabel" ) { + name: "label1" + "text": "银行卡号:" + }, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) { + "x": 25 + "y": 25 + "width": 70 + "height": 40 + } ) + add( new FormComponent( "javax.swing.JLabel" ) { + name: "label2" + "text": "用户姓名:" + }, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) { + "width": 75 + "height": 40 + "x": 25 + "y": 79 + } ) + add( new FormComponent( "javax.swing.JLabel" ) { + name: "label3" + "text": "余 额:" + }, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) { + "width": 75 + "height": 40 + "x": 25 + "y": 129 + } ) + add( new FormComponent( "javax.swing.JLabel" ) { + name: "label4" + "text": "存/取款 :" + }, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) { + "width": 75 + "height": 40 + "x": 355 + "y": 125 + } ) + add( new FormComponent( "javax.swing.JLabel" ) { + name: "label5" + "text": "密 码:" + }, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) { + "width": 75 + "height": 40 + "x": 355 + "y": 75 + } ) + add( new FormComponent( "javax.swing.JLabel" ) { + name: "label6" + "text": "身份证号:" + }, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) { + "width": 75 + "height": 40 + "x": 355 + "y": 20 + } ) + add( new FormComponent( "javax.swing.JTextField" ) { + name: "textField1" + }, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) { + "x": 90 + "y": 25 + "width": 205 + "height": 39 + } ) + add( new FormComponent( "javax.swing.JTextField" ) { + name: "textField2" + }, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) { + "width": 205 + "height": 39 + "x": 90 + "y": 80 + } ) + add( new FormComponent( "javax.swing.JTextField" ) { + name: "textField3" + }, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) { + "width": 205 + "height": 39 + "x": 90 + "y": 130 + } ) + add( new FormComponent( "javax.swing.JTextField" ) { + name: "textField4" + }, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) { + "width": 205 + "height": 39 + "x": 420 + "y": 75 + } ) + add( new FormComponent( "javax.swing.JTextField" ) { + name: "textField5" + }, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) { + "width": 205 + "height": 39 + "x": 420 + "y": 20 + } ) + add( new FormComponent( "javax.swing.JTextField" ) { + name: "textField6" + }, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) { + "width": 205 + "height": 39 + "x": 420 + "y": 125 + } ) + } ) + add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class com.jformdesigner.runtime.NullLayout ) ) { + name: "panel2" + add( new FormComponent( "javax.swing.JLabel" ) { + name: "label7" + "text": "存款期限:" + }, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) { + "x": 50 + "y": 10 + "width": 75 + "height": 35 + } ) + add( new FormComponent( "javax.swing.JRadioButton" ) { + name: "radioButton1" + "text": "1年" + }, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) { + "x": 120 + "y": 15 + } ) + add( new FormComponent( "javax.swing.JRadioButton" ) { + name: "radioButton2" + "text": "3年" + }, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) { + "width": 60 + "height": 24 + "x": 285 + "y": 15 + } ) + add( new FormComponent( "javax.swing.JRadioButton" ) { + name: "radioButton3" + "text": "活期" + }, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) { + "width": 65 + "height": 24 + "x": 440 + "y": 15 + } ) + add( new FormComponent( "javax.swing.JCheckBox" ) { + name: "checkBox1" + "text": "办新卡时存款" + }, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) { + "x": 85 + "y": 50 + } ) + add( new FormComponent( "javax.swing.JButton" ) { + name: "button1" + "text": "办新卡" + }, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) { + "x": 105 + "y": 85 + "width": 90 + "height": 45 + } ) + add( new FormComponent( "javax.swing.JButton" ) { + name: "button2" + "text": "开空卡" + }, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) { + "width": 90 + "height": 45 + "x": 105 + "y": 135 + } ) + add( new FormComponent( "javax.swing.JButton" ) { + name: "button3" + "text": "存款" + }, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) { + "width": 90 + "height": 45 + "x": 210 + "y": 85 + } ) + add( new FormComponent( "javax.swing.JButton" ) { + name: "button4" + "text": "查询" + }, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) { + "width": 90 + "height": 45 + "x": 210 + "y": 135 + } ) + add( new FormComponent( "javax.swing.JButton" ) { + name: "button5" + "text": "取款" + }, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) { + "width": 90 + "height": 45 + "x": 320 + "y": 85 + } ) + add( new FormComponent( "javax.swing.JButton" ) { + name: "button6" + "text": "删除" + }, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) { + "width": 90 + "height": 45 + "x": 320 + "y": 135 + } ) + add( new FormComponent( "javax.swing.JButton" ) { + name: "button7" + "text": "改密码" + }, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) { + "width": 90 + "height": 45 + "x": 430 + "y": 85 + } ) + add( new FormComponent( "javax.swing.JButton" ) { + name: "button8" + "text": "清空" + }, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) { + "width": 90 + "height": 45 + "x": 430 + "y": 135 + } ) + } ) + add( new FormContainer( "javax.swing.JScrollPane", new FormLayoutManager( class javax.swing.JScrollPane ) ) { + name: "scrollPane1" + add( new FormComponent( "javax.swing.JTable" ) { + name: "table1" + "model": new com.jformdesigner.model.SwingTableModel( new java.util.Vector, new java.util.Vector { + add( "卡号" ) + add( "姓名" ) + add( "身份证号" ) + add( "密码" ) + add( "余额" ) + }, new java.util.Vector { + add( null ) + add( null ) + add( null ) + add( null ) + add( null ) + }, new java.util.Vector { + add( null ) + add( null ) + add( null ) + add( null ) + add( null ) + }, new java.util.Vector { + add( null ) + add( null ) + add( null ) + add( null ) + add( null ) + } ) + "border": sfield com.jformdesigner.model.FormObject NULL_VALUE + } ) + } ) + }, new FormLayoutConstraints( null ) { + "location": new java.awt.Point( 0, 0 ) + "size": new java.awt.Dimension( 670, 625 ) + } ) + } +} diff --git a/java-lesson/src/main/java/cn/vscoder/experiment_5/BookSystem/BookManagerSystem.java b/java-lesson/src/main/java/cn/vscoder/experiment_5/BookSystem/BookManagerSystem.java new file mode 100644 index 0000000..a2a3a9a --- /dev/null +++ b/java-lesson/src/main/java/cn/vscoder/experiment_5/BookSystem/BookManagerSystem.java @@ -0,0 +1,298 @@ +/* + * Created by JFormDesigner on Wed May 14 08:53:34 CST 2025 + */ + +package cn.vscoder.experiment_5.BookSystem; + +import java.awt.*; +import java.awt.event.*; +import java.util.Vector; +import javax.swing.*; +import javax.swing.event.ListSelectionEvent; +import javax.swing.event.ListSelectionListener; +import javax.swing.table.*; + +/** + * @author xulongjin + */ +public class BookManagerSystem extends JFrame { + public BookManagerSystem() { + initComponents(); + } + + private void createBook(ActionEvent e) { + // TODO add your code here + dModel = (DefaultTableModel) tableBookList.getModel(); + + Vector data = new Vector<>(); + data.addElement(inputBookSN.getText()); + data.addElement(inputBookName.getText()); + data.addElement(inputBookAuthor.getText()); + data.addElement(inputBookPublisher.getText()); + data.addElement(inputBookPrice.getText()); + data.addElement(inputBookRegisNumber.getText()); + data.addElement(selectIsBorrow.isSelected()); + + dModel.addRow(data); + + inputBookSN.setText(""); + inputBookName.setText(""); + inputBookAuthor.setText(""); + inputBookPublisher.setText(""); + inputBookPrice.setText(""); + inputBookRegisNumber.setText(""); + selectIsBorrow.setSelected(false); + + } + + private void changeInfo(ActionEvent e) { + // TODO add your code here + int i = tableBookList.getSelectedRow(); + if (i > -1) { + DefaultTableModel dModel = (DefaultTableModel) tableBookList.getModel(); + dModel.setValueAt(inputBookSN.getText(), i, 0); + dModel.setValueAt(inputBookName.getText(), i, 1); + dModel.setValueAt(inputBookAuthor.getText(), i, 2); + dModel.setValueAt(inputBookPublisher.getText(), i, 3); + dModel.setValueAt(inputBookPrice.getText(), i, 4); + dModel.setValueAt(inputBookRegisNumber.getText(), i, 5); + dModel.setValueAt(selectIsBorrow.isSelected(), i, 6); + + inputBookSN.setText(""); + inputBookName.setText(""); + inputBookAuthor.setText(""); + inputBookPublisher.setText(""); + inputBookPrice.setText(""); + inputBookRegisNumber.setText(""); + selectIsBorrow.setSelected(false); + + } else { + JOptionPane.showMessageDialog(this, "请先选择要修改的图书!"); + } + } + + private void deleteBook(ActionEvent e) { + // TODO add your code here + DefaultTableModel dModel = (DefaultTableModel) tableBookList.getModel(); + int i = tableBookList.getSelectedRow(); + if (i > -1) { + dModel.removeRow(i); + } else { + JOptionPane.showMessageDialog(this, "请先选择要删除的图书!"); + } + } + + private void initComponents() { + // JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents @formatter:off + panel1 = new JPanel(); + textBookSN = new JLabel(); + textBookName = new JLabel(); + textBookRegisNumber = new JLabel(); + textBookPrice = new JLabel(); + textBookPublisher = new JLabel(); + textBookAuthor = new JLabel(); + inputBookSN = new JTextField(); + inputBookName = new JTextField(); + inputBookRegisNumber = new JTextField(); + inputBookPrice = new JTextField(); + inputBookPublisher = new JTextField(); + inputBookAuthor = new JTextField(); + panel2 = new JPanel(); + buttonCreateBook = new JButton(); + buttonChangeInfo = new JButton(); + buttonDeleteBook = new JButton(); + selectIsBorrow = new JCheckBox(); + scrollPane1 = new JScrollPane(); + tableBookList = new JTable(); + + //======== this ======== + setTitle("\u56fe\u4e66\u7ba1\u7406\u7cfb\u7edf"); + setMaximumSize(new Dimension(2147483647, 600)); + var contentPane = getContentPane(); + contentPane.setLayout(new GridLayout(3, 0)); + + //======== panel1 ======== + { + panel1.setLayout(null); + + //---- textBookSN ---- + textBookSN.setText("\u56fe\u4e66\u7f16\u53f7\uff1a"); + panel1.add(textBookSN); + textBookSN.setBounds(40, 20, 75, 30); + + //---- textBookName ---- + textBookName.setText("\u56fe\u4e66\u540d\u79f0\uff1a"); + panel1.add(textBookName); + textBookName.setBounds(40, 65, 75, 30); + + //---- textBookRegisNumber ---- + textBookRegisNumber.setText("\u56fe\u4e66\u5165\u5e93\u53f7\uff1a"); + panel1.add(textBookRegisNumber); + textBookRegisNumber.setBounds(40, 110, 90, 30); + + //---- textBookPrice ---- + textBookPrice.setText("\u5b9a \u4ef7\uff1a"); + panel1.add(textBookPrice); + textBookPrice.setBounds(320, 20, 75, 30); + + //---- textBookPublisher ---- + textBookPublisher.setText("\u51fa\u7248\u793e \uff1a"); + panel1.add(textBookPublisher); + textBookPublisher.setBounds(320, 65, 75, 30); + + //---- textBookAuthor ---- + textBookAuthor.setText("\u56fe\u4e66\u4f5c\u8005\uff1a"); + panel1.add(textBookAuthor); + textBookAuthor.setBounds(320, 110, 75, 30); + panel1.add(inputBookSN); + inputBookSN.setBounds(115, 15, 180, 39); + panel1.add(inputBookName); + inputBookName.setBounds(115, 60, 180, 39); + panel1.add(inputBookRegisNumber); + inputBookRegisNumber.setBounds(115, 105, 180, 39); + panel1.add(inputBookPrice); + inputBookPrice.setBounds(385, 15, 180, 39); + panel1.add(inputBookPublisher); + inputBookPublisher.setBounds(385, 60, 180, 39); + panel1.add(inputBookAuthor); + inputBookAuthor.setBounds(385, 105, 180, 39); + + { + // compute preferred size + Dimension preferredSize = new Dimension(); + for(int i = 0; i < panel1.getComponentCount(); i++) { + Rectangle bounds = panel1.getComponent(i).getBounds(); + preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width); + preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height); + } + Insets insets = panel1.getInsets(); + preferredSize.width += insets.right; + preferredSize.height += insets.bottom; + panel1.setMinimumSize(preferredSize); + panel1.setPreferredSize(preferredSize); + } + } + contentPane.add(panel1); + + //======== panel2 ======== + { + panel2.setLayout(null); + + //---- buttonCreateBook ---- + buttonCreateBook.setText("\u521b\u5efa\u56fe\u4e66"); + buttonCreateBook.addActionListener(e -> createBook(e)); + panel2.add(buttonCreateBook); + buttonCreateBook.setBounds(45, 55, 110, 50); + + //---- buttonChangeInfo ---- + buttonChangeInfo.setText("\u66f4\u6b63\u4fe1\u606f"); + buttonChangeInfo.addActionListener(e -> changeInfo(e)); + panel2.add(buttonChangeInfo); + buttonChangeInfo.setBounds(230, 55, 110, 50); + + //---- buttonDeleteBook ---- + buttonDeleteBook.setText("\u5220\u9664\u56fe\u4e66"); + buttonDeleteBook.addActionListener(e -> deleteBook(e)); + panel2.add(buttonDeleteBook); + buttonDeleteBook.setBounds(420, 55, 110, 50); + + //---- selectIsBorrow ---- + selectIsBorrow.setText("\u662f\u5426\u53ef\u501f"); + panel2.add(selectIsBorrow); + selectIsBorrow.setBounds(390, 10, 115, 35); + + { + // compute preferred size + Dimension preferredSize = new Dimension(); + for(int i = 0; i < panel2.getComponentCount(); i++) { + Rectangle bounds = panel2.getComponent(i).getBounds(); + preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width); + preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height); + } + Insets insets = panel2.getInsets(); + preferredSize.width += insets.right; + preferredSize.height += insets.bottom; + panel2.setMinimumSize(preferredSize); + panel2.setPreferredSize(preferredSize); + } + } + contentPane.add(panel2); + + //======== scrollPane1 ======== + { + + //---- tableBookList ---- + tableBookList.setModel(new DefaultTableModel( + new Object[][] { + }, + new String[] { + "ISBN", "\u4e66\u540d", " \u4f5c\u8005", " \u51fa\u7248\u793e", " \u5b9a\u4ef7", "\u5165\u5e93\u53f7", "\u662f\u5426\u53ef\u501f" + } + )); + scrollPane1.setViewportView(tableBookList); + } + contentPane.add(scrollPane1); + pack(); + setLocationRelativeTo(getOwner()); + // JFormDesigner - End of component initialization //GEN-END:initComponents @formatter:on + + tableBookList.getSelectionModel().addListSelectionListener(new ListSelectionListener() { + @Override + public void valueChanged(ListSelectionEvent e) { + i = tableBookList.getSelectedRow(); + if (i >= 0) { + inputBookSN.setText(dModel.getValueAt(i, 0).toString()); + inputBookName.setText(dModel.getValueAt(i, 1).toString()); + inputBookAuthor.setText(dModel.getValueAt(i, 2).toString()); + inputBookPublisher.setText(dModel.getValueAt(i, 3).toString()); + inputBookPrice.setText(dModel.getValueAt(i, 4).toString()); + inputBookRegisNumber.setText(dModel.getValueAt(i, 5).toString()); + selectIsBorrow.setSelected(Boolean.parseBoolean(dModel.getValueAt(i, 6).toString())); + } + } + }); + + } + + // JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables @formatter:off + private JPanel panel1; + private JLabel textBookSN; + private JLabel textBookName; + private JLabel textBookRegisNumber; + private JLabel textBookPrice; + private JLabel textBookPublisher; + private JLabel textBookAuthor; + private JTextField inputBookSN; + private JTextField inputBookName; + private JTextField inputBookRegisNumber; + private JTextField inputBookPrice; + private JTextField inputBookPublisher; + private JTextField inputBookAuthor; + private JPanel panel2; + private JButton buttonCreateBook; + private JButton buttonChangeInfo; + private JButton buttonDeleteBook; + private JCheckBox selectIsBorrow; + private JScrollPane scrollPane1; + private JTable tableBookList; + // JFormDesigner - End of variables declaration //GEN-END:variables @formatter:on + + DefaultTableModel dModel; + int i; + + +// @Override +// public void valueChanged(ListSelectionEvent e) { +// i = tableBookList.getSelectedRow(); +// if (i >= 0) { +// inputBookSN.setText(dModel.getValueAt(i, 0).toString()); +// inputBookName.setText(dModel.getValueAt(i, 1).toString()); +// inputBookAuthor.setText(dModel.getValueAt(i, 2).toString()); +// inputBookPublisher.setText(dModel.getValueAt(i, 3).toString()); +// inputBookPrice.setText(dModel.getValueAt(i, 4).toString()); +// inputBookRegisNumber.setText(dModel.getValueAt(i, 5).toString()); +// selectIsBorrow.setSelected(Boolean.parseBoolean(dModel.getValueAt(i, 6).toString())); +// } +// } + +} diff --git a/java-lesson/src/main/java/cn/vscoder/experiment_5/BookSystem/BookManagerSystem.jfd b/java-lesson/src/main/java/cn/vscoder/experiment_5/BookSystem/BookManagerSystem.jfd new file mode 100644 index 0000000..d1dcce3 --- /dev/null +++ b/java-lesson/src/main/java/cn/vscoder/experiment_5/BookSystem/BookManagerSystem.jfd @@ -0,0 +1,203 @@ +JFDML JFormDesigner: "8.2.4.0.393" Java: "21.0.6" encoding: "UTF-8" + +new FormModel { + contentType: "form/swing" + root: new FormRoot { + add( new FormWindow( "javax.swing.JFrame", new FormLayoutManager( class java.awt.GridLayout ) { + "rows": 3 + } ) { + name: "this" + "title": "图书管理系统" + "maximumSize": new java.awt.Dimension( 2147483647, 600 ) + add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class com.jformdesigner.runtime.NullLayout ) ) { + name: "panel1" + add( new FormComponent( "javax.swing.JLabel" ) { + name: "textBookSN" + "text": "图书编号:" + }, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) { + "x": 40 + "y": 20 + "width": 75 + "height": 30 + } ) + add( new FormComponent( "javax.swing.JLabel" ) { + name: "textBookName" + "text": "图书名称:" + }, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) { + "width": 75 + "height": 30 + "x": 40 + "y": 65 + } ) + add( new FormComponent( "javax.swing.JLabel" ) { + name: "textBookRegisNumber" + "text": "图书入库号:" + }, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) { + "width": 90 + "height": 30 + "x": 40 + "y": 110 + } ) + add( new FormComponent( "javax.swing.JLabel" ) { + name: "textBookPrice" + "text": "定 价:" + }, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) { + "width": 75 + "height": 30 + "x": 320 + "y": 20 + } ) + add( new FormComponent( "javax.swing.JLabel" ) { + name: "textBookPublisher" + "text": "出版社 :" + }, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) { + "width": 75 + "height": 30 + "x": 320 + "y": 65 + } ) + add( new FormComponent( "javax.swing.JLabel" ) { + name: "textBookAuthor" + "text": "图书作者:" + }, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) { + "width": 75 + "height": 30 + "x": 320 + "y": 110 + } ) + add( new FormComponent( "javax.swing.JTextField" ) { + name: "inputBookSN" + }, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) { + "x": 115 + "y": 15 + "width": 180 + "height": 39 + } ) + add( new FormComponent( "javax.swing.JTextField" ) { + name: "inputBookName" + }, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) { + "width": 180 + "height": 39 + "x": 115 + "y": 60 + } ) + add( new FormComponent( "javax.swing.JTextField" ) { + name: "inputBookRegisNumber" + }, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) { + "width": 180 + "height": 39 + "x": 115 + "y": 105 + } ) + add( new FormComponent( "javax.swing.JTextField" ) { + name: "inputBookPrice" + }, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) { + "width": 180 + "height": 39 + "x": 385 + "y": 15 + } ) + add( new FormComponent( "javax.swing.JTextField" ) { + name: "inputBookPublisher" + }, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) { + "width": 180 + "height": 39 + "x": 385 + "y": 60 + } ) + add( new FormComponent( "javax.swing.JTextField" ) { + name: "inputBookAuthor" + }, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) { + "width": 180 + "height": 39 + "x": 385 + "y": 105 + } ) + } ) + add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class com.jformdesigner.runtime.NullLayout ) ) { + name: "panel2" + add( new FormComponent( "javax.swing.JButton" ) { + name: "buttonCreateBook" + "text": "创建图书" + addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "createBook", true ) ) + }, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) { + "x": 45 + "y": 55 + "width": 110 + "height": 50 + } ) + add( new FormComponent( "javax.swing.JButton" ) { + name: "buttonChangeInfo" + "text": "更正信息" + addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "changeInfo", true ) ) + }, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) { + "width": 110 + "height": 50 + "x": 230 + "y": 55 + } ) + add( new FormComponent( "javax.swing.JButton" ) { + name: "buttonDeleteBook" + "text": "删除图书" + addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "deleteBook", true ) ) + }, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) { + "width": 110 + "height": 50 + "x": 420 + "y": 55 + } ) + add( new FormComponent( "javax.swing.JCheckBox" ) { + name: "selectIsBorrow" + "text": "是否可借" + }, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) { + "x": 390 + "y": 10 + "width": 115 + "height": 35 + } ) + } ) + add( new FormContainer( "javax.swing.JScrollPane", new FormLayoutManager( class javax.swing.JScrollPane ) ) { + name: "scrollPane1" + add( new FormComponent( "javax.swing.JTable" ) { + name: "tableBookList" + "model": new com.jformdesigner.model.SwingTableModel( new java.util.Vector, new java.util.Vector { + add( "ISBN" ) + add( "书名" ) + add( " 作者" ) + add( " 出版社" ) + add( " 定价" ) + add( "入库号" ) + add( "是否可借" ) + }, new java.util.Vector { + add( null ) + add( null ) + add( null ) + add( null ) + add( null ) + add( null ) + add( null ) + }, new java.util.Vector { + add( null ) + add( null ) + add( null ) + add( null ) + add( null ) + add( null ) + add( null ) + }, new java.util.Vector { + add( null ) + add( null ) + add( null ) + add( null ) + add( null ) + add( null ) + add( null ) + } ) + } ) + } ) + }, new FormLayoutConstraints( null ) { + "location": new java.awt.Point( 0, 0 ) + "size": new java.awt.Dimension( 630, 500 ) + } ) + } +} diff --git a/java-lesson/src/main/java/cn/vscoder/experiment_5/BookSystem/task.java b/java-lesson/src/main/java/cn/vscoder/experiment_5/BookSystem/task.java new file mode 100644 index 0000000..c902bbc --- /dev/null +++ b/java-lesson/src/main/java/cn/vscoder/experiment_5/BookSystem/task.java @@ -0,0 +1,7 @@ +package cn.vscoder.experiment_5.BookSystem; + +public class task { + public static void main(String[] args) { + new BookManagerSystem().setVisible(true); + } +} diff --git a/java-lesson/src/main/java/cn/vscoder/experiment_5/SportRegister.java b/java-lesson/src/main/java/cn/vscoder/experiment_5/Sport_Register/SportRegister.java similarity index 79% rename from java-lesson/src/main/java/cn/vscoder/experiment_5/SportRegister.java rename to java-lesson/src/main/java/cn/vscoder/experiment_5/Sport_Register/SportRegister.java index 74bca7f..786639a 100644 --- a/java-lesson/src/main/java/cn/vscoder/experiment_5/SportRegister.java +++ b/java-lesson/src/main/java/cn/vscoder/experiment_5/Sport_Register/SportRegister.java @@ -1,7 +1,8 @@ -package cn.vscoder.experiment_5; +package cn.vscoder.experiment_5.Sport_Register; import java.awt.*; import java.awt.event.*; +import java.util.ArrayList; import javax.swing.*; import javax.swing.border.*; /* @@ -20,6 +21,9 @@ public class SportRegister extends JFrame { private void selectSexBoyItemStateChanged(ItemEvent e) { // TODO add your code here selectCompetitiveItem.removeAllItems(); + outputMessage.setText(""); + items.clear(); + selectCompetitiveItem.addItem("参赛项"); selectCompetitiveItem.addItem("男子跳高"); selectCompetitiveItem.addItem("男子跳远"); selectCompetitiveItem.addItem("男子铅球"); @@ -31,6 +35,9 @@ public class SportRegister extends JFrame { private void selectSexGirlItemStateChanged(ItemEvent e) { // TODO add your code here selectCompetitiveItem.removeAllItems(); + outputMessage.setText(""); + items.clear(); + selectCompetitiveItem.addItem("参赛项"); selectCompetitiveItem.addItem("女子跳高"); selectCompetitiveItem.addItem("女子跳远"); selectCompetitiveItem.addItem("女子铅球"); @@ -41,10 +48,46 @@ public class SportRegister extends JFrame { private void bottomReset(ActionEvent e) { // TODO add your code here + inputName.setText(""); + inputSn.setText(""); + selectSexBoy.setSelected(true); + outputMessage.setText(""); + items.clear(); } private void bottomPush(ActionEvent e) { // TODO add your code here + if (inputName.getText().isEmpty() || inputSn.getText().isEmpty() || items.isEmpty()) { + JOptionPane.showMessageDialog( + this, + "信息不完整,请填写完整", + "提示", + JOptionPane.ERROR_MESSAGE + ); + } else { + outputMessage.append("\n---------------------\n"); + outputMessage.append("姓名:" + inputName.getText() + ",学号:" + inputSn.getText() + ",\n报名项目:"); + for (int i = 0; i < items.size(); i++) { + outputMessage.append(items.get(i)); + if (!(i == items.size() - 1)){ + outputMessage.append("、"); + } + } + outputMessage.append("\n--------报名成功-------"); + } + + } + + private void selectCompetitiveItem(ActionEvent e) { + // TODO add your code here + String selectedItem = (String) selectCompetitiveItem.getSelectedItem(); + if (selectedItem != null && !selectedItem.trim().isEmpty() && !selectedItem.equals("参赛项")) { + String item = outputMessage.getText(); + if (!item.contains(selectedItem + ";")) { + outputMessage.append(selectedItem + ";"); + items.add(selectedItem); + } + } } private void initComponents() { @@ -59,11 +102,11 @@ public class SportRegister extends JFrame { selectSexGirl = new JRadioButton(); panel3 = new JPanel(); selectCompetitiveItem = new JComboBox<>(); + scrollPane1 = new JScrollPane(); + outputMessage = new JTextArea(); panel4 = new JPanel(); bottomReset = new JButton(); bottomPush = new JButton(); - scrollPane1 = new JScrollPane(); - textArea1 = new JTextArea(); //======== this ======== var contentPane = getContentPane(); @@ -147,15 +190,11 @@ public class SportRegister extends JFrame { //---- selectCompetitiveItem ---- selectCompetitiveItem.setModel(new DefaultComboBoxModel<>(new String[] { - "\u53c2\u8d5b\u9879", - "\u8df3\u9ad8", - "\u8df3\u8fdc", - "100\u7c73", - "400\u7c73", - "\u94c5\u7403" + "\u53c2\u8d5b\u9879" })); + selectCompetitiveItem.addActionListener(e -> selectCompetitiveItem(e)); panel3.add(selectCompetitiveItem); - selectCompetitiveItem.setBounds(115, 30, 140, 40); + selectCompetitiveItem.setBounds(115, 35, 140, 40); { // compute preferred size @@ -174,6 +213,14 @@ public class SportRegister extends JFrame { } contentPane.add(panel3); + //======== scrollPane1 ======== + { + scrollPane1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); + scrollPane1.setBorder(new TitledBorder("\u62a5\u540d\u4fe1\u606f")); + scrollPane1.setViewportView(outputMessage); + } + contentPane.add(scrollPane1); + //======== panel4 ======== { panel4.setBorder(new TitledBorder("\u64cd\u4f5c")); @@ -207,17 +254,13 @@ public class SportRegister extends JFrame { } } contentPane.add(panel4); - - //======== scrollPane1 ======== - { - scrollPane1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); - scrollPane1.setBorder(new TitledBorder("\u62a5\u540d\u4fe1\u606f")); - scrollPane1.setViewportView(textArea1); - } - contentPane.add(scrollPane1); pack(); setLocationRelativeTo(getOwner()); // JFormDesigner - End of component initialization //GEN-END:initComponents @formatter:on + selectSexBoyItemStateChanged(null); + ButtonGroup buttonGroup = new ButtonGroup(); + buttonGroup.add(selectSexGirl); + buttonGroup.add(selectSexBoy); } // JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables @formatter:off @@ -231,10 +274,12 @@ public class SportRegister extends JFrame { private JRadioButton selectSexGirl; private JPanel panel3; private JComboBox selectCompetitiveItem; + private JScrollPane scrollPane1; + private JTextArea outputMessage; private JPanel panel4; private JButton bottomReset; private JButton bottomPush; - private JScrollPane scrollPane1; - private JTextArea textArea1; // JFormDesigner - End of variables declaration //GEN-END:variables @formatter:on + + ArrayList items = new ArrayList<>(); } diff --git a/java-lesson/src/main/java/cn/vscoder/experiment_5/SportRegister.jfd b/java-lesson/src/main/java/cn/vscoder/experiment_5/Sport_Register/SportRegister.jfd similarity index 95% rename from java-lesson/src/main/java/cn/vscoder/experiment_5/SportRegister.jfd rename to java-lesson/src/main/java/cn/vscoder/experiment_5/Sport_Register/SportRegister.jfd index fb267bd..beab0af 100644 --- a/java-lesson/src/main/java/cn/vscoder/experiment_5/SportRegister.jfd +++ b/java-lesson/src/main/java/cn/vscoder/experiment_5/Sport_Register/SportRegister.jfd @@ -78,19 +78,23 @@ new FormModel { "model": new javax.swing.DefaultComboBoxModel { selectedItem: "参赛项" addElement( "参赛项" ) - addElement( "跳高" ) - addElement( "跳远" ) - addElement( "100米" ) - addElement( "400米" ) - addElement( "铅球" ) } + addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "selectCompetitiveItem", true ) ) }, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) { "x": 115 - "y": 30 + "y": 35 "width": 140 "height": 40 } ) } ) + add( new FormContainer( "javax.swing.JScrollPane", new FormLayoutManager( class javax.swing.JScrollPane ) ) { + name: "scrollPane1" + "horizontalScrollBarPolicy": 32 + "border": new javax.swing.border.TitledBorder( "报名信息" ) + add( new FormComponent( "javax.swing.JTextArea" ) { + name: "outputMessage" + } ) + } ) add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class com.jformdesigner.runtime.NullLayout ) ) { name: "panel4" "border": new javax.swing.border.TitledBorder( "操作" ) @@ -113,17 +117,9 @@ new FormModel { "y": 40 } ) } ) - add( new FormContainer( "javax.swing.JScrollPane", new FormLayoutManager( class javax.swing.JScrollPane ) ) { - name: "scrollPane1" - "horizontalScrollBarPolicy": 32 - "border": new javax.swing.border.TitledBorder( "报名信息" ) - add( new FormComponent( "javax.swing.JTextArea" ) { - name: "textArea1" - } ) - } ) }, new FormLayoutConstraints( null ) { "location": new java.awt.Point( 0, 0 ) - "size": new java.awt.Dimension( 425, 565 ) + "size": new java.awt.Dimension( 425, 630 ) } ) } } diff --git a/java-lesson/src/main/java/cn/vscoder/experiment_5/task.java b/java-lesson/src/main/java/cn/vscoder/experiment_5/Sport_Register/task.java similarity index 71% rename from java-lesson/src/main/java/cn/vscoder/experiment_5/task.java rename to java-lesson/src/main/java/cn/vscoder/experiment_5/Sport_Register/task.java index 3dafbe6..d5a4f68 100644 --- a/java-lesson/src/main/java/cn/vscoder/experiment_5/task.java +++ b/java-lesson/src/main/java/cn/vscoder/experiment_5/Sport_Register/task.java @@ -1,4 +1,4 @@ -package cn.vscoder.experiment_5; +package cn.vscoder.experiment_5.Sport_Register; public class task { public static void main(String[] args) { diff --git a/spark-lesson/pom.xml b/spark-lesson/pom.xml index 6b79ccf..c140f54 100644 --- a/spark-lesson/pom.xml +++ b/spark-lesson/pom.xml @@ -136,6 +136,13 @@ ${mysqlconnect.version} + + + org.apache.kafka + kafka-clients + 3.1.0 + + diff --git a/spark-lesson/src/main/scala/date_20250513/MyConsumer.java b/spark-lesson/src/main/scala/date_20250513/MyConsumer.java new file mode 100644 index 0000000..169bfd0 --- /dev/null +++ b/spark-lesson/src/main/scala/date_20250513/MyConsumer.java @@ -0,0 +1,47 @@ +package date_20250513; + +import org.apache.kafka.clients.consumer.*; +import org.apache.kafka.common.serialization.IntegerDeserializer; +import org.apache.kafka.common.serialization.StringDeserializer; + +import java.time.Duration; +import java.util.Arrays; +import java.util.Properties; + + +/** + * 消费者类 + */ + +public class MyConsumer { + + public static void main(String[] args) { + //1.使用Proerties定义配置属性 + Properties props = new Properties(); + //设置生产者Broker服务器连接地址 + props.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "hadoop102:9092"); + //设置序列化key程序类 + props.setProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName()); + //设置序列号化value程序类,此处不一定是非得时Integer,也可以是String + props.setProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName()); + //设置消费者组ID,即组名称,值可自定义,组名称相同的消费者进程属于用一个消费者 + props.setProperty(ConsumerConfig.GROUP_ID_CONFIG, "test-consumer-group4"); + // 设置从头开始消费 + props.put("auto.offset.reset", "earliest"); + //2.定义消费者对象 + Consumer consumer = new KafkaConsumer(props); + //3.设置消费者读取的主题名称,可以设置多个 + consumer.subscribe(Arrays.asList("topic02")); + //4.不同的读取消息 + while (true) { + System.out.println("打印开始"); + //拉取消息,并设置超时时间为10秒 + ConsumerRecords records = consumer.poll(Duration.ofSeconds(100)); + for (ConsumerRecord record : records) { + System.out.println(record.value() + ", " + record.partition() + ",offset: " + record.offset()); + + } + } + + } +} diff --git a/spark-lesson/src/main/scala/date_20250513/MyProducer.java b/spark-lesson/src/main/scala/date_20250513/MyProducer.java new file mode 100644 index 0000000..dad3161 --- /dev/null +++ b/spark-lesson/src/main/scala/date_20250513/MyProducer.java @@ -0,0 +1,48 @@ +package date_20250513; + +import org.apache.kafka.clients.producer.KafkaProducer; +import org.apache.kafka.clients.producer.Producer; +import org.apache.kafka.clients.producer.ProducerConfig; +import org.apache.kafka.clients.producer.ProducerRecord; +import org.apache.kafka.common.serialization.IntegerSerializer; +import org.apache.kafka.common.serialization.StringSerializer; + +import java.util.Properties; + + +/** + * 生产者类 + */ + +public class MyProducer { + + public static void main(String[] args) { + //1.使用Proerties定义配置属性 + Properties props = new Properties(); + //设置生产者Broker服务器连接地址 + props.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "hadoop102:9092"); + //指定topic + String topic = "topic02"; + //设置序列化key程序类 + props.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName()); + //设置序列号化value程序类,此处不一定是非得时Integer,也可以是String + props.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName()); + //2.定义消息生产者对象,依靠此对象可以进行消息的传递 + Producer producer = new KafkaProducer(props); + + //3.循环发送10条消息 + for (int i = 0; i < 10; i++) { + //发送消息,此方式只负责发送消息,不关心是否发送成功 + //第一个参数:主题名称 + //第二个参数,消息的key值 + //第三个参数,消息的value值 + System.out.println(i); + //producer.send(new ProducerRecord(topic,"hello to kafka" + i, String.valueOf(i))); + + producer.send(new ProducerRecord(topic, "hello to kafka" + i)); + + } + //4.关闭生产者,释放资源 + producer.close(); + } +}