【问题标题】:JInternalFrame Opening and FocusJInternalFrame打开和聚焦
【发布时间】:2014-10-17 22:14:58
【问题描述】:

我在 DesktopPane 中有几个 JInternalFrames,第一次打开它们时我注意到了一些东西。

当我第一次打开一个,然后打开第二个时,第一个 JInternalFrame 将位于第二个之上。我希望它们在彼此之上打开,但这只是第一次发生。我现在意识到这是因为它们被添加的顺序。但是我的程序将在桌面窗格加载时检索信息,因此我需要在实际打开窗口之前添加 JIF 变量。那么,无论添加顺序如何,如何确保打开的窗口出现在最前面?

我不想使用对话框。

【问题讨论】:

  • 我无法在 Windows 上重新创建此问题,您可能需要提供一个简短的代码示例。您可以使用InternalFrameListener 并在internalFrameActivated 内调用toFront
  • 我的代码初始化了多个 JIF,但在用户打开窗口之前让它们不可见。因此,当他们打开时,我只需 setVisible(true)。我检查了 InternalFrameListener,但它似乎没有检查 setVisible(false); 的东西。已被调用。有没有办法做到这一点?我没有处理我的 JIF,因为需要保留数据直到程序关闭。
  • 没关系,谢谢!当窗口设置为可见时只需调用 toFront() 就足够了。

标签: java swing jinternalframe pane


【解决方案1】:

试试这个代码...

我已经创建了一个示例应用程序供您理解...

public class NewJFrame extends javax.swing.JFrame {


private javax.swing.JButton jButton1;
private javax.swing.JDesktopPane jDesktopPane1;

//This is a Internal Frame I have created
private PaymentInternalFrame payIF;


public NewJFrame() {
    initComponents();
}


private void initComponents() {

    jButton1 = new javax.swing.JButton();
    jDesktopPane1 = new javax.swing.JDesktopPane();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

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

    javax.swing.GroupLayout jDesktopPane1Layout = new javax.swing.GroupLayout(jDesktopPane1);
    jDesktopPane1.setLayout(jDesktopPane1Layout);
    jDesktopPane1Layout.setHorizontalGroup(
        jDesktopPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 0, Short.MAX_VALUE)
    );
    jDesktopPane1Layout.setVerticalGroup(
        jDesktopPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 412, Short.MAX_VALUE)
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 529, Short.MAX_VALUE)
        .addComponent(jDesktopPane1)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jDesktopPane1))
    );

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



private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    try {
        if(payIF==null||payIF.isClosed()){
            payIF = new PaymentInternalFrame();
            mainDesktop.add(payIF);
            payIF.setVisible(true);
            payIF.setMaximum(true);
            payIF.isSelected();
        }else{
            payIF.setSelected(true);
        }
    } catch (PropertyVetoException e) {
        NotificationMessage.errorMessage(this, e);
    }
}                                        



//Main Method...


public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new NewJFrame().setVisible(true);
        }
    });
}

}

【讨论】:

  • 在构造函数之上声明你的代码.. YourInternalFrame yourInternalFrameObject;
  • 请解释您的解决方案并将修改或扩展放入您的帖子而不是 cmets。
猜你喜欢
  • 2011-09-09
  • 1970-01-01
  • 2017-03-19
  • 1970-01-01
  • 2013-03-05
  • 1970-01-01
  • 1970-01-01
  • 2012-07-26
  • 1970-01-01
相关资源
最近更新 更多