请看下面的代码
主.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);
}
}
}
对话框.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));
}
}
在这里,我想将 Dialog 表单“附加”到主表单。这意味着,当我单击 Main.Java 中的 OK 按钮时,对话框表单将附加到主表单的右侧。所以,当我移动主窗体时,对话框也会移动。但是,对话框表单应该是独立的,这意味着当我在对话框表单中单击“x”按钮时,只有那个表单存在,而不是主表单。
单击按钮时,如何将此对话框表单附加到主表单的右侧?请帮忙!