我执行的操作不起作用,很容易编译

时间:2016-03-11 19:13:46

标签: 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();
}
}

任何想法的人?请帮帮我

2 个答案:

答案 0 :(得分:1)

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

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

同样适用于其他按钮

答案 1 :(得分:0)

在main方法中当你说新的Menu()时,它创建了一个Java.awt.Menu的实例,如果你需要在你的代码中调用menu()方法,你需要使用Assignment实例并调用Menu()方法