我希望所有打開的文檔的工作功能不是當前打開的僅文檔。我創建了一個swing應用程序。我打開了一個文檔。通過menuItem的剪切,複製,粘貼和selectAll操作適用於當前打開的文檔;不適用於以前打開的文檔。請幫幫我。如何在擺動中處理所有文檔的功能
這裏是我的代碼:
public class OpenDemo extends javax.swing.JFrame implements ChangeListener{
JTextPane textPane;
JTextPane lnNum;
JScrollPane scrollPane;
int i=0;
JTextField status;
ArrayList<JTextPane> textPanes=new ArrayList<>();
public OpenDemo() {
initComponents();
viewLineNumbers.setSelected(false);
}
@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();
cut = new javax.swing.JMenuItem();
selectAll = new javax.swing.JMenuItem();
viewLineNumbers = new javax.swing.JCheckBoxMenuItem();
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);
cut.setText("Cut");
cut.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cutActionPerformed(evt);
}
});
jMenu1.add(cut);
selectAll.setText("SelectAll");
selectAll.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
selectAllActionPerformed(evt);
}
});
jMenu1.add(selectAll);
viewLineNumbers.setSelected(true);
viewLineNumbers.setText("ViewLineNumbers");
viewLineNumbers.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
viewLineNumbersActionPerformed(evt);
}
});
jMenu1.add(viewLineNumbers);
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.DEFAULT_SIZE, 279, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
private void updateStatus(int linenumber, int columnnumber) {
status.setText("Line: " + linenumber + " Column: " + columnnumber);
}
private void openActionPerformed(java.awt.event.ActionEvent evt) {
FileDialog fd = new FileDialog(OpenDemo.this, "Select File", FileDialog.LOAD);
fd.show();
String title;
String sts;
File f;
if (fd.getFile() != null) {
sts = fd.getDirectory() + fd.getFile();
title=fd.getFile();
System.out.println(sts);
title=fd.getFile();
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(OpenDemo.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(OpenDemo.class.getName()).log(Level.SEVERE, null, ex);
}
String t = str.toString();
final JInternalFrame internalFrame = new JInternalFrame("",true,true);
textPane = new JTextPane();
textPane.setFont(new java.awt.Font("Miriam Fixed", 0, 13));
internalFrame.add(textPane);
i++;
internalFrame.setName("Doc "+i);
internalFrame.setTitle(title);
try {
internalFrame.setSelected(true);
} catch (PropertyVetoException ex) {
Logger.getLogger(OpenDemo.class.getName()).log(Level.SEVERE, null, ex);
}
textPane.addCaretListener(new CaretListener() {
@Override
public void caretUpdate(CaretEvent e) {
JTextPane editArea = (JTextPane)e.getSource();
int linenum = 1;
int columnnum = 1;
try {
int caretpos = editArea.getCaretPosition();
linenum=getLineAtCaret(editArea)-1;
columnnum=getColumnAtCaret(editArea);
linenum += 1;
}
catch(Exception ex) { }
updateStatus(linenum, columnnum);
}
});
status=new JTextField();
status.setBackground(Color.LIGHT_GRAY);
internalFrame.add(status,BorderLayout.SOUTH);
tp.add(internalFrame);
scrollPane=new JScrollPane(textPane);
tp.setSelectedIndex(i-1);
internalFrame.add(scrollPane);
internalFrame.setVisible(true);
textPane.setText(t);
tp.addChangeListener(this);
textPanes.add(textPane);
textPane.setCaretPosition(0);
}
}
private void cutActionPerformed(java.awt.event.ActionEvent evt) {
textPane.cut();
}
private void selectAllActionPerformed(java.awt.event.ActionEvent evt) {
textPane.selectAll();
}
private void viewLineNumbersActionPerformed(java.awt.event.ActionEvent evt) {
AbstractButton button = (AbstractButton) evt.getSource();
if(button.isSelected()){
lnNum = new JTextPane();
lnNum.setEditable(false);
lnNum.setSize(50,50);
lnNum.setFont(new java.awt.Font("Miriam Fixed", 0, 13));
lnNum.setText(getText());
textPane.getDocument().addDocumentListener(new DocumentListener(){
@Override
public void changedUpdate(DocumentEvent de) {
lnNum.setText(getText());
}
@Override
public void insertUpdate(DocumentEvent de) {
lnNum.setText(getText());
}
@Override
public void removeUpdate(DocumentEvent de) {
lnNum.setText(getText());
}
});
scrollPane.getViewport().add(textPane);
scrollPane.setRowHeaderView(lnNum);
}
else{
scrollPane.setRowHeaderView(null);
}
}
public String getText(){
int caretPosition = textPane.getDocument().getLength();
javax.swing.text.Element root = textPane.getDocument().getDefaultRootElement();
String text = "1" + System.getProperty("line.separator");
for(int i = 2; i <= root.getElementIndex(caretPosition) + 1; i++){
text += i + System.getProperty("line.separator");
}
return text;
}
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(OpenDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(OpenDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(OpenDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(OpenDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new OpenDemo().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JMenuItem cut;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem open;
private javax.swing.JMenuItem selectAll;
private javax.swing.JTabbedPane tp;
private javax.swing.JCheckBoxMenuItem viewLineNumbers;
// End of variables declaration
@Override
public void stateChanged(ChangeEvent ce) {
JTabbedPane sourceTabbedPane = (JTabbedPane) ce.getSource();
int index = sourceTabbedPane.getSelectedIndex();
try
{
textPane =textPanes.get(index);
}
catch(IndexOutOfBoundsException e1){
}
}
public int getLineAtCaret(JTextComponent component)
{
int caretPosition = component.getCaretPosition();
Element root = component.getDocument().getDefaultRootElement();
return root.getElementIndex(caretPosition) + 1;
}
public int getColumnAtCaret(JTextComponent component)
{
FontMetrics fm = component.getFontMetrics(component.getFont());
int characterWidth = fm.stringWidth("0");
int column = 0;
try
{
Rectangle r = component.modelToView(component.getCaretPosition());
int width = r.x - component.getInsets().left;
column = width/characterWidth;
}
catch(BadLocationException ble) {}
return column + 1;
}
}
如何** Cut **操作應該適用於多個文檔?我無法想象剪貼板應該是什麼? – StanislavL
你看過[內部幀](http://docs.oracle.com/javase/tutorial/uiswing/components/internalframe.html)嗎? –
@Catalina島,哈我已經證明,但它不工作,請幫助我。 – user3709795