以下是我使用的代码,但没有我正在使用netbeans ;
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
new NewJFrame().setVisible(true); // to open a new frame
Menu.setVisible(false); //not working
Menu.dispose();// not working
Menu().setVisible(false); //not working
new Menu().setVisible(true); // this don't give me error but nothing happens if I press the button only open the new frame
// I tried all of these with "this." but nothing
}
答案 0 :(得分:3)
适合我的工作
public class ParentFrame extends JFrame {
private JButton btnBirth;
public ParentFrame() {
setTitle("Parent");
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setLayout(new GridBagLayout());
setSize(200, 200);
setLocationByPlatform(true);
btnBirth = new JButton("Spawn");
btnBirth.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setVisible(false);
ChildFrame child = new ChildFrame(ParentFrame.this);
child.setVisible(true);
}
});
add(btnBirth);
}
}
public class ChildFrame extends JFrame {
private JLabel lblGoo;
private JFrame parent;
public ChildFrame(JFrame parent) {
this.parent = parent;
setTitle("Child");
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setLayout(new GridBagLayout());
setSize(100, 100);
setLocationByPlatform(true);
lblGoo = new JLabel("Goo");
add(lblGoo);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
ChildFrame.this.parent.setVisible(true);
}
});
}
}
使用Netbeans表单编辑器
更新public class ParentFrame extends javax.swing.JFrame {
/**
* Creates new form ParentFrame
*/
public ParentFrame() {
initComponents();
setSize(200, 200);
}
/**
* 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")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Parent");
setLocationByPlatform(true);
setPreferredSize(new java.awt.Dimension(200, 200));
getContentPane().setLayout(new java.awt.GridBagLayout());
jButton1.setText("Spawn");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1, new java.awt.GridBagConstraints());
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
setVisible(false);
new ChildFrame(this).setVisible(true);
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
// End of variables declaration
}
public class ChildFrame extends javax.swing.JFrame {
private JFrame parentFrame;
/**
* Creates new form ChildFrame
*/
public ChildFrame() {
initComponents();
setSize(100, 100);
}
public ChildFrame(JFrame frame) {
this();
parentFrame = frame;
}
/**
* 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")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Child");
setLocationByPlatform(true);
setPreferredSize(new java.awt.Dimension(100, 100));
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
doWindowClosing(evt);
}
});
getContentPane().setLayout(new java.awt.GridBagLayout());
jLabel1.setText("Goo");
getContentPane().add(jLabel1, new java.awt.GridBagConstraints());
pack();
}// </editor-fold>
private void doWindowClosing(java.awt.event.WindowEvent evt) {
setVisible(false);
parentFrame.setVisible(true);
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
// End of variables declaration
}