单击 JOPtionPane 按钮后,如何控制窗口发生的情况?我正在尝试实现简单的文件选择器。在我的框架中,我有 3 个按钮(确定、取消、浏览)。浏览按钮打开文件搜索窗口,选择文件后应返回主框架。单击确定将打开一个包含文件内容的框架。现在 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);
}
}
使用第二个代码,我可以返回我的菜单,但我无法弹出这个新框架,它与第一个代码一起出现。如何控制这个?我错过了什么?
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);
}
}