我正在尝试使用 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);
}
}