如何在java中为复选框文本编写上标单词?

时间:2010-10-13 16:35:28

标签: java swing jcheckbox superscript

我在Java中使用swing制作了一些复选框。 我想为复选框写一个上标文本,但我不确定如何。

代码目前看起来像这样。

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

我想在JCheckBox参数的上标中加上“+”符号。 有什么简单的方法可以做到这一点?

提前谢谢。

2 个答案:

答案 0 :(得分:4)

Java按钮在其文本中支持html。您需要稍微改变字符串的格式。试试这个:

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

答案 1 :(得分: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
            );
        }
    }
}

我希望它会对你有所帮助:)。