2010-05-29 32 views
2

如何在点击JOPtionPane按钮后控制窗口发生了什么?我试图实现简单的文件选择器。在我的框架中,我有3个按钮(确定,取消,浏览)。浏览按钮打开文件搜索窗口,拾取文件后应返回主框架。点击确定将打开一个包含文件内容的框架。现在porblem看起来这样。用下面的代码,我可以选择文件,但创建一个新的帧后直接,和我的框架与按键自败:
alt text http://img20.imageshack.us/img20/7614/windowh.png
alt text http://img267.imageshack.us/img267/1953/emptywindow.pngJOptionPane如何工作

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); 
    } 

} 

回答

2

使用你的代码。试图让它直接:

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(); 
     } 
    } 
} 
0

与下面的代码,我可以选择文件 而是直接后,一个新的帧 创建的,我用的按钮框架 自败:

是因为当你点击一个按钮,尽快在JOptionPane上,选项窗格关闭。我真的不明白你在做什么,所以我不能提出建议。

但是,一般来说你的程序的设计是错误的。您不应该在创建GUI的方法中创建和显示选项窗格。执行此代码后,用户将永远无法选择其他文件,因为无法重新显示选项窗格。

所以,也许你需要创建一个像“选择文件”按钮的JFrame。然后,您可以将一个简单的ActionListener添加到显示当前选项窗格的此按钮。那就是你应该用一个永久的JFrame开始显示你的应用程序。然后你使用菜单和菜单项来选择一个文件。这是大多数应用程序的工作原理在“文件”菜单下,通常有一个“打开”菜单项。点击打开会导致一个对话框弹出所有打开的选项。然后,当选择文件时,将在主JFrame中显示该文件的内容。