JOPtionPaneボタンをクリックした後、ウィンドウで何が起こるかを制御するにはどうすればよいですか?単純なファイルチューザーを実装しようとしています。私のフレームには3つのボタンがあります([OK]、[キャンセル]、[参照])。[参照]ボタンをクリックすると、ファイル検索ウィンドウが開き、ファイルを選択するとメインフレームに戻るはずです。[OK]をクリックすると、ファイルの内容を含むフレームが開きます。今、porblemはこのように見えます。以下のコードでファイルを選択できますが、その直後に新しいフレームが作成され、ボタンのあるフレームが消えます:
alt text http://img20.imageshack.us/img20/7614/windowh.png
alt text http:/ /img267.imageshack.us/img267/1953/emptywindow.png
import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.awt.*;
import javax.swing.*;
import java.io.*;
public class Main {
public static void main(String args[]) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
show("Window");
}
});
}
public static void show(String frame_name){
JFrame frame = new JFrame(frame_name);
frame.setPreferredSize(new Dimension(450, 300));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel top = new JPanel();
top.setLayout(new BoxLayout(top, BoxLayout.Y_AXIS));
JFileChooser fc = new JFileChooser(new File("."));
JPanel creator = new JPanel();
creator.setLayout(new BoxLayout(creator, BoxLayout.Y_AXIS));
creator.add(top);
String[] buttons = {"OK", "Cancel", "Browse"};
int rc = JOptionPane.showOptionDialog(
null,
creator,
frame_name,
JOptionPane.DEFAULT_OPTION,
JOptionPane.PLAIN_MESSAGE,
null,
buttons,
buttons[0]
);
String approveButt = "";
switch(rc){
case 0:
break;
case 1:
break;
case 2:
approveButt = buttons[rc];
int retVal = fc.showDialog(null, approveButt);
if (retVal == JFileChooser.APPROVE_OPTION)
System.out.println(approveButt + " " + fc.getSelectedFile());
break;
}
frame.pack();
frame.setVisible(true);
}
}
2番目のコードでメニューに戻ることはできますが、最初のコードで表示されたこの新しいフレームをポップすることはできません。これを制御する方法は?私は何が欠けていますか?
public class Main {
public static void main(String args[]) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
show("Window");
}
});
}
public static void show(String frame_name){
JFrame frame = new JFrame(frame_name);
frame.setPreferredSize(new Dimension(450, 300));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel top = new JPanel();
top.setLayout(new BoxLayout(top, BoxLayout.Y_AXIS));
JFileChooser fc = new JFileChooser(new File("."));
JPanel creator = new JPanel();
creator.setLayout(new BoxLayout(creator, BoxLayout.Y_AXIS));
creator.add(top);
String[] buttons = {"OK", "Cancel", "Browse"};
String approveButt = "";
Plane m = null;
int rc = -1;
while (rc != 0) {
rc = JOptionPane.showOptionDialog(
null,
creator,
frame_name,
JOptionPane.DEFAULT_OPTION,
JOptionPane.PLAIN_MESSAGE,
null,
buttons,
buttons[0]
);
switch (rc) {
case 0:
m = new Plane();
case 1:
System.exit(0);
case 2:
approveButt = buttons[rc];
int retVal = fc.showDialog(null, approveButt);
if (retVal == JFileChooser.APPROVE_OPTION)
System.out.println(approveButt + " " + fc.getSelectedFile());
break;
default:
break;
}
}
addComponents(frame.getContentPane(), m);
frame.pack();
frame.setVisible(true);
}
private static void addComponents(Container c, Plane e) {
c.setLayout(new BoxLayout(c, BoxLayout.Y_AXIS));
c.add(e);
}
}
class Plane extends JPanel {
public Plane(){
}
@Override
public void paint(Graphics g){
g.setColor(Color.BLUE);
g.fillRect(0, 0, 400, 250);
}
}