【问题标题】:how to open other different Jframe after surrounding code with try and catch如何在使用try和catch包围代码后打开其他不同的Jframe
【发布时间】:2016-10-23 03:00:29
【问题描述】:

一段时间以来,我一直在尝试打开不同的JFrame AdminHome,但它没有打开。即使我做了JFrame的方法

AdminHome hmscrn = new AdminHome();

它所做的只是关闭我已经完成的框架并完全结束程序而不是打开主页

private void addBttnActionPerformed(java.awt.event.ActionEvent evt) {                                        
    try{
    String sql = "insert into maintainsite (siteID,country,state,city,revenue,date,capacity) values (?,?,?,?,?,?,?)";
    pst=conn.prepareStatement(sql);
    pst.setString(1, (String) siteID.getSelectedItem());
    pst.setString(2, (String) country.getSelectedItem());
    pst.setString(3, (String) state.getSelectedItem());
    pst.setString(4, (String) city.getSelectedItem());
    pst.setString(5, revenuelb.getText());
    pst.setString(6, ((JTextField) date.getDateEditor().getUiComponent()).getText());
    pst.setString(7, capacitylb.getText());


    pst.execute();
      int replying = JOptionPane.showConfirmDialog(null, "information saved! return to home page?","successful", JOptionPane.YES_NO_OPTION);
    if (replying == JOptionPane.YES_OPTION) {
      JOptionPane.showMessageDialog(null, "got it!");
      close();
    AdminHome hmscrn = new AdminHome();
    hmscrn.setVisible(true);
    }
      else {
       JOptionPane.showMessageDialog(null, "close program!");

    close();
    }


    }
    catch(Exception e){JOptionPane.showMessageDialog(null, "please complete your selection and try again");}
} 

【问题讨论】:

标签: java swing awt


【解决方案1】:

我看到了我的错误。

在属性方面的实际JFrame 选项中,您必须将defaultCloseOperationEXIT ON CLOSE 更改为DISPOSE_ON_CLOSE

我希望我也能拯救经历同样情况的其他人。

【讨论】:

  • 下次请提供minimal reproducible example而不是代码sn-p,它将为我们提供更多信息,您将获得更多、更快、更好的答案。
  • 不,您的问题是您尝试在应该使用 JDialog 的地方使用 JFrame。一个应用程序应该只有一个打开的 JFrame,并且应该避免交换 JFrame。
猜你喜欢
  • 1970-01-01
  • 2016-02-14
  • 2021-03-17
  • 1970-01-01
  • 1970-01-01
  • 2014-08-28
  • 2014-09-02
  • 2013-06-12
  • 2018-03-04
相关资源
最近更新 更多