首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jMenuItem ActionListiner

jMenuItem ActionListiner
EN

Stack Overflow用户
提问于 2014-06-17 19:02:42
回答 1查看 114关注 0票数 1

尊重爵士,以下代码状态中的问题是什么

  1. 我想执行jMenuItem ActionListiner操作
  2. 对于新客户jMenuItem,它的工作,但对于删除客户jMenuItem,它不工作
  3. 我执行代码,但没有得到我想要的适当输出。

主菜单代码

代码语言:javascript
复制
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class MainMenu extends JFrame implements ActionListener
{ 
    //Constructor   
public MainMenu() 
{
    JMenuBar menuBar = new JMenuBar();
    this.setJMenuBar(menuBar);
    this.setVisible(true);
    this.setSize(646,401);
    this.setResizable( false );
    this.setTitle("Shree Datta Digambar");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JMenu customer=new JMenu("Customer");
    menuBar.add(customer);
    JMenu paper=new JMenu("Paper");
    menuBar.add(paper);
    JMenu edit=new JMenu("Edit");
    menuBar.add(edit);              
    JMenu view=new JMenu("View");
    menuBar.add(view); 
    JMenu bill=new JMenu("Bill");
    menuBar.add(bill);
    JMenu help=new JMenu("Help");
    menuBar.add(help);

   JMenuItem newItem = new JMenuItem("New Customer");
    customer.add(newItem);
    JMenuItem deleteItem = new JMenuItem("Delete Customer");
    customer.add(deleteItem);

    JMenuItem addpaperItem = new JMenuItem("Add Paper");
    paper.add(addpaperItem);
    JMenuItem deletepaperItem = new JMenuItem("Delete Paper");
    paper.add(deletepaperItem);

    JMenuItem customer_detail = new JMenuItem("Customer Detail");
    edit.add(customer_detail);
    JMenuItem paper_detail = new JMenuItem("Paper Detail");
    edit.add(paper_detail);

    JMenuItem perticular_customer_detail = new JMenuItem("Perticular Customer Detail");
    view.add(perticular_customer_detail);

    JMenuItem customer_bill = new JMenuItem("Customer Bills");
    bill.add(customer_bill);   
    newItem.addActionListener(this);  
    newItem.setActionCommand("New Customer"); 
    deleteItem.addActionListener(this);  
    deleteItem.setActionCommand("Delete Customer");
}
    public void actionPerformed(ActionEvent e) 
    {     
        if(e.getActionCommand().equals("New Customer"))
        {         
            System.out.println("clicked new customer menu");  
            MainMenu.this.dispose(); 
            New_Customer nn = new New_Customer() {}; 
        }
        else if(e.getActionCommand().equals("Delete Customer"))   
        {                                 
            System.out.println("clicked new customer menu");  
            MainMenu.this.dispose(); 
            Delete_Customer ntn = new Delete_Customer(); 
        } 
    }
public static void main(String arg[])
{
    MainMenu menu1= new MainMenu();
}   
}

删除客户代码

代码语言:javascript
复制
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.*;
 public class Delete_Customer extends JFrame implements ActionListener
 {
JTextField tx1;
JLabel l1;
JLabel l2;
JButton b1;
Container c = getContentPane();
Delete_Customer()
{
    super("Shree Datta Digambar");
    setBounds(140,50,525,450);
    c.setLayout(null);
    tx1 = new JTextField(); 
l1 = new JLabel("Delete Customer Entry");
l2 = new JLabel("Building No");     
l1.setBounds(10,10,340,20);
l2.setBounds(10,20,140,70);
tx1.setBounds(10,70,90,20);     
b1 = new JButton("Ok"); 
b1.setBounds(110,70,70,20);
c.add(b1);
    c.add(l1);
c.add(l2);
    c.add(tx1);        
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
    b1.addActionListener(this);        
}    
 public void actionPerformed(ActionEvent e)
        {

            System.out.println("You clicked the button");
            if(e.getSource()==b1)
            {
                try 
                {
                    Connection con;
                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                    con = DriverManager.getConnection("jdbc:odbc:Dalvi");
                    try 
                    {

                        java.sql.Statement st = con.createStatement();
                        PreparedStatement ps = con.prepareStatement("DELETE FROM customer_details " + " WHERE Customer_Id = ?");                            
                        ps.setString(1,tx1.getText());
                        int string = ps.executeUpdate();
                        tx1.setText("");
                        JOptionPane.showMessageDialog(null,"You Successfully Deleted The Customer");                           
                    } 
                    catch (SQLException s) 
                    {
                        System.out.println("SQL code does not execute.");
                        JOptionPane.showMessageDialog(null,"Please Enter the Detail Correctly");   
                    }
                } 
                catch (ClassNotFoundException | SQLException ee) 
                {
                    System.out.println("Error:connection not created");
                    JOptionPane.showMessageDialog(null,"Please Enter the Detail Correctly");
                }
            }

            }

public static void main(String args[])
{
    Delete_Customer dc = new Delete_Customer();
}
}

谢谢你的帮助。更重要的是,如果任何一个程序员想建议我的贡献,你可以。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-17 19:17:21

在您的中删除客户代码

请在主method.Try上发表评论

代码语言:javascript
复制
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.*;
public class Delete_Customer extends JFrame implements ActionListener
{
  JTextField tx1;
  JLabel l1;
 JLabel l2;
JButton b1;
Container c = getContentPane();
Delete_Customer()
{
 super("Shree Datta Digambar");
 setBounds(140,50,525,450);
 c.setLayout(null);
 tx1 = new JTextField(); 
l1 = new JLabel("Delete Customer Entry");
l2 = new JLabel("Building No");     
l1.setBounds(10,10,340,20);
l2.setBounds(10,20,140,70); 
tx1.setBounds(10,70,90,20);     
b1 = new JButton("Ok"); 
b1.setBounds(110,70,70,20);
c.add(b1);
c.add(l1);
c.add(l2);
c.add(tx1);        
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
b1.addActionListener(this);        
}    
public void actionPerformed(ActionEvent e)
    {

        System.out.println("You clicked the button");
        if(e.getSource()==b1)
        {
            try 
            {
                Connection con;
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                con = DriverManager.getConnection("jdbc:odbc:Dalvi");
                try 
                {

                    java.sql.Statement st = con.createStatement();
                    PreparedStatement ps = con.prepareStatement("DELETE FROM customer_details " + " WHERE Customer_Id = ?");                            
                    ps.setString(1,tx1.getText());
                    int string = ps.executeUpdate();
                    tx1.setText("");
                    JOptionPane.showMessageDialog(null,"You Successfully Deleted The Customer");                           
                } 
                catch (SQLException s) 
                {
                    System.out.println("SQL code does not execute.");
                    JOptionPane.showMessageDialog(null,"Please Enter the Detail Correctly");   
                }
            } 
            catch (ClassNotFoundException | SQLException ee) 
            {
                System.out.println("Error:connection not created");
                JOptionPane.showMessageDialog(null,"Please Enter the Detail Correctly");
            }
        }

        }

  /*public static void main(String args[]) 
 {
  Delete_Customer dc = new Delete_Customer();
 }*/   
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24271447

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档