gpt4 book ai didi

java - 如何使用swing永久存储最近的项目

转载 作者:行者123 更新时间:2023-11-29 05:15:23 26 4
gpt4 key购买 nike

<分区>

我想将最近打开的文件添加到最近菜单。我做了那个任务。但是,它不会永久存储。这意味着当我关闭正在运行的应用程序时,最近的项目也会被删除。我想存储最近的项目永久地,重新运行应用程序后,最近打开的项目可用。

我的代码:

public class RecentItems extends javax.swing.JFrame {
int i=0;
Queue<String> q;
JMenuItem item;
String recentPath;
int recentItems_count=0;
JTextPane textPane;
JScrollPane scrollPane;
public RecentItems() {
initComponents();
q=new LinkedList<>();
textPane=new JTextPane();
scrollPane=new JScrollPane(textPane);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

tp = new javax.swing.JTabbedPane();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
openAction = new javax.swing.JMenuItem();
recentItems = new javax.swing.JMenu();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jMenu1.setText("File");

openAction.setText("OPEN");
openAction.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
openActionActionPerformed(evt);
}
});
jMenu1.add(openAction);

recentItems.setText("Recent");
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 openActionActionPerformed(java.awt.event.ActionEvent evt) {
FileDialog fd = new FileDialog(RecentItems.this, "Select File", FileDialog.LOAD);
fd.setVisible(true);
String title;
String sts;
File file;
if (fd.getFile() != null) {
sts = fd.getDirectory() + fd.getFile();
file=new File(sts);
title=fd.getFile();
JInternalFrame internalFrame = new JInternalFrame("",true,true);
i++;
internalFrame.setName("Doc "+i);
internalFrame.setTitle(title);
try {
internalFrame.setSelected(true);
}
catch (PropertyVetoException ex) {
Logger.getLogger(RecentItems.class.getName()).log(Level.SEVERE, null, ex);
}
internalFrame.add(scrollPane);
tp.add(internalFrame);
try{
tp.setSelectedIndex(i-1);
}
catch(IndexOutOfBoundsException ioe){
}
try {
textPane.setPage(file.toURI().toURL());
}
catch (MalformedURLException ex) {
Logger.getLogger(RecentItems.class.getName()).log(Level.SEVERE, null, ex);
}
catch (IOException ex) {
Logger.getLogger(RecentItems.class.getName()).log(Level.SEVERE, null, ex);
}
setVisible(true);
textPane.setCaretPosition(0);
q.add(sts);
recentItems_count++;
if(recentItems_count>1) {
int size=q.size();
String[] strArray = new String[q.size()];
q.toArray(strArray);
for(int k=0;k<size;k++) {
for(int i=size-1;i>k;i--) {
if(strArray[k].equals(strArray[i])) {
q.remove(strArray[k]);
tp.remove(internalFrame);
}
}
}
}
if(q.size()<=3) {
recentItems.removeAll();
for( String string : q) {
item=recentItems.add(string);
recentPath=item.getText();
}
}
else if(q.size()>3) {
q.remove();
recentItems.removeAll();
for( String string : q) {
item=recentItems.add(string);
recentPath=item.getText();
}
}
}
}

public static void main(String args[]) {
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(RecentItems.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(RecentItems.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(RecentItems.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(RecentItems.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new RecentItems().setVisible(true);
}
});
}
private javax.swing.JMenu jMenu1;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem openAction;
private javax.swing.JMenu recentItems;
private javax.swing.JTabbedPane tp;
}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com