我想为所有已打开的文档处理所有菜单项功能。在我的应用程序中它仅适用于最近打开的文档。例如:我打开两个文件/文档(比如Document1和Document2)。我的应用程序是仅适用于Document2(最近)。如果我使用Document1并尝试应用菜单项功能,它不适用于此。因为它是较旧的。在我的应用程序中我添加两个菜单项。一个打开 - - >打开文件,另一个是ViewSpace - >用于检查功能是否适用于所有打开的文档。请检查我的应用程序并帮助我。谢谢。
我的代码: 主类:
public class TabbedPaneFocus extends javax.swing.JFrame {
JTextArea tx;
int i=0;
JTabbedPane tabbedPane;
public TabbedPaneFocus() {
initComponents();
viewSpace.setSelected(false);
tabbedPane=new CloseButtonTabbedPane();
add(tabbedPane);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tabbedPane, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 512, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 366, Short.MAX_VALUE)
);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
open = new javax.swing.JMenuItem();
viewSpace = 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);
viewSpace.setSelected(true);
viewSpace.setText("ViewSpace");
viewSpace.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
viewSpaceActionPerformed(evt);
}
});
jMenu1.add(viewSpace);
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)
.addGap(0, 512, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 366, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
private void openActionPerformed(java.awt.event.ActionEvent evt) {
final JFileChooser jc = new JFileChooser();
int returnVal= jc.showOpenDialog(TabbedPaneFocus.this);
String title;
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("");
try {
br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
str.append(line + "\n");
}
}
catch (IOException ex) {
Logger.getLogger(tabbedpaneDemo.class.getName()).log(Level.SEVERE, null, ex);
}
String t = str.toString();
title=file.getName();
tx.setFont(new java.awt.Font("Miriam Fixed", 0, 13));
JScrollPane scrollpane=new JScrollPane(tx);
tabbedPane.addTab(title, scrollpane);
i++;
tabbedPane.setSelectedIndex(i-1);
tx.setText(t);
tx.setCaretPosition(0);
}
}
private void viewSpaceActionPerformed(java.awt.event.ActionEvent evt) {
AbstractButton button = (AbstractButton) evt.getSource();
String previous=tx.getText();
if(button.isSelected()){
previous=previous.replaceAll(" ",".");
tx.setText(previous);
tx.setCaretPosition(0);
}
else{
String str=tx.getText();
str=str.replaceAll("\\."," ");
tx.setText(str);
tx.setCaretPosition(0);
}
}
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(TabbedPaneFocus.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TabbedPaneFocus.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TabbedPaneFocus.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TabbedPaneFocus.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new TabbedPaneFocus().setVisible(true);
}
});
}
private javax.swing.JMenu jMenu1;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem open;
private javax.swing.JCheckBoxMenuItem viewSpace;
}
CloseButtonTabbedPane.java
public class CloseButtonTabbedPane extends JTabbedPane {
public CloseButtonTabbedPane() {
}
@Override
public void addTab(String title, Icon icon, Component component, String tip) {
super.addTab(title, icon, component, tip);
int count = this.getTabCount() - 1;
setTabComponentAt(count, new CloseButtonTab(component, title, icon));
}
@Override
public void addTab(String title, Icon icon, Component component) {
addTab(title, icon, component, null);
}
@Override
public void addTab(String title, Component component) {
addTab(title, null, component);
}
public class CloseButtonTab extends JPanel {
private Component tab;
public CloseButtonTab(final Component tab, String title, Icon icon) {
this.tab = tab;
setOpaque(false);
FlowLayout flowLayout = new FlowLayout(FlowLayout.CENTER, 3, 3);
setLayout(flowLayout);
setVisible(true);
JLabel jLabel = new JLabel(title);
jLabel.setIcon(icon);
add(jLabel);
JButton button = new JButton(MetalIconFactory.getInternalFrameCloseIcon(16));
button.setMargin(new Insets(0, 0, 0, 0));
button.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 1));
button.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
JTabbedPane tabbedPane = (JTabbedPane) getParent().getParent();
tabbedPane.remove(tab);
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
JButton button = (JButton) e.getSource();
button.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1));
}
public void mouseExited(MouseEvent e) {
JButton button = (JButton) e.getSource();
button.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 1));
}
});
add(button);
}
}
}
答案 0 :(得分:0)
。在我的应用程序中它仅适用于最近打开的文档。
那是因为你的&#34; tx&#34;变量总是引用最近创建的JTextArea。
对于您的菜单项,您应该使用Action
,然后通过扩展Action
来创建TextAction
。 TextAction
有一个方法,允许您访问最后具有焦点的文本组件。然后你就这个组件进行处理:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
public class TextFieldSelectAll
{
private static void createAndShowGUI()
{
JPanel north = new JPanel();
north.add( new JTextField("one", 10) );
north.add( new JTextField("two", 10) );
north.add( new JTextField("three", 10) );
JButton selectAll = new JButton( new SelectAll() );
JFrame frame = new JFrame("SSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(north, BorderLayout.NORTH);
frame.add(selectAll, BorderLayout.SOUTH);
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );
}
static class SelectAll extends TextAction
{
public SelectAll()
{
super("Select All");
}
public void actionPerformed(ActionEvent e)
{
JTextComponent component = getFocusedComponent();
component.selectAll();
component.requestFocusInWindow();
}
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
答案 1 :(得分:0)
正如camickr已经解释的那样,你的&#34; tx&#34;字段始终引用最近创建的文本区域。此外,当您切换标签时,您需要确保所选文本区域根据当前&#34;查看空格&#34;设置。
我认为可以通过进行以下更改来解决这两个问题:
在代码中:
// Change 1 (before the constructor).
private final List<JTextArea> textAreas = new ArrayList<>();
// Change 2 (after assigning the tabbedPane field in the constructor).
tabbedPane.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(final ChangeEvent changeEvent) {
setDocumentContents();
}
});
// Change 3 (after creating a text area in the openActionPerformed method).
textAreas.add(tx);
// Change 4 (replacing the viewSpaceActionPerformed method).
private void setDocumentContents() {
final JTextArea textArea = textAreas.get(tabbedPane.getSelectedIndex());
String previous = textArea.getText();
if (viewSpace.isSelected()) {
previous = previous.replaceAll(" ", ".");
} else {
previous = previous.replaceAll("\\.", " ");
}
textArea.setText(previous);
textArea.setCaretPosition(0);
}