最初に、デスクトップ Swing アプリケーションで次のコードを使用しました。MyDialog は内部クラスであり、frame
JFrame です。
private class MyDialog extends JDialog {
public MyDialog (String title) {
super(frame, title, true);
...
}
次に、デスクトップとアプレットの両方をサポートするようにこのコードを修正しました。だからこうなる。またはどちらかowber
です。JFrame
JApplet
private class MyDialog extends JDialog {
public MyDialog (String title) {
super(SwingUtilities.windowForComponent(owner), title, ModalityType.APPLICATION_MODAL);
...
}
問題は、コードをデスクトップとして実行することですが、モダリティの動作が異なります。アプリケーションの起動後、タスク バーで Eclipse をクリックすると、アプリケーションが Eclipse の背後に隠れます。タスク バーで、アプリケーション アイコンをクリックします。
JFrame
JDialog
Eclipse のすぐ上に表示されます- タスクバーには と の 2 つのオプションがあります
JFrame
がJDialog
、どちらも 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();
}
}