看起来我正在寻求帮助来禁用 Java Swing 中 JComboBox 内的蓝色焦点指示器效果。我想在选择 JComboBox 时删除焦点的视觉指示(蓝色边框),我想我可以通过使用自定义 UI 来实现此目的。我不知道我该怎么做:
JComboBox<Integer> day = new JComboBox<>();
for (int i = 1; i <= 31; i++) {
day.addItem(i);
}
day.setForeground(Color.LIGHT_GRAY);
day.setFont(new Font("SansSerif", Font.PLAIN, 11));
day.setFocusable(false);
day.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
day.setBorder(new RoundBorder());
day.setBackground(Color.WHITE);
day.setBounds(40, 192, 73, 40);
form.add(day);
public class RoundBorder extends AbstractBorder {
private static final long serialVersionUID = 1L;
@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);
RoundRectangle2D round = new RoundRectangle2D.Float(x, y, width - 1, height - 1, 8, 8);
g2.setColor(new Color(0xe9eaec, false));
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.set(4, 8, 4, 8);
return insets;
}
}
在这里找到了答案。由于该链接将来可能会消失,因此以下是其本质。
问题:
我真正想要的是知道如何访问箭头按钮左侧框中绘制的边框,并能够将该复合边框(在 Metal L&F 中)更改为简单的软线边框。
答案:
也许是这样的?
cb.setUI(new MetalComboBoxUI() {
public void paintCurrentValueBackground(Graphics g, Rectangle bounds, boolean hasFocus) {
g.setColor(MetalLookAndFeel.getControlDarkShadow());
g.drawRect(bounds.x, bounds.y, bounds.width, bounds.height - 1);
g.setColor(((BasicComboBoxRenderer) comboBox.getRenderer()).getBackground());
g.drawRect(bounds.x + 1, bounds.y + 1, bounds.width - 2, bounds.height - 3);
}
});
这是根据您问题中的代码改编的 SSCCE,其中包含上述代码:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.ComponentOrientation;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.geom.RoundRectangle2D;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.AbstractBorder;
import javax.swing.plaf.metal.MetalComboBoxUI;
import javax.swing.plaf.metal.MetalLookAndFeel;
public class ComBordr implements Runnable {
public void run() {
showGui();
}
private JPanel createButton() {
JButton button = new JButton("Exit");
JPanel panel = new JPanel();
panel.add(button);
return panel;
}
private JComboBox<Object> createCombo() {
Object[] list = new Object[31];
for (int i = 0; i < 31; i++) {
list[i] = i + 1;
}
JComboBox<Object> day = new JComboBox<>(list);
day.setUI(new MetalComboBoxUI() {
public void paintCurrentValueBackground(Graphics g, Rectangle bounds, boolean hasFocus) {
g.setColor(MetalLookAndFeel.getControlDarkShadow());
g.drawRect(bounds.x, bounds.y, bounds.width, bounds.height - 1);
g.setColor(new Color(0xe9eaec, false));
g.drawRect(bounds.x + 1, bounds.y + 1, bounds.width - 2, bounds.height - 3);
}
});
day.setForeground(Color.LIGHT_GRAY);
day.setFont(new Font("SansSerif", Font.PLAIN, 11));
day.setFocusable(false);
day.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
day.setBorder(new RoundBorder());
day.setBackground(Color.WHITE);
return day;
}
private void showGui() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createCombo(), BorderLayout.PAGE_START);
frame.add(createButton(), BorderLayout.PAGE_END);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new ComBordr());
}
}
class RoundBorder extends AbstractBorder {
private static final long serialVersionUID = 1L;
@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);
RoundRectangle2D round = new RoundRectangle2D.Float(x, y, width - 1, height - 1, 8, 8);
g2.setColor(new Color(0xe9eaec, false));
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.set(4, 8, 4, 8);
return insets;
}
}
这是结果的屏幕截图。