我猜你的意思是 RoundedCornerBorder:
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.plaf.basic.*;
public class RoundedComboBoxTest {
public JComponent makeUI() {
UIManager.put("Panel.background", Color.GRAY);
UIManager.put("ComboBox.foreground", Color.BLACK);
UIManager.put("ComboBox.background", Color.GRAY);
UIManager.put("ComboBox.selectionForeground", Color.WHITE);
UIManager.put("ComboBox.selectionBackground", Color.GRAY);
UIManager.put("ComboBox.buttonDarkShadow", Color.BLACK);
UIManager.put("ComboBox.border", new RoundedCornerBorder());
DefaultComboBoxModel<String> m = new DefaultComboBoxModel<>();
m.addElement("1234");
m.addElement("5555555555555555555555");
m.addElement("6789000000000");
JComboBox<String> combo = new JComboBox<>(m);
combo.setUI(new BasicComboBoxUI() {
@Override protected JButton createArrowButton() {
JButton b = super.createArrowButton();
b.setContentAreaFilled(false);
b.setBackground(Color.GRAY);
b.setBorder(BorderFactory.createEmptyBorder());
return b;
}
});
Object o = combo.getAccessibleContext().getAccessibleChild(0);
((JComponent)o).setBorder(
BorderFactory.createMatteBorder(0,1,1,1,Color.BLACK));
JPanel p = new JPanel(new BorderLayout());
p.add(combo, BorderLayout.NORTH);
p.setOpaque(true);
p.setBackground(Color.GRAY);
p.setBorder(BorderFactory.createEmptyBorder(15,15,15,15));
return p;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new RoundedComboBoxTest().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
class RoundedCornerBorder extends AbstractBorder {
@Override public void paintBorder(
Component c, Graphics g, int x, int y, int width, int height) {
Graphics2D g2 = (Graphics2D)g.create();
g2.setRenderingHint(
RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int r = 12;
Area round = new Area(
new RoundRectangle2D.Float(x, y, width-1, height-1, r, r));
Rectangle b = round.getBounds();
b.setBounds(b.x, b.y + r, b.width, b.height - r);
round.add(new Area(b));
Container parent = c.getParent();
if(parent!=null) {
g2.setColor(parent.getBackground());
Area corner = new Area(new Rectangle2D.Float(x, y, width, height));
corner.subtract(round);
g2.fill(corner);
}
g2.setColor(Color.BLACK);
g2.draw(round);
g2.dispose();
}
@Override public Insets getBorderInsets(Component c) {
return new Insets(4, 8, 4, 8);
}
@Override public Insets getBorderInsets(Component c, Insets insets) {
insets.left = insets.right = 8;
insets.top = insets.bottom = 4;
return insets;
}
}