问题描述
想实现的功能能是:当选中复选框的插入操作时能弹出一个能同时输入多项信息的对话框向SQLsever插入信息!自己也谢了一些就是有些问题求高手帮忙看一下importjava.awt.*;importjava.awt.event.*;importjava.awt.Container;importjava.sql.*;importjavax.swing.JButton;importjavax.swing.JDialog;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JTextField;publicclassDataManagement{publicstaticvoidmain(Stringargs[]){MyMenuFramemf=newMyMenuFrame();}}classMyMenuFrameextendsFrame{MenuBarm_MenuBar;MenumenuManagement,menuSearch,sub_Mana_M;MenuItems_personaldata,s_threecheck;CheckboxMenuItemm_insert,m_delete;TextAreata;MyMenuFrame(){super("三检信息管理系统");ta=newTextArea("nnntt欢迎使用三检信息管理系统",1,3);add(ta,BorderLayout.CENTER);m_MenuBar=newMenuBar();menuManagement=newMenu("管理");sub_Mana_M=newMenu("三检信息");m_insert=newCheckboxMenuItem("插入");m_delete=newCheckboxMenuItem("删除");sub_Mana_M.addActionListener(newHandleAct());m_insert.addItemListener(newHandleItem());m_delete.addItemListener(newHandleItem());sub_Mana_M.add(m_insert);sub_Mana_M.add(m_delete);menuManagement.add(sub_Mana_M);m_MenuBar.add(menuManagement);menuSearch=newMenu("查询");s_personaldata=newMenuItem("个人信息查看");s_threecheck=newMenuItem("三检信息查看");s_personaldata.addActionListener(newHandleAct());s_threecheck.addActionListener(newHandleAct());menuSearch.add(s_personaldata);menuSearch.add(s_threecheck);m_MenuBar.add(menuSearch);this.setMenuBar(m_MenuBar);addWindowListener(newHandleClose());this.setSize(200,200);this.setLocation(1000,600);this.setVisible(true);}classHandleActimplementsActionListener{publicvoidactionPerformed(ActionEvente){if(e.getActionCommand()=="三检信息"){dispose();System.exit(0);}if(e.getActionCommand()=="个人信息查看"){ta.setText("lovejade");}elseta.setText("nnntt"+"执行菜单命令:"+e.getActionCommand());}}classInsertDialogextendsJDialog{privatestaticfinallongserialVersionUID=1L;privateJLabellabNo=null;privateJLabellabMoring=null;privateJLabellabNight=null;privateJLabellabBreak=null;privateJTextFieldfldNo=null;privateJTextFieldfldMoring=null;privateJTextFieldfldNight=null;privateJTextFieldfldBreak=null;privateJButtonbtnOK=null;privateJButtonbtnCancel=null;publicInsertDialog(JFramearg0,booleanarg1){super(arg0,arg1);initialize();}privatevoidinitialize(){setSize(300,100);setLocationRelativeTo(null);setResizable(false);Containercont=getContentPane();labNo=newJLabel("学号");labMoring=newJLabel("早检");labNight=newJLabel("课检");labBreak=newJLabel("课检");fldNo=newJTextField();fldMoring=newJTextField();fldNight=newJTextField();fldBreak=newJTextField();btnOK=newJButton("插入");btnCancel=newJButton("取消");Stringssql;finalStringmsg;PreparedStatementps=null;labNo.setBounds(10,10,30,20);labMoring.setBounds(110,10,30,20);labNight.setBounds(210,10,30,20);labBreak.setBounds(310,10,30,20);fldNo.setBounds(40,10,40,20);fldMoring.setBounds(140,10,40,20);fldNight.setBounds(240,10,40,20);fldBreak.setBounds(340,10,40,20);btnOK.setBounds(80,40,60,24);try{StringdriverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";Class.forName(driverName);Stringurl="jdbc:sqlserver://localhost:1434;"+"databaseName=SPJ;";Connectioncon=DriverManager.getConnection(url,"sa","away");ssql="INSERTINTOthreecheckVALUES(?,?,?,?)";ps=con.prepareStatement(ssql);System.out.println("连接MySql成功!!!");}catch(Exceptione){e.printStackTrace();btnOK.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){//实现你的插入操作try{ps.setString(1,fldNo.getText());ps.setInt(2,Integer.parseInt(fldMoring.getText()));ps.setInt(3,Integer.parseInt(fldNight.getText()));ps.setInt(4,Integer.parseInt(fldBreak.getText()));ps.executeUpdate();fldNo.setText("");fldMoring.setText("");fldNight.setText("");fldBreak.setText("");fldNo.requestFocus();}catch(Exceptione1){fldNo.requestFocus();}dispose();}});btnCancel.setBounds(160,40,60,24);btnCancel.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){dispose();}});cont.setLayout(null);cont.add(labNo);cont.add(labMoring);cont.add(labNight);cont.add(labBreak);cont.add(fldNo);cont.add(fldMoring);cont.add(fldNight);cont.add(fldBreak);cont.add(btnOK);cont.add(btnCancel);}}classHandleItemimplementsItemListener{publicvoiditemStateChanged(ItemEvente){if(((CheckboxMenuItem)e.getSource()).getState()){if(e.getItem()=="插入"){ta.setText("lovejade");}else{dispose();System.exit(0);}}}}classHandleCloseextendsWindowAdapter{publicvoidwindowClosing(WindowEvente){dispose();System.exit(0);}}}}
解决方案
解决方案二:
楼主,你问的太多了,你不能要求别人帮你实现一个功能,应该把你遇到的问题说出来。
解决方案三:
问题是写了一个弹出对话框的类要求在处理复选框事件的时弹出这个对话框,但是引用时老是出错运行不了该怎么办?
解决方案四:
你的代码好多错误,你用什么工具写的代码?不会是记事本吧。
解决方案五:
你程序好多错误啊,我刚运行了下,菜单项显示不出来。
解决方案六:
你代码乱,建议你一个个功能实现。
解决方案七:
你代码乱,建议你一个个功能实现。
解决方案八:
插入功能实现了,能向数据插入数据!但是那个删除功能有点问题实现不了各位给看一下:importjava.awt.Container;importjava.awt.Dialog;importjava.awt.Font;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.KeyEvent;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjava.sql.SQLException;importjavax.swing.JButton;importjavax.swing.JDialog;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JMenu;importjavax.swing.JMenuBar;importjavax.swing.JMenuItem;importjavax.swing.JTextField;importjavax.swing.UIManager;publicclassInsertFrameextendsJFrame{Stringssql;PreparedStatementps;privatestaticfinallongserialVersionUID=1L;privateJMenuBarmenuBar=null;publicInsertFrame(){super("三检信息管理系统");initialize();}privatevoidinitialize(){setSize(300,200);setLocationRelativeTo(null);setResizable(false);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);Fontfont=newFont("Dialog",Font.PLAIN,12);UIManager.put("MenuBar.font",font);UIManager.put("Menu.font",font);UIManager.put("MenuItem.font",font);UIManager.put("Button.font",font);UIManager.put("Label.font",font);makeMenuBar();}privateInsertFramegetFrame(){returnthis;}privatevoidmakeMenuBar(){if(menuBar==null){menuBar=newJMenuBar();setJMenuBar(menuBar);JMenumenuManagement;JMenuItemmenuItem;menuManagement=newJMenu("三检管理");menuItem=newJMenuItem("插入",KeyEvent.VK_I);menuItem.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){InsertDialogdia=newInsertDialog(getFrame(),true);dia.setVisible(true);}});menuManagement.add(menuItem);menuItem=newJMenuItem("删除",KeyEvent.VK_D);menuItem.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){//加入你自己的代码DeletaDialogddg=newDeletaDialog(getFrame(),true);ddg.setVisible(true);}});menuManagement.add(menuItem);menuBar.add(menuManagement);}}publicstaticfinalvoidmain(Stringarg[]){InsertFrameframe=newInsertFrame();frame.setVisible(true);}classInsertDialogextendsJDialog{privatestaticfinallongserialVersionUID=1L;privateJLabellabNo=null;privateJLabellabMoring=null;privateJLabellabNight=null;privateJLabellabBreak=null;privateJTextFieldfldNo=null;privateJTextFieldfldMoring=null;privateJTextFieldfldNight=null;privateJTextFieldfldBreak=null;privateJButtonbtnOK=null;privateJButtonbtnCancel=null;publicInsertDialog(JFramearg0,booleanarg1){super(arg0,arg1);initialize();}privatevoidinitialize(){setSize(400,100);setLocationRelativeTo(null);setResizable(false);Containercont=getContentPane();try{StringdriverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";Class.forName(driverName);;Stringurl="jdbc:sqlserver://localhost:1434;"+"databaseName=threechecks;";Connectioncon=DriverManager.getConnection(url,"sa","away");ssql="INSERTINTOthreecheckVALUES(?,?,?,?)";ps=con.prepareStatement(ssql);System.out.println("连接MySql成功!!!");}catch(Exceptione){e.printStackTrace();}labNo=newJLabel("学号");labMoring=newJLabel("早检");labNight=newJLabel("晚检");labBreak=newJLabel("课检");fldNo=newJTextField(10);fldMoring=newJTextField(10);fldNight=newJTextField(10);fldBreak=newJTextField(10);btnOK=newJButton("插入");btnCancel=newJButton("取消");labNo.setBounds(10,10,30,20);labMoring.setBounds(110,10,30,20);labNight.setBounds(210,10,30,20);labBreak.setBounds(310,10,30,20);fldNo.setBounds(40,10,40,20);fldMoring.setBounds(140,10,40,20);fldNight.setBounds(240,10,40,20);fldBreak.setBounds(340,10,40,20);btnOK.setBounds(80,40,60,24);btnOK.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){//实现插入try{ps.setString(1,fldNo.getText());ps.setInt(2,Integer.parseInt(fldMoring.getText()));ps.setInt(3,Integer.parseInt(fldNight.getText()));ps.setInt(4,Integer.parseInt(fldBreak.getText()));ps.executeUpdate();fldNo.setText("");fldMoring.setText("");fldNight.setText("");fldBreak.setText("");fldNo.requestFocus();}catch(Exceptione1){fldNo.requestFocus();}dispose();}});btnCancel.setBounds(160,40,60,24);btnCancel.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){dispose();}});cont.setLayout(null);cont.add(labNo);cont.add(labMoring);cont.add(labNight);cont.add(labBreak);cont.add(fldNo);cont.add(fldMoring);cont.add(fldNight);cont.add(fldBreak);cont.add(btnOK);cont.add(btnCancel);}}classDeletaDialogextendsJDialog{privatestaticfinallongserialVersionUID=1L;privateJLabellabNo=null;privateJTextFieldfldNo=null;privateJButtonOK=null;privateJButtonCancel=null;publicDeletaDialog(JFramearg0,booleanarg1){super(arg0,arg1);DeletaDialog();}privatevoidDeletaDialog(){setSize(400,100);setLocationRelativeTo(null);setResizable(false);Containercont=getContentPane();try{StringdriverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";Class.forName(driverName);;Stringurl="jdbc:sqlserver://localhost:1434;"+"databaseName=threechecks;";Connectioncon=DriverManager.getConnection(url,"sa","away");ssql="delete*Fromthreecheckwhereid=fld.getText()";ps=con.prepareStatement(ssql);System.out.println("连接MySql成功!!!");}catch(Exceptione){e.printStackTrace();}labNo=newJLabel("学号");fldNo=newJTextField(10);OK=newJButton("删除");Cancel=newJButton("取消");labNo.setBounds(10,10,30,20);fldNo.setBounds(40,10,40,70);OK.setBounds(80,40,60,24);OK.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){try{ps.setString(1,fldNo.getText());ps.executeUpdate();fldNo.setText("");fldNo.requestFocus();}catch(SQLExceptione1){fldNo.requestFocus();}dispose();}});Cancel.setBounds(160,40,60,24);Cancel.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){dispose();}});cont.setLayout(null);cont.add(labNo);cont.add(fldNo);cont.add(OK);cont.add(Cancel);}}}
解决方案九:
不好意思,代码太多,有点眼花,唯有飘过!建议,只贴关键代码。或者将问题说明的详细些,再或者将实现流程写出来,说那一步没有实现。
解决方案十:
这是swing代码啊!调试起来不太方便。建议你在其中断点打印或者是弹出alert()来锁定错误行。
解决方案十一:
这是swing代码啊!调试起来不太方便。建议你在其中断点打印或者是弹出alert()来锁定错误行
解决方案十二:
飘走前,留个印
解决方案十三:
我也留个脚印
解决方案十四:
回复八楼的插入已经实现了,不解的是删除操作不能实现,估计是删除这个类写的有点问题代码如下:classDeletaDialogextendsJDialog{privatestaticfinallongserialVersionUID=1L;privateJLabellabNo=null;privateJTextFieldfldNo=null;privateJButtonOK=null;privateJButtonCancel=null;publicDeletaDialog(JFramearg0,booleanarg1){super(arg0,arg1);DeletaDialog();}privatevoidDeletaDialog(){setSize(400,100);setLocationRelativeTo(null);setResizable(false);Containercont=getContentPane();try{StringdriverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";Class.forName(driverName);;Stringurl="jdbc:sqlserver://localhost:1434;"+"databaseName=threechecks;";Connectioncon=DriverManager.getConnection(url,"sa","away");ssql="delete*Fromthreecheckwhereid=fld.getText()";ps=con.prepareStatement(ssql);System.out.println("连接MySql成功!!!");}catch(Exceptione){e.printStackTrace();}labNo=newJLabel("学号");fldNo=newJTextField(10);OK=newJButton("删除");Cancel=newJButton("取消");labNo.setBounds(10,10,30,20);fldNo.setBounds(40,10,40,70);OK.setBounds(80,40,60,24);OK.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){try{ps.setString(1,fldNo.getText());ps.executeUpdate();fldNo.setText("");fldNo.requestFocus();}catch(SQLExceptione1){fldNo.requestFocus();}dispose();}});Cancel.setBounds(160,40,60,24);Cancel.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){dispose();}});cont.setLayout(null);cont.add(labNo);cont.add(fldNo);cont.add(OK);cont.add(Cancel);}}
解决方案十五:
建议拆分实现
解决方案:
ssql="delete*Fromthreecheckwhereid=fld.getText()";这个错了ssql="delete*Fromthreecheckwhereid="+fldNo.getText();
解决方案:
对数据库的操作,sql语法细节很重要。