2

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

代码目前看起来像这样。

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

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

先感谢您。

4

2 回答 2

4

Java 按钮在其文本中支持 html。不过,您需要稍微不同地格式化字符串。试试这个:

JCheckBox hCheckBox = new JCheckBox("<html>[M + H]<sup>+</sup></html>"); 
于 2010-10-13T16:39:55.113 回答
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
            );
        }
    }
}

我希望它会帮助你:)

于 2013-07-20T05:57:20.273 回答