2013-07-31 69 views
5

我正在开发基于桌面的第一个Java项目。我实际上有两个问题如何对JOptionPane.showMessageDialog的OK执行操作

1)如何在JOptionPane.showMessageDialog的OK按钮上执行操作。我想要导航到一个新的Jframe,在点击ok时指定x.java。

2)我有一个名为用户的表。此表有8列userid(主键),名称,密码,emailid,dob,mobileno,城市,日期。四个列条目必须从Jframe x中提取,其余四个从其他Jframe y中提取。

我写了下面的代码

对于帧X

PreparedStatement stm = con.prepareStatement("insert into user 

(userrid,name,password,emailid))values (?,?,?,?) "); 

     stm.setString(1,id); // id is a public variable 
     stm.setString(2,name); 
     stm.setString(3,ps); 
     stm.setString(4,email); 
    stm.executeUpdate(); 

而对于帧Y.(用户ID是主键)

public class Y extends javax.swing.JFrame 
{ 
    X o = new X(); // to access id variable from frame X 

} 



PreparedStatement stm = con.prepareStatement(" update user set dob ='? ', mobileno 
='?' ,city='?', date='?' where userid= 'o.id' "); 

持续了上面的SQL查询抛出异常

java.sql.SQLException:参数索引超出范围(1> pa数rameters,这是0)。

回答

11

1)如何在JOptionPane.showMessageDialog的OK按钮上执行动作。我想要导航到一个新的Jframe,在点击ok时指定x.java。

int input = JOptionPane.showOptionDialog(null, "Hello World", "The title", JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE, null, null, null); 

if(input == JOptionPane.OK_OPTION) 
{ 
    // do something 
} 
0

的showMessageDialog不返回值,所以任何必须在不同的JOptionPane。上述答案中的一个就是一个很好的例子。

添加到上面的答案中,如果int输入等于OK_OPTION,则可以在当前帧上调用dispose()(如果您正在从一个移动到下一个而不保留原始),然后创建一个新实例期望的框架。

1

它保持抛出异常对于上述SQL查询

java.sql.SQLException中:参数索引超出范围(1> 参数号,它是0)。

这是因为您在Update语句中使用'?',并且不需要'。你应该重写像这样(假设你正确设置参数):

PreparedStatement stm = con.prepareStatement("UPDATE user SET dob = ?, mobileno = ?, city = ?, date = ? where userid= 'o.id' "); 
+0

是的愚蠢的错误!我后来意识到它。谢谢 :) – Malwaregeek

0

这里结束我的java程序之前,我的代码和它的作品。在no_option程序仍在运行,但您必须设置帧的默认关闭操作:setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

int dialogResult = JOptionPane.showConfirmDialog(frame, "Are you sure to close this window?", "Really Closing me?",JOptionPane.OK_CANCEL_OPTION); 
      if (dialogResult==0){ 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        try{ 
        myDBExecuter.closeConnection(); 
        myDBExecutero.closeConnection(); 
        }catch(Exception e){ 
         JOptionPane.showMessageDialog(null,new JTextField(" GoodBye :(")); 
        } 
        System.exit(0); 
      }else contentPane.updateUI();//after else can you put what you want als alternative 
相关问题