单击JOPtionPane按钮后,如何控制window发生的情况?我正在尝试实现简单的文件选择器。在我的框架中我有3个按钮(OK,Cancel,Browse)。浏览按钮打开文件搜索窗口,选择文件后应返回主机。单击“确定”将打开一个包含文件内容的框架。现在,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);
}
}发布于 2010-05-29 19:42:19
使用你的代码。试图让它变得简单明了:
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));
JPanel creator = new JPanel();
creator.setLayout(new BoxLayout(creator, BoxLayout.Y_AXIS));
creator.add(top);
JFileChooser fc = new JFileChooser(new File("."));
String[] buttons = {"OK", "Cancel", "Browse"};
int rc=-1;
do {
rc = JOptionPane.showOptionDialog(
null,
creator,
frame_name,
JOptionPane.DEFAULT_OPTION,
JOptionPane.PLAIN_MESSAGE,
null,
buttons,
buttons[0]
);
if( rc == 1){
System.exit(0);
break;
}
else if(rc == 2){
int retVal = fc.showDialog(null, "Test");
if (retVal == JFileChooser.APPROVE_OPTION)
System.out.println("File choose" + fc.getSelectedFile());
}
} while (rc != 0);
if( rc == 0){
frame.setVisible(true);
frame.pack();
}
}
}发布于 2010-05-29 10:45:11
您可以让浏览按钮显示FileDialog,如此example所示。
发布于 2010-05-29 12:10:34
使用下面的代码,我可以选择文件,但之后会直接创建一个新的框架,并且我的带有按钮的框架将消失:
是的,因为只要您单击JOptionPane上的按钮,选项窗格就会关闭。我真的不明白你想做什么,所以我不能提出建议。
然而,一般来说,你的程序的设计是错误的。您不应该在创建GUI的方法中创建和显示选项窗格。一旦执行此代码,用户将永远无法选择另一个文件,因为无法重新显示选项窗格。
所以也许你需要用一个类似“选择文件”的按钮来创建你的JFrame。然后,您可以向此按钮添加一个简单的ActionListener,该按钮显示当前选项窗格。也就是说,您应该使用永久的JFrame开始显示您的应用程序。然后使用菜单和菜单项选择文件。这就是大多数应用程序的工作方式。在“文件”菜单下,通常有一个“打开”菜单项。单击Open将弹出一个对话框,其中包含所有打开的选项。然后,当选择一个文件时,您将在主JFrame中显示该文件的内容。
https://stackoverflow.com/questions/2933479
复制相似问题