隐藏表单显示表单B,然后隐藏表单B显示表单Java JFrame

时间:2015-01-16 07:00:52

标签: java swing jframe hide show

我在JFrame表格中有两个表格,比如表格A和表格B

我在表格A中有按钮以显示表格B并隐藏表格A.

我想在表格B中也有按钮显示以前的表格A(不创建新实例)并处理表格B.

这样做的任何线索?感谢。

此时我的代码:

private void buttonAActionPerformed(java.awt.event.ActionEvent evt) {
    FormB formB = new FormB();
    formB.setVisible(true);
    this.setVisible(false);
}

private void buttonBActionPerformed(java.awt.event.ActionEvent evt) {
    FormA formA = new FormA();
    formA.setVisible(true);
    this.dispose();
}

编辑:

找到解决方案,使表单A变为静态和全局,因此可以在表单B中调用。感谢

1 个答案:

答案 0 :(得分:0)

您需要创建一个公共静态同步方法来返回表单,如果为null,则需要创建它。

FormA.java

/**
 *
 * @author joepa37
 */
public class FormA extends javax.swing.JFrame {

    private static FormA f = null;

    public FormA() {
        initComponents();
    }

    public static synchronized FormA getInstance(){
        try {
            if (f == null) {
                f = (FormA) Class.forName("FormA").newInstance();
            }
        } catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
            System.out.println(e.toString());
        }
        return f;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        titleLbl = new javax.swing.JLabel();
        goToBtn = new javax.swing.JButton();
        dataTxt = new javax.swing.JTextField();
        dataLbl = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        titleLbl.setFont(new java.awt.Font("Calibri", 1, 14)); // NOI18N
        titleLbl.setText("FORM A");

        goToBtn.setText("Go to FORM B");
        goToBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                goToBtnActionPerformed(evt);
            }
        });

        dataLbl.setText("Data:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(dataLbl)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(dataTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(goToBtn)
                    .addComponent(titleLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 198, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(94, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(titleLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(goToBtn)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(dataTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(dataLbl))
                .addContainerGap(122, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    private void goToBtnActionPerformed(java.awt.event.ActionEvent evt) {                                        
        FormB.getInstance().setVisible(true);
        this.dispose();
    }                                       

    public static void main(String args[]) {
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                FormA.getInstance().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JLabel dataLbl;
    private javax.swing.JTextField dataTxt;
    private javax.swing.JButton goToBtn;
    private javax.swing.JLabel titleLbl;
    // End of variables declaration                   
}

FormB.java

/**
 *
 * @author joepa37
 */
public class FormB extends javax.swing.JFrame {
    private static FormB f = null;

    public FormB() {
        initComponents();
    }

    public static synchronized FormB getInstance(){
        try {
            if (f == null) {
                f = (FormB) Class.forName("FormB").newInstance();
            }
        } catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
            System.out.println(e.toString());
        }
        return f;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        titleLbl = new javax.swing.JLabel();
        goToBtn = new javax.swing.JButton();
        dataTxt = new javax.swing.JTextField();
        dataLbl = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        titleLbl.setFont(new java.awt.Font("Calibri", 1, 14)); // NOI18N
        titleLbl.setText("FORM B");

        goToBtn.setText("Go to FORM A");
        goToBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                goToBtnActionPerformed(evt);
            }
        });

        dataLbl.setText("Data:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(dataLbl)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(dataTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(goToBtn)
                    .addComponent(titleLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 198, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(94, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(titleLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(goToBtn)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(dataTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(dataLbl))
                .addContainerGap(122, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    private void goToBtnActionPerformed(java.awt.event.ActionEvent evt) {                                        
        FormA.getInstance().setVisible(true);
        this.dispose();
    }                                       

    // Variables declaration - do not modify                     
    private javax.swing.JLabel dataLbl;
    private javax.swing.JTextField dataTxt;
    private javax.swing.JButton goToBtn;
    private javax.swing.JLabel titleLbl;
    // End of variables declaration                   
}

请记得更改 f = (FormB) Class.forName("FormA").newInstance(); Class.forName("FormA")Class.forName("FormB")请记住,参数字符串必须是每个文件的路径。