【问题标题】:"Open file..." dialog box won't close“打开文件...”对话框不会关闭
【发布时间】:2014-03-17 23:30:53
【问题描述】:

您好,我是 Java 语言的新手。我使用 Eclipse 作为我的开发工具。我有代码可以打开文件对话框,但我有两个问题:

  1. 当我选择文件并单击对话框中的“打开”按钮时,对话框再次出现而不是关闭。
  2. 有时对话框中文件名文本框中的文本不清楚和/或按钮上的文本消失。如果我放大对话框,文本将完全显示。

这是我的代码:

package PDFAnnotationPackage;

import java.awt.event.*;
import javax.swing.*;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.*;
import java.io.*;

public class MainForm extends JFrame implements ActionListener  {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    new MainForm();
}

public MainForm(){
    super("Example");
    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });

    // Name the JMenu & Add Items
    JMenu menu = new JMenu("File");
    menu.add(makeMenuItem("Open"));
    menu.add(makeMenuItem("Save"));
    menu.add(makeMenuItem("Quit"));

    // Add JMenu bar
    JMenuBar menuBar = new JMenuBar();
    menuBar.add(menu);
    setJMenuBar(menuBar);
    setSize(300, 300);
    setLocation(200, 200);
    setVisible(true);
}

public void actionPerformed(ActionEvent e) {

    // Menu item actions
    String command = e.getActionCommand();

    if (command.equals("Quit")) {
        System.exit(0);
    } else if (command.equals("Open")) {
        // Open menu item action
        JFileChooser fileChooser = new JFileChooser();           
        if (fileChooser.showOpenDialog(MainForm.this) == JFileChooser.APPROVE_OPTION) {
          File file = fileChooser.getSelectedFile();
          System.out.println("Open menu item clicked");
          // load from file
        }
        if (fileChooser.showOpenDialog(this) == JFileChooser.CANCEL_OPTION ) {

            }


        } else if (command.equals("Save")) {
        // Save menu item action
        System.out.println("Save menu item clicked");
        }
    }

    private JMenuItem makeMenuItem(String name) {
        JMenuItem m = new JMenuItem(name);
        m.addActionListener(this);
        return m;
    }
}

我该如何解决这些问题?提前致谢。

【问题讨论】:

    标签: java dialog jfilechooser


    【解决方案1】:

    您的对话框再次出现,因为您调用了方法 showOpenDialog 两次。试试这个

    if (command.equals("Quit")) {
        // Close application
    } else if (command.equals("Open")) {
        JFileChooser fileChooser = new JFileChooser(); 
        int returnVal = fileChooser.showOpenDialog(parent);
    
        if (returnVal ==  FileChooser.APPROVE_OPTION) {
            File file = fileChooser.getSelectedFile();
            // Load file
        } else if (returnVal == JFileChooser.CANCEL_OPTION ) {
            // Do something else
        } 
    } else if (command.equals("Save")) {
        // Save menu item action
    }
    

    【讨论】:

    • @ThomaszBekas,谢谢。你知道为什么按钮上的文本或对话框上的文本框有时不会显示吗?
    • 请编辑您的问题并粘贴与按钮或文本框相关的代码片段。如果没有,就不可能说为什么会发生这种情况。
    • 我可能会创建另一个问题并发布它。谢谢。
    【解决方案2】:

    您多次调用fileChooser.showOpenDialog(this),这就是您的程序运行正常的原因。而是调用一次fileChooser.showOpenDialog(this),并将其值保存到变量中。

    其实你甚至不需要这个空块:

    if (fileChooser.showOpenDialog(this) == 
        JFileChooser.CANCEL_OPTION ) {
    }
    

    所以摆脱它!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-30
      • 1970-01-01
      • 2010-09-26
      相关资源
      最近更新 更多