0
首先通過打開的menuitem打開文件,但是當第二次打開時,我選擇在新選項卡中打開的文件。我的要求,它不是在新標籤打開並顯示消息文件已經存在。當我打開文件第一次打開文件,但如果選擇第二次相同的文件沒有打開如何在java swing中做到這一點?
這裏是我的代碼,
public class Open extends javax.swing.JFrame {
JTextArea tx;
int i=0;
public Open() {
initComponents();
}
@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();
Open = new javax.swing.JMenuItem();
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);
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) {
final JFileChooser jc = new JFileChooser();
JComponent panel = new JPanel((LayoutManager) new FlowLayout(
FlowLayout.LEFT));
int returnVal= jc.showOpenDialog(Open.this);
String title;
String sts;
File file=null;
if(returnVal == JFileChooser.APPROVE_OPTION)
file = jc.getSelectedFile();
JTextArea text = new JTextArea();
if (jc.getSelectedFile()!= null) {
tx = new JTextArea();
BufferedReader br = null;
StringBuffer str = new StringBuffer("");
StringBuffer st = new StringBuffer("");
try {
br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
str.append(line + "\n");
}
}
catch (IOException ex) {
Logger.getLogger(Open.class.getName()).log(Level.SEVERE, null, ex);
}
String t = str.toString();
// tx = new JTextArea();
final JInternalFrame internalFrame = new JInternalFrame("",true,true);
title=file.getName();
sts=file.getPath();
tx.setFont(new java.awt.Font("Miriam Fixed", 0, 13));
//tx.setLineWrap(true);
internalFrame.add(tx);
i+=1;
internalFrame.setName("Doc "+i);
JScrollPane scrollpane=new JScrollPane(tx);
internalFrame.setTitle(title);
tp.add(internalFrame);
try{
tp.setSelectedIndex(i-1);
}
catch(IndexOutOfBoundsException ioe){
}
internalFrame.add(scrollpane);
internalFrame.setVisible(true);
internalFrame.addInternalFrameListener(new InternalFrameAdapter() {
@Override
public void internalFrameClosing(InternalFrameEvent e) {
tp.remove(internalFrame);
}
});
tx.setText(t);
try {
br.close();
}
catch (IOException ex) {
Logger.getLogger(Open.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
Logger.getLogger(Open.class.getName()).log(Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
Logger.getLogger(Open.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(Open.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedLookAndFeelException ex) {
Logger.getLogger(Open.class.getName()).log(Level.SEVERE, null, ex);
}
new Open().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JMenuItem Open;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JTabbedPane tp;
// End of variables declaration
}
您需要將'File'與'JInternalFrame'關聯,使用某種'Map'。這樣,你可以檢查'Map'是否已經包含'File',如果是,你可以提取關聯的'JInternalFrame'並使其活躍......或者你想要做什麼...... – MadProgrammer 2014-09-26 07:37:03
你可以試試看[Collections Trail](http://docs.oracle.com/javase/tutorial/collections/) – MadProgrammer 2014-09-26 08:17:33