Java Applet JFileChooser按钮问题?

时间:2014-03-06 11:51:06

标签: java swing applet jfilechooser

     if ("Analyze Text File".equals(command)) {
    JFileChooser chooser = new JFileChooser();
    chooser.setMultiSelectionEnabled(true);
    if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
        File[] files = chooser.getSelectedFiles();
        for (File file : files) {
            try {
                BufferedReader reader = new BufferedReader(new FileReader(file));
                    sb.append(line);
                }
                String text = sb.toString();
                Map<Integer, Integer> counts = getCounts(text)
                HistogramPanel panel = new HistogramPanel(width, counts, height, horizon
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

我点击“分析到文本”按钮后,我会尝试弹出JFileChooser,然后允许用户选择要处理的文本文件用我的代码输出一个条形图 由于某种原因,除按钮外一切正常。如果有人可以提供帮助,我们将不胜感激。

    // Object textfile = null; 
  else if("Text".equals(command)) {
    JFileChooser chooser = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter(
          "txt", "text", "docx");
      chooser.setFileFilter(filter);
 int returnVal = JFileChooser.showOpenDialog(null);
       if (returnVal == JFileChooser.APPROVE_OPTION) {
           File file = JFileChooser.getSelectedFile();
           Map<Integer, Integer> counts = getCounts(Stext);
           int width = counts.size() * BAR
           int horizon = height - 25;
           HistogramPanel panel = new HistogramPanel(width, counts, height, horizon);

       }
   }
   }

1 个答案:

答案 0 :(得分:1)

  

“一旦我点击”分析到文字“按钮,”

也许actionCommand不是"Text",你应该检查"Analyze to text"

  

if ("Text".equals(command)) {if ("Analyze to Text".equals(command)) {

     

// or whatever the text/actionCommand of your button is

另一个看似显而易见的问题是,您已宣布JFileChooser chooser,但您尝试访问filechooser


<强>更新

使用此代码。更改按钮文本。如果它不起作用,那么你在代码中改变了其他内容,我无能为力,因为这对我来说很好。

    if ("Analyze Text File".equals(command)) {
        JFileChooser chooser = new JFileChooser();
        int result = chooser.showOpenDialog(null);
        if (result == JFileChooser.APPROVE_OPTION) {
            try {
                File file = chooser.getSelectedFile();
                BufferedReader reader = new BufferedReader(new FileReader(file));
                StringBuilder sb = new StringBuilder();
                String line;
                while ((line = reader.readLine()) != null) {
                    sb.append(line);
                }
                String text = sb.toString();
                Map<Integer, Integer> counts = getCounts(text);
                int width = counts.size() * BAR_WIDTH;
                int max = maxCount(counts);
                int height = max * INCREMENT + 100;
                int horizon = height - 25;
                HistogramPanel panel = new HistogramPanel(width, counts, height, horizon);
                JOptionPane.showMessageDialog(null, panel);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }