【问题标题】:how to retrieve image or text files using path in java?如何在java中使用路径检索图像或文本文件?
【发布时间】:2014-03-25 18:20:49
【问题描述】:

我想从这个文件中得到什么,首先我在flash的基础上做了数据集收集。在 dat 中,我考虑了 swf 和 flv 文件。使用 Java Script 和 eclipse 平台,我们需要在 java 上显示网页,我显示了那个。现在在同一个程序中,我需要一个代码来从桌面获取文件路径并将其显示在 Eclipse 框架上,如网页 显示,我需要获取像 flv 和 swf 这样的文件。但现在至少我必须显示一些 txt 文件和图像。请帮帮我

package browser;

import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;

public class modifiedcode extends JFrame {
    public JPanel address_panel, window_panel;
    public JLabel address_label;
    public JTextField address_tf;
    public JEditorPane window_pane, tree_pane, attr_pane;
    public JScrollPane window_scroll, tree_scroll, attr_scroll;
    TextArea t1, t2;
    public JButton address_b;
    private Go go = new Go();

    public modifiedcode() throws IOException {
        // Define address bar
        address_label = new JLabel(" address: ", SwingConstants.CENTER);
        address_tf = new JTextField("http://www.yahoo.com", 25);
        address_tf.setBounds(80, 10, 150, 30);
        address_label.setBounds(10, 10, 70, 30);
        address_tf.addActionListener(go);
        address_b = new JButton("Go");
        address_b.setBounds(240, 10, 50, 30);
        address_b.addActionListener(go);
        address_panel = new JPanel();
        address_panel.setLayout(null);
        window_pane = new JEditorPane("http://www.yahoo.com") {
            public boolean getScrollableTracksViewportWidth() {
                return true;
            }
        };
        JPanel noWrapPanel = new JPanel(new BorderLayout());
        noWrapPanel.add(window_pane);
        window_pane.setContentType("text/html");
        window_pane.setEditable(false);

        t1 = new TextArea();
        t1.setBounds(650, 380, 300, 250);
        t2 = new TextArea();
        t2.setBounds(650, 50, 300, 280);
        JLabel l = new JLabel("Tree");
        l.setBounds(650, 30, 300, 10);

        JScrollPane window_scroll = new JScrollPane();
        window_scroll
                .setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        window_scroll
                .setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        window_scroll.setViewportView(noWrapPanel);
        window_scroll.setBounds(10, 50, 600, 600);
        window_scroll.setPreferredSize(new Dimension(600, 600));
        window_pane.setBounds(10, 50, 600, 600);

        JFrame f = new JFrame("Web browser");
        JPanel pane = new JPanel();
        pane.setLayout(null);
        // pane.add(window_pane);
        pane.add(window_scroll);
        pane.add(t2);
        pane.add(address_label);
        pane.setVisible(true);
        pane.add(address_tf);
        pane.add(address_b);
        pane.add(l);
        pane.add(t1);
        f.setContentPane(pane);
        f.setSize(1000, 700);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public class Go implements ActionListener {
        public void actionPerformed(ActionEvent ae) {
            try {
                window_pane.setPage(address_tf.getText());
            } catch (MalformedURLException e) { // new URL() failed
                window_pane.setText("MalformedURLException: " + e);
            } catch (IOException e) { // openConnection() failed
                window_pane.setText("IOException: " + e);
            }
        }
    }

    public static void main(String args[]) throws IOException {
        modifiedcode wb = new modifiedcode();
    }
}

【问题讨论】:

  • 欢迎来到 Stack Overflow。请阅读"how to ask" guide。提问时,请在发布之前花一些时间阅读您的问题,并注意您的拼写和语法。在写糟糕的问题时,您的问题更有可能被忽略。

标签: java eclipse swing


【解决方案1】:

要从路径中获取文件,您可以使用 JFrame.class.getResourceAsFile()。这将在类路径中找到一个资源。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-22
    • 2019-06-20
    • 2017-04-05
    • 2014-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多