java - 我执行的操作不起作用,并且很容易编译

标签 java swing

我正在做一个将数据库连接到 jFrame 的任务,我需要一些帮助。我已经尝试编译java,那没关系,但是当我按下按钮时什么也没有发生。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class Assignment extends JFrame implements ActionListener{
JLabel label, l1, l2, l3, l4, l5;
JButton Add,Delete,Update,Display,Exit;
JTextField tf1, tf2, tf3, tf4, tf5;
Connection conn = null;
Statement stmt = null;
/**
 * Create the frame.
 */
Menu() { 
    setVisible(true);
    setSize(500, 500);
    setLayout(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setTitle("JDBC");
    label = new JLabel("Database"); //Label
    Add = new JButton("Add Data"); //Button
    Delete = new JButton("Delete Data");
    Update = new JButton("Update Data");
    Display = new JButton("Display Data");
    Exit = new JButton("Exit");
    tf1 = new JTextField(); //textfield
    tf2 = new JTextField(); 
    tf3 = new JTextField();
    tf4 = new JTextField();
    /**
    * Set bounds.
    */
    Add.addActionListener(this);
    Delete.addActionListener(this);
    Update.addActionListener(this);
    Display.addActionListener(this);
    Exit.addActionListener(this);
    /**
    * add frame.
    */
}
public void actionPerformed(ActionEvent e) {
    if(e.getActionCommand().equals("Add")){ //i already clicked on the button but it doesnt works
        Add();
    }
    else if(e.getActionCommand().equals("Delete")){} //i havent code for this
    else if(e.getActionCommand().equals("Update")){}
    else if(e.getActionCommand().equals("Display")){}
    else if(e.getActionCommand().equals("Exit")){
        System.exit(0);
    }
    }
/**
 * Create the second frame.
 */
public void Add() { 
    JFrame frm = new JFrame();
    frm.setVisible(true);
    frm.setSize(500, 500);
    frm.setLayout(null);
    frm.setTitle("JDBC");
    l1 = new JLabel("ID : "); //Label
    l2 = new JLabel("Name : ");
    l3 = new JLabel("Adress : ");
    l4 = new JLabel("Gender : ");
    l5 = new JLabel("IP : ");       
    /**
    * Set bounds.
    */
    /**
    * add frames.
    */
    try {
        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection("jdbc:mysql://localhost/academic", "root", "abc"); //connecting
        stmt = conn.createStatement();
        String sql;
        sql = "INSERT INTO student VALUES(" +
              "'" + tf1.getText() + "'," + 
              "'" + tf2.getText() + "'," +
              "'" + tf3.getText() + "'," +
              "'" + tf4.getText() + "'," +
              tf5.getText() + ")";
        stmt.executeUpdate(sql);
        stmt.close();
        conn.close();
    }catch(SQLException se){
        se.printStackTrace();
    }catch(Exception e){
        e.printStackTrace();
    }
}
/**
* Launch the application.
*/
public static void main(String args[]) { 
    new Menu();
}
}

大家有什么想法吗?请帮助我

最佳答案

您应该为按钮设置操作命令

 Add = new JButton("Add Data"); //Button
 Add.setActionCommand("Add");

其他按钮也是如此

关于java - 我执行的操作不起作用,并且很容易编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35948146/

相关文章:

java - 将 UTF-8 PDF 字体获取到用 Java/Spring 编译的 JasperReports 3.7.* 报告中

java - 非对称算法使用的问题

java - 使用 JFrame 和 JPanel 绘制线条

java - 无法获取文本字符串的值

java - 具有多个 "scatter lines"的散点图

java - (如何)是否可以捕获 Java 三元运算符语句中的异常?

java - 需要帮助创建 "help"命令,用户可以随时键入该命令并查看关键字或在遇到困难时获取帮助

java - 如何在运行时控制 OSGi 中的依赖注入(inject)

java - 使用paintComponent以颜色为参数绘制图形

Java 不断给我坐标乘以 93