简体   繁体   English

从另一个JFrame内部创建的JFrame拒绝出现

[英]JFrame created from inside another JFrame refuses to appear

It appears that tonight will be the night that I complete my rebaptism into the insanity that is Swing. 看来今晚将是我完成对Swing的精神错乱的洗礼的夜晚。 I am sure that my problem is a common, simple one, but I cannot seem to locate an appropriate answer. 我确信我的问题是一个常见的简单问题,但是我似乎找不到合适的答案。

Continuing from my earlier design in another question, I am now trying to call a (NetBeans-designed) JFrame class, called ExamForm, from within my MainForm class (also another JFrame), and despite my attempts, it does not manifest itself. 继续我先前的设计中的另一个问题,我现在尝试从我的MainForm类(也是另一个JFrame)中调用一个(NetBeans设计的)JFrame类,称为ExamForm,尽管我做了尝试,但它并未表现出来。 I have setVisible() to true, and called validate() & pack() with a MinimumSize, but it still exists only in the nether world. 我已经将setVisible()设置为true,并使用MinimumSize调用了validate()和pack(),但是它仍然只存在于其他世界中。

Here is my event code in the MainForm: 这是我在MainForm中的事件代码:

 private void MenuItem_NewMouseClicked(java.awt.event.MouseEvent evt) {                                          
        // TODO add your handling code here:

        ExamForm newForm = new ExamForm();
        newForm.setMinimumSize(new Dimension(200, 200));
        newForm.pack();
        newForm.setVisible(true);
        newForm.validate();

    }           

And the ExamForm class itself: 还有ExamForm类本身:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bakarangerx;

/**
 *
 * @author Ryan
 */
public class ExamForm extends javax.swing.JFrame {

    /**
     * Creates new form Exam
     */
    public ExamForm() {
        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")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        Button_OK = new javax.swing.JButton();
        Button_Cancel = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        TextField_Filename = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        ComboBox_Encoding = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        Button_OK.setText("OK");
        Button_OK.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                Button_OKMouseClicked(evt);
            }
        });

        Button_Cancel.setText("Cancel");
        Button_Cancel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                Button_CancelMouseClicked(evt);
            }
        });

        jLabel1.setText("Filename");

        jLabel2.setText("Encoding");

        ComboBox_Encoding.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        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(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(Button_OK, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Button_Cancel, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(379, 379, 379))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(TextField_Filename, javax.swing.GroupLayout.PREFERRED_SIZE, 400, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(406, 406, 406))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(ComboBox_Encoding, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(633, 633, 633))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(TextField_Filename, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ComboBox_Encoding, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(379, 379, 379)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Button_Cancel)
                    .addComponent(Button_OK))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

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

    private void Button_OKMouseClicked(java.awt.event.MouseEvent evt) {                                       
        // TODO add your handling code here:
    }                                      

    private void Button_CancelMouseClicked(java.awt.event.MouseEvent evt) {                                           
        this.setVisible(false);
        this.dispose();
    }                                          

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ExamForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ExamForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ExamForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ExamForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ExamForm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton Button_Cancel;
    private javax.swing.JButton Button_OK;
    private javax.swing.JComboBox ComboBox_Encoding;
    private javax.swing.JTextField TextField_Filename;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    // End of variables declaration
}

Perhaps one of you could be kind, and help me spot what I am missing? 也许你们当中的一个可能很友善,可以帮助我发现我所缺少的东西?

Make sure that 确保

  • You're not creating the frame from inside a modal dialog 您不是从模态对话框中创建框架
  • That there are no frames set to stayOnTop (and the child frame isn't being placed under it) 没有设置为stayOnTop的框架(子框架未放置在其下方)
  • The child frame has a location applied to, Window#setLocationRelativeTo(Component) might help 子框架已应用位置, Window#setLocationRelativeTo(Component)可能会有所帮助
  • That the frame has a size (other then 0x0) - even if you use pack and minimumSize , it would worth while to set the frames size to an arbitrary amount to test it. 框架的大小(0x0以外)-即使使用packminimumSize ,将框架大小设置为任意值进行测试也是值得的。

Try; 尝试;

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

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