次のコードを見てください
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);
}
}
}
ダイアログ.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 で [OK] ボタンをクリックすると、メイン フォームの右側にダイアログ フォームが追加されます。そのため、メイン フォームを移動すると、ダイアログも移動します。ただし、ダイアログ フォームは独立している必要があります。つまり、ダイアログ フォームの [x] ボタンをクリックすると、メイン フォームではなく、そのフォームのみが存在します。
ボタンがクリックされたときに、このダイアログ フォームをメイン フォームの右側にアタッチするにはどうすればよいですか? 助けてください!