java - 如何将一个窗口附加到另一个窗口

标签 java swing user-interface jframe jdialog

请看下面的代码

Main.Java

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


public class Main extends JFrame
{
    private JButton ok;

    public Main()
    {
        ok = new JButton("OK");
        ok.addActionListener(new ButtonAction());

        JPanel panel = new JPanel();
        panel.setLayout(new FlowLayout());
        panel.add(ok);

        getContentPane().add(panel,"South");

        this.setVisible(true);
        this.setSize(new Dimension(200,200));
        this.validate();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public static void main(String[]args)
    {
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            new Main();
        }
        catch(Exception e)
        {

        }
    }

    private class ButtonAction implements ActionListener
    {
        public void actionPerformed(ActionEvent ae)
        {
            Dialog d = new Dialog();
            d.setVisible(true);
        }
    }

}

Dialog.java

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


public class Dialog extends JDialog
{
    private JButton done;

    public Dialog()
    {
        done = new JButton("Done");

        this.add(done);

        this.setSize(new Dimension(400,200));
    }

}

在这里,我想将对话框窗体“附加”到主窗体。这意味着,当我单击 Main.Java 中的“确定”按钮时,对话框表单将附加到主表单的右侧。因此,当我移动主窗体时,对话框也会移动。但是,对话框窗体应该是独立的,这意味着,当我单击对话框窗体中的“x”按钮时,只有该窗体存在,而不是主窗体。

当单击按钮时,如何将此对话框表单附加到主表单的右侧?请帮忙!

最佳答案

答案不是MouseListener,而是ComponentListener。我设法使用该监听器的“componentMoved()”方法来做到这一点。

Main.java

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

public class Main extends JFrame implements ComponentListener, ActionListener
{
    private JButton ok;
    private Dialog dialog;

    public Main()
    {
        ok = new JButton("OK");
        ok.addActionListener(this);

        dialog = new Dialog();

        JPanel panel = new JPanel();
        panel.setLayout(new FlowLayout());
        panel.add(ok);

        getContentPane().add(panel,"South");

        this.addComponentListener(this);

        this.setVisible(true);
        this.setSize(new Dimension(200,200));
        this.validate();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[]args)
    {
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            new Main();
        }
        catch(Exception e){}
    }

    public void actionPerformed(ActionEvent ae)
    {   
        dialog.setVisible(true);
    }

    @Override
    public void componentHidden(ComponentEvent arg0) {}

    @Override
    public void componentMoved(ComponentEvent arg0) 
    {
        int x = this.getX() + this.getWidth();
        int y = this.getY();

        dialog.setDialogLocation(x, y);
    }

    @Override
    public void componentResized(ComponentEvent arg0) {}

    @Override
    public void componentShown(ComponentEvent arg0) {}
}

对话框.java

import java.awt.Dimension;

import javax.swing.JButton;
import javax.swing.JDialog;


public class Dialog extends JDialog
{
    private JButton done;

    public Dialog()
    {
        done = new JButton("Done");

        this.add(done);

        this.setSize(new Dimension(400,200));
    }

    public void setDialogLocation(int x, int y)
    {
        this.setLocation(x, y);
    }

}

关于java - 如何将一个窗口附加到另一个窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11833626/

相关文章:

java - 单例设计模式实现

java - 如何使用 jsoup 在 java 中提取/解析此 html 表?

java - 在用户输入值时添加字符

html - Bootstrap 响应式网格中的输入前置

java - 在 Matlab 中从 Java GUI 调用 2 个类

java - 如何将 mysql 命令传递给 ProcessBuilder

java - 如何使用固定宽度的 Swing FlowLayout?

java - Swing:如何忽略取消选择事件?

android - 在 Android 布局中使用 <Space>

java - java中反转整数(关于方法的问题)