0

時々ダイアログが開かれるフレームがあります。このダイアログを既存のフレームにアタッチしたいので、たとえば、そのフレームをドラッグすると、開いたダイアログがそれに続きます。これを使用して達成できる可能性があると聞いたことがありますGlassPaneが、いくつかのヒントが必要です。現在、新しいダイアログを開いてその位置を相対的に設定するframeと、次のようになります。

ここに画像の説明を入力

  1. 右上隅に取り付けられたフレームの横に「testDialog」を表示したいと思います。
  2. 「テスト」フレームをドラッグすると、「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);
    }

}
4

0 に答える 0