我想更改选项卡的名称保存创建的文档后,当我创建新文档时,光标未放置/焦点在TextArea中,当我点击textarea时只有它如何创建自动对焦Textarea.In我的应用程序,当我创建一个新的文档,它打开一个空Doc与Doc 1 /文档2 /文档3的Tab名称....当我输入一个文本和通过“保存”菜单保存项目,我想用文件Name替换Doc 1。请检查两者。谢谢。如何更改JTabbedPane中的选项卡名称
我的代码:
public class TabbedPaneFocus extends javax.swing.JFrame {
JTextArea textArea;
int i=0;
JTabbedPane tabbedPane;
public TabbedPaneFocus() {
initComponents();
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();
create = new javax.swing.JMenuItem();
save = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jMenu1.setText("File");
create.setText("Create");
create.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
createActionPerformed(evt);
}
});
jMenu1.add(create);
save.setText("Save");
save.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
saveActionPerformed(evt);
}
});
jMenu1.add(save);
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 createActionPerformed(java.awt.event.ActionEvent evt) {
try{
i++;
textArea = new JTextArea();
textArea.setFont(new java.awt.Font("Miriam Fixed", 0, 13));
JScrollPane scrollpane=new JScrollPane(textArea);
tabbedPane.add(scrollpane);
tabbedPane.addTab("Doc "+i, scrollpane);
tabbedPane.setSelectedIndex(i-1);
tabbedPane.setFocusable(true);
}
catch(ArrayIndexOutOfBoundsException aio){
}
}
private void saveActionPerformed(java.awt.event.ActionEvent evt) {
int chooserStatus;
String filename = null;
int index=tabbedPane.getSelectedIndex();
String name=tabbedPane.getTitleAt(index);
if(name.isEmpty() || name.startsWith("Doc ")){
JFileChooser chooser = new JFileChooser();
chooser.setPreferredSize(new Dimension(450, 400));
chooserStatus = chooser.showSaveDialog(this);
if (chooserStatus == JFileChooser.APPROVE_OPTION) {
File selectedFile = chooser.getSelectedFile();
if (!selectedFile.getName().endsWith(".txt")) {
selectedFile = new File(selectedFile.getAbsolutePath() + ".txt");
}
filename = selectedFile.getPath();
tabbedPane.setTitleAt(index, selectedFile.getName());
}
else{
return;
}
}
boolean success;
String editorString;
FileWriter fwriter;
PrintWriter outputFile;
try {
DataOutputStream d = new DataOutputStream(new FileOutputStream(filename));
String line = textArea.getText();
BufferedReader br = new BufferedReader(new StringReader(line));
while((line = br.readLine())!=null) {
d.writeBytes(line + "\r\n");
}
}
catch (IOException e) {
success = false;
}
success = true;
}
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);
}
});
}
// Variables declaration - do not modify
private javax.swing.JMenuItem create;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem save;
// End of variables declaration
public class CloseButtonTabbedPane extends JTabbedPane {
public CloseButtonTabbedPane() {
}
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));
}
public void addTab(String title, Icon icon, Component component) {
addTab(title, icon, component, null);
}
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() {
int index;
public void mouseClicked(MouseEvent e) {
tabbedPane.remove(tabbedPane.getSelectedIndex());
i--;
}
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.RED, 1));
}
public void mouseExited(MouseEvent e) {
JButton button = (JButton) e.getSource();
button.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 1));
}
});
add(button);
}
}
}
}
In My SaveAction code I write the code to change the tab name with saved file name but not apply.
tabbedPane.setTitleAt(index, selectedFile.getName());
对不起,我知道英语不是每个人的第一语言,但我不知道这里实际提出了什么。请尝试澄清你想知道的内容。 – BarrySW19 2014-11-25 10:43:33
好的,没问题。我有创建和保存菜单Items.When我点击创建菜单项它打开一个文档与文档1.然后我写在打开的文档上的文本,然后我保存我的文档有一些名称。然后,我想用给定的文件/文档名称替换标签名称Doc 1。 – user3912886 2014-11-25 11:55:37