【发布时间】:2014-07-23 09:36:16
【问题描述】:
我想在单击 View-spaces 复选框 MenuItem 时显示点而不是空格。我的问题是,当我单击复选框时,它会用点替换空格,但是,当我向 textarea 添加一些额外的文本并单击之后Viewspace JCheckBox menuitem,它只取了以前的文本并替换了。我试过了,请运行我的代码你可以很容易地理解这个问题。请给一些建议.....谢谢。 这是我的代码:
public class VisibleSpaces extends javax.swing.JFrame {
int i=0;
JTextArea tx,lnNum;
JScrollPane scrollpane;
public VisibleSpaces() {
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();
Create = new javax.swing.JMenuItem();
ViewSpace = new javax.swing.JCheckBoxMenuItem();
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);
ViewSpace.setSelected(true);
ViewSpace.setText("ViewSpaces");
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)
.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 ViewSpaceActionPerformed(java.awt.event.ActionEvent evt) {
ViewSpace.addItemListener(new ItemListener() {
String str=tx.getText();
String previous=str;
@Override
public void itemStateChanged(ItemEvent ie) {
if(ViewSpace.getState()){
tx.setText(previous.replace(" ","."));
}
else
tx.setText(str);
}
});
}
private void CreateActionPerformed(java.awt.event.ActionEvent evt) {
final JInternalFrame internalFrame = new JInternalFrame("");
i++;
internalFrame.setName("Document"+i);
internalFrame.setClosable(true);
internalFrame.setAutoscrolls(true);
tx=new JTextArea();
tx.setFont(new java.awt.Font("Miriam Fixed", 0, 13));
scrollpane=new JScrollPane(tx);
internalFrame.add(scrollpane);
tp.add(internalFrame);
internalFrame.setSize(internalFrame.getMaximumSize());
internalFrame.pack();
internalFrame.setVisible(true);
}
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
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(VisibleSpaces.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(VisibleSpaces.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(VisibleSpaces.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(VisibleSpaces.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new VisibleSpaces().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JMenuItem Create;
private javax.swing.JCheckBoxMenuItem ViewSpace;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JTabbedPane tp;
// End of variables declaration
}
【问题讨论】:
-
最好先了解一下 java 命名约定和代码样式。
-
PLAF 更改、菜单栏、内部框架.. 这些似乎都与问题无关,所以将它们去掉。为了尽快获得更好的帮助,请发布MCVE(最小完整且可验证的示例)。或者几乎是@ImmerAllein 评论的内容.. ;)
-
我运行了你的代码,但它工作正常。首先,我输入了一个“空格”并单击了 View-spaces 复选框 MenuItem,它变成了一个“点”。然后我输入了一个带有一些额外文本的“空格”(比如这个“abc”),然后单击 View-spaces 复选框 MenuItem ,它会根据您的要求进行填充。 (比如这个“.abc”)
-
感谢您的回复。不,它不起作用。例如写 (abc) 并单击 Viewspace checkbox menuitem 它是显示 (abc..) 然后取消选中并添加 (def) 并再次选中复选框 menuitem 结果是 (abc..) 只有不是 ( abc..def..)这是我的代码的问题。