簡體   English   中英

Jdialog沒有專注於IE9

[英]Jdialog not getting focused in IE9

我正在從applet調用2 jDialog。 一旦我從第一個對話框中選擇了該選項並單擊“確定”按鈕,小程序窗口便被聚焦,第二個對話框失去了焦點。

該問題僅在IE中出現,並且在Firefox和chrome中可以正常工作。 請輸入代碼段。 (盡管我的完整代碼中的實際問題僅發生在IE9中,但我不確定為什么這在SSCCE的IE8中不起作用)

public class SampleApplet extends Applet{

protected JButton countryButton = new JButton("Select");

public synchronized void init()
{
    this.setBounds(new Rectangle(350,350));
    this.add(countryButton);


    countryButton.addActionListener(new ActionListener(){

        public void actionPerformed(ActionEvent arg0) {
            getCountries();
            getCountries();             
        }

    });
}

protected void getCountries() {
    JPanel panel = new JPanel(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    JComboBox CountriesCombo = new JComboBox();
    CountriesCombo.addItem("India");
    CountriesCombo.addItem("Japan");
    panel.add(CountriesCombo, gbc);

    JOptionPane     optionPane  = new JOptionPane(panel, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION);

    final JDialog   dialog      = optionPane.createDialog(panel, "Select Countries");
    dialog.setModal(true);

    dialog.addWindowListener ( new WindowAdapter ()
    {
        public void windowOpened ( WindowEvent e )
        {
            dialog.requestFocus ();
        }
    });
    dialog.pack();
    dialog.setLocationRelativeTo(null);
    dialog.setVisible(true);
}

}

HTML代碼:

<html>
<head>
    <title>Sample Code</title>
</head>
<body>
    <applet code="SampleApplet.class" width="350" height="350">
    </applet>

我可以在這方面尋求幫助嗎?

此行為可能取決於您在其中運行applet的瀏覽器(似乎已經證明了這一點),因此我建議您嘗試在對話框(或對話框中的組件)上調用requestFocus()和requestFocusInWindow()以獲取將焦點放到對話框中。

再說一次-如果您在打開對話框之前(在setVisible(true)之前)請求焦點,則它將失敗,因為該對話框尚未顯示。 如果在setVisible(true)方法之后調用它-如果您的對話框(或OptionPane)是模態的-它僅在對話框關閉時執行,因此在那里也沒有意義。 您應該將WindowListener添加到對話框中,並在對話框打開后請求焦點。

檢查以下示例:

public static void main ( String[] args )
{
    // Modal dialog
    final JDialog dialog = new JDialog (  );
    dialog.setModal ( true );

    // Adding listener
    dialog.addWindowListener ( new WindowAdapter ()
    {
        public void windowOpened ( WindowEvent e )
        {
            dialog.requestFocus ();
        }
    } );

    // Displaying dialog
    dialog.setVisible ( true );
}

盡管如此,dialog.requestFocus()仍是一個依賴於平台的調用,它可能不會將對話框聚焦/彈出到其他打開的窗口之上。 在所有Windows版本中,它應該都能正常工作。

您也可以嘗試使用dialog.toFront()-這將彈出對話框並將焦點轉移到其中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM