嘿,我有一些问题。
我必须创建一个文本编辑的小程序。应设置(选定)文本的样式。粗体、斜体、下划线、右-左-中对齐。它工作得很好。我使用了特定的StyleEditorKit操作。
我现在的问题是,这些操作是通过j工具栏中的按钮和jmenubar /jmenubar中的jmenuitem来触发的。
因此,有两个click元素用于设置文本粗体,两个元素用于设置文本斜体,依此类推。如果单击了一个元素(例如工具栏中的按钮),则jmenuitem也应该被选中/激活。但是我怎么才能意识到这一点呢?
我的想法是检查选定的文本(实现了CaretListener)。如果文本是粗体,则将按钮和菜单项设置为活动状态。但是如何获取selectedText是否为粗体/斜体等?
我想有一个带有叶子的StyledDocument树可以用来做这些事情。但是我怎么才能得到这棵树呢?我怎样才能得到叶子呢?
这是我的第一步:
jTextPane1.addCaretListener(new CaretListener() {
@Override
public void caretUpdate(CaretEvent e) {
Highlight[] h = jTextPane1.getHighlighter().getHighlights();
for(int i = 0; i < h.length; i++) {
System.out.println(h[i].getStartOffset());
System.out.println(h[i].getEndOffset());
String selectedText = jTextPane1.getSelectedText();
StyledDocument styleddoc = (StyledDocument) jTextPane1.getDocument();
System.out.println(styleddoc);
}
}
});但我只得到javax.swing.text.DefaultStyledDocument@5098cb76
如何遍历树并获得leafs /粗体或斜体元素?
谢谢
发布于 2016-05-11 23:47:23
如何获取selectedText是否为粗体/斜体等?
在您的CaretListener中,您可以使用:
AttributeSet attributes = jTextPane1.getCharacterAttributes();
System.out.println( attributes.containsAttribute(StyleConstants.Bold, Boolean.TRUE) );如果单击了一个元素(例如工具栏中的按钮),则jmenuitem也应该被选中/激活。
您应该使用Action:
Action bold = new StyledEditorKit.BoldAction();
JButton boldButton = new JButton( bold );
JCheckBoxMenuItem boldMenuItem = new JCheckBoxMenuItem( bold );设置Action的状态时,使用该Action的所有组件的状态都会更改。例如,您可以使Action处于选中或启用状态。
发布于 2016-05-12 00:14:36
我的代码现在看起来像这样
public void jToolBarInitButtons() {
jTextPane1.addCaretListener(new CaretListener() {
@Override
public void caretUpdate(CaretEvent e) {
StyledDocument styleddoc = (StyledDocument) jTextPane1.getDocument();
AttributeSet attributes = jTextPane1.getCharacterAttributes();
System.out.println("bold " + attributes.containsAttribute(StyleConstants.Bold, Boolean.TRUE));
System.out.println("italic " + attributes.containsAttribute(StyleConstants.Italic, Boolean.TRUE));
}发布于 2016-05-12 00:24:14
抱歉的。下面是完整的代码:
更新2:代码如下
public class TestFrame extends javax.swing.JFrame {
/**
* Creates new form TestFrame
*/
public TestFrame() {
initComponents();
initButtons();
}
Action boldAction = new StyledEditorKit.BoldAction();
Action italicAction = new StyledEditorKit.ItalicAction();
Action underlineAction = new StyledEditorKit.UnderlineAction();
public void initButtons() {
JButton boldButton = new JButton(boldAction);
boldButton.setText("bold");
JButton italicButton = new JButton(italicAction);
boldButton.setText("italic");
JButton underlineButton = new JButton(underlineAction);
boldButton.setText("underline");
jToolBar1.add(boldButton);
jToolBar1.add(italicButton);
jToolBar1.add(underlineButton);
jTextPane1.addCaretListener(new CaretListener() {
@Override
public void caretUpdate(CaretEvent e) {
Highlighter.Highlight[] h = jTextPane1.getHighlighter().getHighlights();
for(int i = 0; i < h.length; i++) {
System.out.println("length " + h.length);
System.out.println(h[i].getStartOffset());
System.out.println(h[i].getEndOffset());
String selectedText = jTextPane1.getSelectedText();
StyledDocument styleddoc = (StyledDocument) jTextPane1.getDocument();
AttributeSet attributes = jTextPane1.getCharacterAttributes();
System.out.println("Bold " + attributes.containsAttribute(StyleConstants.Bold, Boolean.TRUE));
System.out.println("Italic " + attributes.containsAttribute("Italic " + StyleConstants.Italic, Boolean.TRUE));
System.out.println("Underline " + attributes.containsAttribute(StyleConstants.Underline, Boolean.TRUE));
}
}
});
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTextPane1 = new javax.swing.JTextPane();
jToolBar1 = new javax.swing.JToolBar();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jScrollPane1.setViewportView(jTextPane1);
jToolBar1.setRollover(true);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(116, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 269, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(75, 75, 75))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(23, 23, 23)
.addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(14, 14, 14)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(89, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
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(TestFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TestFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TestFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TestFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TestFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextPane jTextPane1;
private javax.swing.JToolBar jToolBar1;
// End of variables declaration
}https://stackoverflow.com/questions/37166893
复制相似问题