简体   繁体   English

如何对JOptionPane.showMessageDialog的OK执行操作

[英]How to perform action on OK of JOptionPane.showMessageDialog

I am working on my first desktop based Java project. 我正在研究我的第一个基于桌面的Java项目。 I have 2 questions actually 我实际上有两个问题

1) How to perform action on OK button of JOptionPane.showMessageDialog.I want to navigate to a new Jframe say x.java on clicking ok. 1)如何对JOptionPane.showMessageDialog的OK按钮执行操作。我想在单击确定时导航到新的Jframe说x.java。

2) I have a table named as user. 2)我有一个名为user的表。 This table has 8 columns userid (Primary Key), name, password,emailid, dob, mobileno ,city, date. 此表有8列userid(主键),名称,密码,emailid,dob,mobileno,city,date。 Four column entries has to be fetched from a Jframe x and remaining four from other Jframe y. 必须从Jframe x中获取四个列条目,而从其他Jframe y中获取四个列条目。

I wrote the following code 我写了以下代码

For Frame X 对于第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();

And for Frame Y. (userid is primary key) 对于帧Y.(userid是主键)

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' ");  

It keeps throwing exceptions for the above sql query 它不断抛出上述sql查询的异常

java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0). java.sql.SQLException:参数索引超出范围(1>参数个数,为0)。

1) How to perform action on OK button of JOptionPane.showMessageDialog.I want to navigate to a new Jframe say x.java on clicking ok. 1)如何对JOptionPane.showMessageDialog的OK按钮执行操作。我想在单击确定时导航到新的Jframe说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
}

It keeps throwing exceptions for the above sql query 它不断抛出上述sql查询的异常

java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0). java.sql.SQLException:参数索引超出范围(1>参数个数,为0)。

That's because you're using '?' 那是因为你正在使用'?' in Update statement and ' is no needed. 在Update语句中, '不需要。 You should rewrite like this (assuming you're setting parameters properly): 你应该像这样重写(假设你正确设置参数):

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

The showMessageDialog does not return a value, so any must take place in a different JOptionPane. showMessageDialog不返回值,因此任何必须在不同的JOptionPane中进行。 The one in the above answer is a good example. 上面答案中的一个就是一个很好的例子。

Adding to the above answer, if int input is equal to OK_OPTION, you could call dispose() on the current frame (if you are moving from one to the next without keeping the original) and then create a new instance of the desired frame. 添加上面的答案,如果int输入等于OK_OPTION,您可以在当前帧上调用dispose()(如果您在不保留原始帧的情况下从一个移动到下一个),然后创建所需帧的新实例。

Here is my code before closing my java program and it works. 在关闭我的java程序之前,这是我的代码,它的工作原理。 On no_option the programm still running but you have to set the defaultcloseoperation of your frame on : setDefaultCloseOperation( JFrame.DO_NOTHING_ON_CLOSE ); no_option ,程序仍在运行,但你必须在你的框架上设置defaultcloseoperation: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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM