3

最初に、デスクトップ Swing アプリケーションで次のコードを使用しました。MyDialog は内部クラスであり、frameJFrame です。

private class MyDialog extends JDialog {
    public MyDialog (String title) {
        super(frame, title, true);
        ...
    }

次に、デスクトップとアプレットの両方をサポートするようにこのコードを修正しました。だからこうなる。またはどちらかowberです。JFrameJApplet

 private class MyDialog extends JDialog {
    public MyDialog (String title) {
        super(SwingUtilities.windowForComponent(owner), title, ModalityType.APPLICATION_MODAL);
        ...
    }

問題は、コードをデスクトップとして実行することですが、モダリティの動作が異なります。アプリケーションの起動後、タスク バーで Eclipse をクリックすると、アプリケーションが Eclipse の背後に隠れます。タスク バーで、アプリケーション アイコンをクリックします。

  1. JFrameJDialogEclipse のすぐ上に表示されます
  2. タスクバーには と の 2 つのオプションがありますJFrameJDialog、どちらも JDialog のみが Eclipse の上に表示され、JFrame は表示されません。

そして、JDialodには、私に最も適した次のコンストラクターがありません:

JDialog(Window owner, String title, boolean modal) 

さまざまなフィールドを試しましModalityTypeたが、スニペット #1 と同じ望ましい結果が得られるものはありません。私のアプローチのどこが間違っていて、なぜ行動が違うのですか?

mKorbelの更新情報:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class WindowForComp {
    private JFrame mainwindow;
    private CustomDialog customDialog;

    private void displayGUI() {
        mainwindow = new JFrame("MyFrame");
        customDialog = new CustomDialog(mainwindow, "Modal Dialog", true);
        mainwindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel contentPane = new JPanel();
        JButton mainButton = new JButton("Just a button");
        mainButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                EventQueue.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        customDialog.setVisible(true);
                    }
                });
            }
        });

        contentPane.add(mainButton);
        mainwindow.setContentPane(contentPane);
        mainwindow.pack();
        mainwindow.setLocationByPlatform(true);
        mainwindow.setVisible(true);
    }

    public static void main(String... args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new WindowForComp().displayGUI();
            }
        });
    }
}


class CustomDialog extends JDialog {
    public CustomDialog(JFrame owner, String title, boolean modal) {
        super(SwingUtilities.windowForComponent(owner), title, ModalityType.APPLICATION_MODAL);
        System.out.println(SwingUtilities.windowForComponent(owner));

        JPanel contentPane = new JPanel();
        JLabel dialogLabel = new JLabel("I am a Label on JDialog.", JLabel.CENTER);
        contentPane.add(dialogLabel);
        setContentPane(contentPane);
        pack();
    }
}
4

1 に答える 1

1

nullを返しているように見えたSwingUtilities.windowForComponent(JFrame)ため、ダイアログに親がありませんでした。

SwingUtilities.windowForComponent(JFrame)はnullを返します

今、私は代わりにこの方法を使用し、それは完全に機能します(モダリティ):

public static Window windowForComponent (Component c) {
    if (c instanceof Window) return (Window)c;
    return SwingUtilities.windowForComponent(c);
}
于 2013-01-15T11:23:58.980 に答える