2013-04-07 17 views
-1

我想用JFileChooser打開並查看文件,但在查看文件時遇到問題。任何幫助或批評都是開放的,謝謝配偶。我想使用JFileChooser查看和顯示文件,無法弄清楚如何顯示文件

package jmenu_bar; 

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.*; 
import javax.swing.border.BevelBorder; 


/** 
* 
* @author Stafford J Villavicencio 
*/ 

public class Jmenu_Bar extends JFrame 
{ 

    public static void main(String[] args) 
    { 

     //create JFrame 
     final JFrame frame = new JFrame(); 
     frame.setTitle(" JMenuBar Practice "); 
     frame.setSize(400,400); 
     frame.setLocationRelativeTo(null); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 

     //create JMenuBar 
     JMenuBar jbar = new JMenuBar(); 
     jbar.setBorder(new BevelBorder(BevelBorder.RAISED)); 
     //add JMenuBar to JFrame 
     frame.setJMenuBar(jbar); 

     //create JMenu File 
     JMenu file = new JMenu(" File"); 
     //add seperator between sub-Options 
     file.addSeparator(); 
     //add JMenu file to JMenuBAr 
     jbar.add(file); 

     //create JMenuItem exit 
     JMenuItem exit = new JMenuItem(" Exit"); 
     //create ActionListener for exit 
     exit.addActionListener(new ActionListener() 
     { 
      @Override 
      public void actionPerformed(ActionEvent e) 
      { 
       System.exit(0); 
      } 
     }); 
     //add exit to file menu 
     file.add(exit); 

     /*IM trying to have JMenuItem "open" actualy open and display selected file within the JFrame......> 
     *create JMenuItem open 
     */ 
     JMenuItem open = new JMenuItem(" Open"); 
     //create actionListener for open 
     open.addActionListener(new ActionListener() 
     { 
      JFileChooser fChoose = new JFileChooser(); 

      @Override 
      public void actionPerformed(ActionEvent e) 
      { 
       fChoose.showOpenDialog(frame); 

      } 
     }); 
     file.add(open); 

      //create JMenu edit 
      JMenu edit = new JMenu(" Edit"); 
      jbar.add(edit); 

      //create JMenuItem save 
      JMenuItem save = new JMenuItem(" Save"); 
      edit.add(save); 


    } 
} 
+0

您的意思是您在查看用戶點擊「打開」時選擇的文件時遇到問題? – drewmoore 2013-04-07 23:02:44

+0

是的,我的朋友,那完全正確!我可以選擇文件,但一旦選擇就無法查看。如果您看到程序的底部,我正在使用JMenuItem「open」來完成該項目,但我遇到了邏輯問題。 – Bearsjv 2013-04-07 23:10:14

+0

你明白了嗎? – drewmoore 2013-04-08 00:20:19

回答

0

所有文件選擇都會給你一個要讀取的文件的名稱。您仍然有責任閱讀文件的文本並將其加載到Swing組件中。

我會建議使用JTextArea來顯示文本文件。然後你可以使用read(...)方法。

首先,您應該閱讀關於How to Use A File Chooser的Swing教程。然後,一旦你得到你可以做類似的文件名:

FileReader reader = new FileReader(the file name); 
BufferedReader br = new BufferedReader(reader); 
textArea.read(br, null); 

代碼是未經測試,我會讓你工作的細節。

+0

我再次來到了正確的地方,我喜歡編碼並渴望成爲SR級別。感謝隊友,對我有一個虛擬的啤酒。 – Bearsjv 2013-04-07 23:12:39

0

我看着你的程序,它沒有向我顯示任何東西......你只是搞砸了,讓我們看看我的代碼JFile Chooser ..這個代碼只是一個例子,這段代碼打開.gif,。 jpg文件,意味着它可以用作圖片瀏覽器,

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
public class ImageViewer extends JFrame 
{ 
    static JFileChooser imageChooser = new JFileChooser(); 
    static JLabel imageLabel = new JLabel(); 


    public static void main(String args[]) 
    { 
    //construct frame 
    new ImageViewer().show(); 
    } 
    public ImageViewer() 
    { 
    // create frame 
    setTitle("Image Viewer"); 
    setResizable(false); 
    addWindowListener(new WindowAdapter() 
    { 
     public void windowClosing(WindowEvent e) 
     { 
     exitForm(e); 
     } 
    }); 
    getContentPane().setLayout(new GridBagLayout()); 


    GridBagConstraints gridConstraints = new GridBagConstraints(); 
    String[] ext = new String[] {"gif", "jpg"}; 

    gridConstraints.gridy = 0; 
    getContentPane().add(imageChooser, gridConstraints); 
    imageChooser.addActionListener(new ActionListener() 
    { 
     public void actionPerformed(ActionEvent e) 
     { 
     imageChooserActionPerformed(e); 
     } 
    }); 
     imageLabel.setPreferredSize(new Dimension(270, 300)); 
     imageLabel.setBorder(BorderFactory.createLineBorder(Color.RED)); imageLabel.setOpaque(true); 
     imageLabel.setBackground(Color.white); 
     imageLabel.setHorizontalAlignment(SwingConstants.CENTER); 
     imageLabel.setVerticalAlignment(SwingConstants.CENTER); 
     gridConstraints.gridx = 1; 
     gridConstraints.gridy = 0; 
     gridConstraints.insets = new Insets(10, 10, 10, 10); 
     getContentPane().add(imageLabel, gridConstraints); 

     pack(); 
     Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
     setBounds((int) (0.5 * (screenSize.width - getWidth())), (int) (0.5 * (screenSize.height - getHeight())), getWidth(), getHeight()); 


    } 
    private void imageChooserActionPerformed(ActionEvent e) 
    { 
    // create and display graphic if open selected 
    if (e.getActionCommand().equals(JFileChooser.APPROVE_SELECTION)) 
    { 
     ImageIcon myImage = new ImageIcon(imageChooser.getSelectedFile().toString()); 
     imageLabel.setIcon(myImage); 
    } 
    } 


    private void exitForm(WindowEvent e) 
    { 
    System.exit(0); 
    } 
} 
+0

太棒了!我仍然像一位軟件工程師那樣學習,我很喜歡當我能夠看到更高層次的程序員工作時,它激發我更多的動力。 Shukriya。 – Bearsjv 2013-04-07 23:19:54

+0

yewr歡迎,你是印度人嗎?「Shukriya」 – 2013-04-07 23:26:12

+0

沒有我只是一個非常不尋常的美國人....我喜歡印度文化,並希望表現出尊重。 – Bearsjv 2013-04-07 23:40:23

0

我想你是說你不知道如何查看用戶選擇了哪個文件。

 final JFileChooser jfc = new JFileChooser(); 

     int returnVal = jfc.showOpenDialog(this); 

     if (returnVal == JFileChooser.APPROVE_OPTION) { 
      File selected = jfc.getSelectedFile(); 
      //do something with the file 

    } 
} 

使用int returnVal = jfc.showOpenDialog(this);來啓動打開的對話框。這樣,當用戶點擊一個按鈕(取消或批准(打開))時,它將返回一個int值。因此,if語句表示:如果點擊的按鈕是「打開」的,那麼獲取他們選擇的文件,將其稱爲選定的文件並對其進行處理。

讓我知道是否需要更深入地引導您完成任何事情。

相關問題