-1
当我打开添加到文件中最近菜单的文件时,但当我单击最近菜单的添加路径时,它将打开。怎么样?如何打开最近添加到Swing菜单中的文件?
这里是我的代码:
public class RecentItemList extends javax.swing.JFrame {
JTextArea tx;
int i=0;
int recentItems_count=0;
String filename;
String recentItem;
Queue<String> q;
public RecentItemList() {
q=new LinkedList<>();
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
tp = new javax.swing.JTabbedPane();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
create = new javax.swing.JMenuItem();
save = new javax.swing.JMenuItem();
open = new javax.swing.JMenuItem();
recentItems = new javax.swing.JMenu();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jMenu1.setText("File");
open.setText("Open");
open.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
openActionPerformed(evt);
}
});
jMenu1.add(open);
recentItems.setText("Recent Items.....");
recentItems.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
recentItemsActionPerformed(evt);
}
});
jMenu1.add(recentItems);
jMenuBar1.add(jMenu1);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tp, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tp, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
private void openActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openActionPerformed
FileDialog fd = new FileDialog(RecentItemList.this, "Select File", FileDialog.LOAD);
fd.show();
String title;
String sts;
if (fd.getFile() != null) {
sts = fd.getDirectory() + fd.getFile();
title=fd.getFile();
System.out.println("title :"+sts);
BufferedReader br = null;
StringBuffer str = new StringBuffer("");
try {
br = new BufferedReader(new FileReader(sts));
String line;
try {
while ((line = br.readLine()) != null) {
str.append(line + "\n");
}
} catch (IOException ex) {
Logger.getLogger(RecentItemList.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(RecentItemList.class.getName()).log(Level.SEVERE, null, ex);
}
String t = str.toString();
final JInternalFrame internalFrame = new JInternalFrame("",true,true);
tx = new JTextArea();
internalFrame.add(tx);
i+=1;
internalFrame.setName("Document"+i);
internalFrame.setTitle(title);
tp.add(internalFrame);
internalFrame.setVisible(true);
internalFrame.addInternalFrameListener(new InternalFrameAdapter() {
@Override
public void internalFrameClosing(InternalFrameEvent e) {
tp.remove(internalFrame);
}
});
tx.setText(t);
q.add(sts);
recentItems.add(sts);
recentItems_count++;
if(recentItems_count>2){
recentItem=(String)q.remove();
recentItems.removeAll();
// recentItems_count--;
for (String string : q) {
recentItems.add(string);
}
}
try {
br.close();
}
catch (IOException ex) {
Logger.getLogger(RecentItemList.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
private void recentItemsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_recentItemsActionPerformed
Object[] selectedObjects = recentItems.getSelectedObjects();
System.out.println(selectedObjects);
}//GEN-LAST:event_recentItemsActionPerformed
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(RecentItemList.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(RecentItemList.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(RecentItemList.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(RecentItemList.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new RecentItemList().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JMenuItem create;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem open;
private javax.swing.JMenu recentItems;
private javax.swing.JMenuItem save;
private javax.swing.JTabbedPane tp;
// End of variables declaration//GEN-END:variables
}
查找到['JTextComp onent.read(阅读器,对象)'](http://docs.oracle.com/javase/8/docs/api/javax/swing/text/JTextComponent.html#read-java.io.Reader-java.lang .Object-)为此。 –
*“当我打开添加到文件中最近菜单的文件时,但当我单击最近菜单的添加路径时,它将打开。”*等等..什么?我不明白这些词的组合。请尝试用下面的话来描述:a)你预期会发生什么b)实际发生了什么,以及对于效用c)为什么你期望(a)发生。 –
像编辑加opend文件自动添加到文件菜单中最近的菜单。最近的菜单有最近打开的文件。我怎样才能打开(点击)从最近的菜单中的这些文件。 – user3531731