時々ダイアログが開かれるフレームがあります。このダイアログを既存のフレームにアタッチしたいので、たとえば、そのフレームをドラッグすると、開いたダイアログがそれに続きます。これを使用して達成できる可能性があると聞いたことがありますGlassPane
が、いくつかのヒントが必要です。現在、新しいダイアログを開いてその位置を相対的に設定するframe
と、次のようになります。
- 右上隅に取り付けられたフレームの横に「testDialog」を表示したいと思います。
- 「テスト」フレームをドラッグすると、「testDialog」がそれに続きます。
これが実際の例です:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Example {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
showGUI();
}
});
}
public static void showGUI() {
final JFrame frame=new JFrame("test");
JButton open=new JButton("Open new dialog");
open.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("test");
JDialog dialog=new JDialog((Frame)null,"testdialog");
dialog.setPreferredSize(new Dimension(200,300));
dialog.getContentPane().add(new JLabel("testlabel"));
dialog.pack();
dialog.setLocationRelativeTo(frame);
dialog.setVisible(true);
}
});
frame.setLayout(new FlowLayout());
frame.getContentPane().add(open);
frame.getContentPane().add(new JLabel("test"));
frame.setLocationRelativeTo(null);
frame.setPreferredSize(new Dimension(400, 200));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}