Java中实现点击一个按钮出现一个新的页面且关闭当前页面

2024年12月04日 15:40
有5个网友回答
网友(1):

采用以下代码即可:

JButton btn=new JButton(new AbstractAction("关闭并打开") {   
   @Override
   public void actionPerformed(ActionEvent e) {
      oldFrame.dispose();// 关闭并销毁,无需销毁可采用oldFrame.setVisible(false);
      newFrame.setVisible(true);// 打开新窗口
   }
});

网友(2):

是Web工程吗?
你可以直接跳转到某页面,不用关闭,就在当前页面显示你要跳转的目标页面就行了啊!
或者写个js方法:function ct(){
window.open("2.jsp");//这个2.jsp是你要跳转到那个页面的页面url!
window.close();
}
在按钮的onclick="ct();"属性中调用这个方法!

网友(3):

在事件里添加
JFrame f=new JFrame();//不局限于JFrame,以你自己的窗口类名自行修改
this.dispose();//关闭当前窗口
就可以了

网友(4):

public class NewJFrame extends javax.swing.JFrame {

/** Creates new form NewJFrame */
public NewJFrame() {
initComponents();
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
//
private void initComponents() {

jPanel1 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jScrollPane2 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jScrollPane3 = new javax.swing.JScrollPane();
jTree1 = new javax.swing.JTree();
jButton1 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jPanel1.setLayout(new java.awt.CardLayout());

jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);

jPanel1.add(jScrollPane1, "card2");

jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane2.setViewportView(jTable1);

jPanel1.add(jScrollPane2, "card3");

jScrollPane3.setViewportView(jTree1);

jPanel1.add(jScrollPane3, "card4");

jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(451, Short.MAX_VALUE)
.addComponent(jButton1)
.addGap(39, 39, 39))
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 563, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 264, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton1))
);

pack();
}//


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
((CardLayout)this.jPanel1.getLayout()).next(jPanel1);
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JTable jTable1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTree jTree1;
// End of variables declaration
}

网友(5):

JSP中的onclick可以做到