2010-10-13 23 views
2

J'ai fait quelques cases en utilisant swing en Java. Je veux écrire un texte en exposant pour les cases à cocher, mais je ne sais pas comment.Comment écrire un mot en exposant pour le texte de la case à cocher dans java?

Le code ressemble à ceci.

JCheckBox hCheckBox = new JCheckBox("[M + H]+"); 

Je veux avoir signe "+" à l'intérieur du paramètre JCheckBox en exposant. Qu'est-ce qu'un moyen facile de faire cela?

Merci d'avance.

Répondre

4

Les boutons Java prennent en charge html dans leur texte. Vous devez cependant formater la chaîne différemment. Essayez ceci:

JCheckBox hCheckBox = new JCheckBox("<html>[M + H]<sup>+</sup></html>"); 
+0

Merci beaucoup! C'était facile, on dirait que je cherchais ça à la dure en passant par les bibliothèques haha. – js0823

0
 package infonode; 

     /** 
     * 
    * @author Deepak 
    */ 
    import java.awt.*; 
    import java.awt.event.*; 

    import javax.swing.*; 
    import javax.swing.border.*; 
    import javax.swing.table.*; 
    import javax.swing.text.*; 

    public class CoolTable extends AbstractTableModel{ 


    public static void main(String[] arg){ 
     JFrame f = new JFrame("Cool Table"); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     JTable t = new JTable(new CoolTable()); 
     t.setDefaultRenderer(Object.class, new StyledCellRenderer()); 
     t.setDefaultEditor(Object.class, new StyledCellEditor()); 
     t.setRowHeight(50); 

     System.out.println(t.isCellEditable(0, 0)); 

     f.add(new JScrollPane(t)); 
     f.pack(); 
     f.setVisible(true); 
    } 

    private Object[] data; 

    /** Creates a new instance of CoolTable */ 
    public CoolTable() { 
     final String[] dataSrc = "This is a test".split(" "); 
     data = new Object[dataSrc.length]; 
     System.arraycopy(dataSrc, 0, data, 0, data.length); 
    } 

    public int getRowCount() { return data.length; } 
    public int getColumnCount() { return 1; } 
    public boolean isCellEditable(int r, int c) { return true; } 

    public Object getValueAt(int r, int c){ 
     if(data[r] instanceof String) { 
      try{ 
       DefaultStyledDocument doc = new DefaultStyledDocument(); 
       doc.insertString(0, data[r].toString(), null); 
       data[r] = doc; 
      } catch(BadLocationException x) { 
       x.printStackTrace(System.err); 
       System.exit(1); 
      } 
     } 

     return data[r]; 
    } 

    public void setValueAt(int r, int c, Object val){ 
     data[r] = val instanceof DefaultStyledDocument ? val : val.toString(); 
    } 

    public static class StyledCellRenderer implements TableCellRenderer { 
     private static Border etchedBorder = BorderFactory.createEtchedBorder(); 

     private JTextComponent styledRenderer = new JTextPane(); 

     public Component getTableCellRendererComponent(JTable table, Object value, 
              boolean isSelected, boolean hasFocus, 
              int row, int column) { 

      styledRenderer.setDocument((Document)value); 
      styledRenderer.setBackground(isSelected ? Color.LIGHT_GRAY : Color.WHITE); 
      styledRenderer.setBorder(hasFocus ? etchedBorder : null); 
      return styledRenderer; 
     } 
    } 

    public static class StyledCellEditor extends DefaultCellEditor { 
     private JTextPane styledEditor = new JTextPane(); 

     public StyledCellEditor() { 
      super(new JTextField()); 
      editorComponent = new JScrollPane(styledEditor); 

      installActions(); 

      delegate = new DefaultCellEditor.EditorDelegate() { 
       public void setValue(Object val) { 
        styledEditor.setDocument((Document)val); 
       } 

       public Object getCellEditorValue() { return styledEditor.getDocument(); } 
      }; 

      styledEditor.addFocusListener(new FocusAdapter(){ 
       public void focusLoast(FocusEvent e){ delegate.stopCellEditing(); } 
      }); 
     } 

     private void installActions() { 
      styledEditor.getInputMap().put(
       KeyStroke.getKeyStroke(KeyEvent.VK_UP, InputEvent.ALT_MASK), 
       "superscript" 
      ); 
      styledEditor.getInputMap().put(
       KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, InputEvent.ALT_MASK), 
       "subscript" 
      ); 
      styledEditor.getInputMap().put(
       KeyStroke.getKeyStroke(KeyEvent.VK_B, InputEvent.CTRL_MASK), 
       "bold" 
      ); 

      styledEditor.getActionMap().put("superscript", new SuperscriptAction(true)); 
      styledEditor.getActionMap().put("subscript", new SuperscriptAction(false)); 
      styledEditor.getActionMap().put("bold", new StyledEditorKit.BoldAction()); 
     } 
    } 

    private static class SuperscriptAction extends AbstractAction { 
     private MutableAttributeSet superscript = new SimpleAttributeSet(); 

     public SuperscriptAction() { 
      this(true); 
     } 

     public SuperscriptAction(boolean isSuperscript) { 
      StyleConstants.setSuperscript(superscript, isSuperscript); 
     } 

     public void actionPerformed(ActionEvent e) { 
      final JTextPane styledEditor = (JTextPane)e.getSource(); 

      styledEditor.getStyledDocument().setCharacterAttributes(
       styledEditor.getSelectionStart(), 
       styledEditor.getSelectionEnd() - styledEditor.getSelectionStart(), 
       superscript, 
       false 
      ); 
     } 
    } 
} 

J'espère que cela vous aidera :)