日期:2014-05-20  浏览次数:20848 次

Java Swing JCheckBox中的复选框的边框怎样去除?
如题,

Java Swing JCheckBox中的复选框的边框怎样去除?

Java JCheckBox中的复选框有一个四方形的边框,选中的话就是一个吊钩。现在只想有一个吊钩,不想要那个边框,请问哪位大侠能给出解决方案?

是JCheckBox类有专门的UI方法更改,还是必须继承JCheckBox类然后重写一些方法?

------解决方案--------------------
Java code

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.AbstractButton;
import javax.swing.ButtonModel;
import javax.swing.Icon;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.SwingUtilities;
import javax.swing.plaf.basic.BasicHTML;
import javax.swing.plaf.metal.MetalCheckBoxUI;
import javax.swing.plaf.metal.MetalLookAndFeel;
import javax.swing.text.View;

import sun.swing.SwingUtilities2;

public class C extends JDialog {

    /**
     * Launch the application
     * @param args
     */
    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    C dialog = new C();
                    dialog.addWindowListener(new WindowAdapter() {
                        public void windowClosing(WindowEvent e) {
                            System.exit(0);
                        }
                    });
                    dialog.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the dialog
     */
    public C() {
        super();
        getContentPane().setLayout(null);
        setBounds(100, 100, 500, 375);

        final JCheckBox checkBox = new JCheckBox();
        checkBox.setText("New JCheckBox");
        checkBox.setBounds(87, 57, 118, 26);
        getContentPane().add(checkBox);
        //
        MetalCheckBoxUI ui = new MetalCheckBoxUI(){
            public Icon getDefaultIcon() {
                return new ColumnControlIcon();
            }

            public synchronized void paint(Graphics g, JComponent c) {

                AbstractButton b = (AbstractButton) c;
                ButtonModel model = b.getModel();

                Dimension size = c.getSize();

                int w = size.width;
                int h = size.height;

                Font f = c.getFont();
                g.setFont(f);
                FontMetrics fm = SwingUtilities2.getFontMetrics(c, g, f);

                Rectangle viewRect = new Rectangle(size);
                Rectangle iconRect = new Rectangle();
                Rectangle textRect = new Rectangle();

                Insets i = c.getInsets();
                viewRect.x += i.left;
                viewRect.y += i.top;
                viewRect.width -= (i.right + viewRect.x);
                viewRect.height -= (i.bottom + viewRect.y);
                Icon altIcon = b.getIcon();
                String text = SwingUtilities.layoutCompoundLabel(c, fm, b
                        .getText(), altIcon != null ? altIcon
                        : getDefaultIcon(), b.getVerticalAlignment(), b
                        .getHorizontalAlignment(), b.getVerticalTextPosition(),
                        b.getHorizontalTextPosition(), viewRect, iconRect,
                        textRect, b.getIconTextGap());

                // fill background
                if (c.isOpaque()) {
                    g.setColor(b.getBackground());
                    g.fillRect(0, 0, size.width, size.height);
                }

                // Paint the radio button
                if (altIcon != null) {

                    if (!model.isEnabled()) {
                        if (model.isSelected()) {
                            altIcon = b.getDisabledSelectedIcon();
                        } else {
                            altIcon = b.getDisabledIcon();
                        }
                    } else if (model.isPressed() && model.isArmed()) {
                        altIcon = b.getPressedIcon();
                        if (altIcon == null) {
                            // Use selected icon
                            altIcon = b.getSelectedIcon();
                        }
                    } else if (model.isSelected()) {
                        if (b.isRolloverEnabled() && model.isRollover()) {
                            altIcon = (Icon) b.getRolloverSelectedIcon();
                            if (altIcon == null) {
                                altIcon = (Icon) b.getSelectedIcon();
                            }
                        } else {
                            altIcon = (Icon) b.getSelectedIcon();
                        }
                    } else if (b.isRolloverEnabled() && model.isRollover()) {
                        altIcon = (Icon) b.getRolloverIcon();
                    }

                    if (altIcon == null) {
                        altIcon = b.getIcon();
                    }

                    altIcon.paintIcon(c, g, iconRect.x, iconRect.y);

                } else {
                    if (model.isEnabled()) {
                        if (model.isPressed() && model.isArmed()) {
                            g.setColor(MetalLookAndFeel.getControlShadow());
                            g.fillRect(iconRect.x, iconRect.y, 13, 13);
                        }
                        g.setColor(c.getForeground());
                    } else {
                        g.setColor(MetalLookAndFeel.getControlShadow());
                    }

                    if (model.isSelected()) {
                        int controlSize = 13;
                        g.fillRect(iconRect.x + 3, iconRect.y + 5, 2,
                                controlSize - 8);
                        g.drawLine(iconRect.x + (controlSize - 4),
                                iconRect.y + 3, iconRect.x + 5, iconRect.y
                                        + (controlSize - 6));
                        g.drawLine(iconRect.x + (controlSize - 4),
                                iconRect.y + 4, iconRect.x + 5, iconRect.y
                                        + (controlSize - 5));
                    }
                }

                // Draw the Text
                if (text != null) {
                    View v = (View) c.getClientProperty(BasicHTML.propertyKey);
                    if (v != null) {
                        v.paint(g, textRect);
                    } else {
                        int mnemIndex = b.getDisplayedMnemonicIndex();
                        if (model.isEnabled()) {
                            // *** paint the text normally
                            g.setColor(b.getForeground());
                        } else {
                            // *** paint the text disabled
                            g.setColor(getDisabledTextColor());
                        }
                        SwingUtilities2.drawStringUnderlineCharAt(c, g, text,
                                mnemIndex, textRect.x, textRect.y
                                        + fm.getAscent());
                    }
                    if (b.hasFocus() && b.isFocusPainted()
                            && textRect.width > 0 && textRect.height > 0) {
                        paintFocus(g, textRect, size);
                    }
                }
            }
        };
        checkBox.setUI(ui);
        System.out.println();
    }

}