Java Swing JCheckBox中的复选框的边框怎样去除? 如题,Java Swing JCheckBox中的复选框的边框怎样去除?Java JCheckBox中的复选框有一个四方形的边框,选中的话就是一个吊钩。现在只想有一个吊钩,不想要那个边框,请问哪位大侠能给出解决方案?是JCheckBox类有专门的UI方法更改,还是必须继承JCheckBox类然后重写一些方法? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 CheckBox.setFocusPainted(false); Java Swing JCheckBox中的复选框的边框怎样去除?.....\求解答 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(); }} 哦,谢了。我看了一遍,您的意思是重写了UI中的getDefaultIcon()和paint()方法。我会试试。。 package com.han;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 CheckBoxCustomized extends JDialog { /** * */ private static final long serialVersionUID = 5925267340818484608L; /** * Launch the application * @param args */ public static void main(String args[]) { EventQueue.invokeLater(new Runnable() { public void run() { try { CheckBoxCustomized dialog = new CheckBoxCustomized(); dialog.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); dialog.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the dialog */ public CheckBoxCustomized() { 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 synchronized void paint(Graphics g, JComponent c) { AbstractButton b = (AbstractButton) c; ButtonModel model = b.getModel(); Dimension size = c.getSize(); 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 = b.getRolloverSelectedIcon(); if (altIcon == null) { altIcon = b.getSelectedIcon(); } } else { altIcon = b.getSelectedIcon(); } } else if(b.isRolloverEnabled() && model.isRollover()) { altIcon = b.getRolloverIcon(); } if(altIcon == null) { altIcon = b.getIcon(); } altIcon.paintIcon(c, g, iconRect.x, iconRect.y); } else { System.out.println("here"); // paint the button icon by myself 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(checkBox.isFocusPainted()); checkBox.setFocusPainted(false); // this is optional System.out.println(); }} Java中关于字符串的一道面试题 一道很简单的IO问题(java.io.EOFException) 新手问题 struts的<forward>迁移到别的网站 iReport 如何执行多条SQL语句 如何在已经绘制好的board上通过后来运行的函数画一条线段? jar路径问题。 你是高手的话,请看这里!!! 一个关于FileWriter的问题 利用String的getBytes方法,怎么得到的结果是这样? 菜鸟 请教一个问题 关于线程的!~ 高分:如何把数组传递到新打开的窗口(不是对话框)
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();
}}
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 CheckBoxCustomized extends JDialog { /**
*
*/
private static final long serialVersionUID = 5925267340818484608L; /**
* Launch the application
* @param args
*/
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CheckBoxCustomized dialog = new CheckBoxCustomized();
dialog.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
} /**
* Create the dialog
*/
public CheckBoxCustomized() {
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 synchronized void paint(Graphics g, JComponent c) { AbstractButton b = (AbstractButton) c;
ButtonModel model = b.getModel(); Dimension size = c.getSize();
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 = b.getRolloverSelectedIcon();
if (altIcon == null) {
altIcon = b.getSelectedIcon();
}
} else {
altIcon = b.getSelectedIcon();
}
} else if(b.isRolloverEnabled() && model.isRollover()) {
altIcon = b.getRolloverIcon();
} if(altIcon == null) {
altIcon = b.getIcon();
} altIcon.paintIcon(c, g, iconRect.x, iconRect.y); } else {
System.out.println("here");
// paint the button icon by myself
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(checkBox.isFocusPainted());
checkBox.setFocusPainted(false); // this is optional
System.out.println();
}}