gpt4 book ai didi

java - Swing - 删除选定的边框并更改 JComboBox 的箭头颜色

转载 作者:行者123 更新时间:2023-12-01 18:41:52 26 4
gpt4 key购买 nike

我正在尝试删除 JComboBox 的选定边框(顶部箭头)并更改箭头颜色(底部箭头)

如果可以的话,如何去掉外边框? (深灰色的)

我该如何去做呢?

最佳答案

因此,您可以通过实现 ComboBoxUI 来做到这一点,或者实际上,子类化 BasicComboBoxUI例如。第二个选项更好,因为您只需要在某些地方调整一些代码就可以了(而不是从头开始实现您自己的ComboBoxUI)。因此,下面的代码可以满足您的要求(希望如此):

import java.awt.Color;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.LookAndFeel;
import javax.swing.plaf.basic.BasicArrowButton;
import javax.swing.plaf.basic.BasicComboBoxUI;

public class MainComboBoxUI {
public static class MyComboBoxUI extends BasicComboBoxUI {
@Override
protected void installDefaults() {
super.installDefaults();
LookAndFeel.uninstallBorder(comboBox); //Uninstalls the LAF border for both button and label of combo box.
}

@Override
protected JButton createArrowButton() {
//Feel free to play with the colors:
final Color background = Color.CYAN.darker(); //Default is UIManager.getColor("ComboBox.buttonBackground").
final Color pressedButtonBorderColor = Color.RED; //Default is UIManager.getColor("ComboBox.buttonShadow"). The color of the border of the button, while it is pressed.
final Color triangle = Color.BLACK; //Default is UIManager.getColor("ComboBox.buttonDarkShadow"). The color of the triangle.
final Color highlight = background; //Default is UIManager.getColor("ComboBox.buttonHighlight"). Another color to show the button as highlighted.
final JButton button = new BasicArrowButton(BasicArrowButton.SOUTH, background, pressedButtonBorderColor, triangle, highlight);
button.setName("ComboBox.arrowButton"); //Mandatory, as per BasicComboBoxUI#createArrowButton().
return button;
}
}

public static void main(final String[] args) {
final JComboBox<String> combo = new JComboBox<>(new String[]{"A string", "B string 2", "C string 3"});
combo.setUI(new MyComboBoxUI());

final JPanel panel = new JPanel(new GridBagLayout());
panel.add(combo);
panel.setBackground(Color.RED.darker());

final JFrame frame = new JFrame("MainComboBoxUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

首先,我们删除installDefaults()内组合框的边框。方法。这将删除按钮和标签的边框。然后我们模仿createArrowButton()方法BasicComboBoxUI创建我们自己的自定义按钮。默认实现BasicComboBoxUI.createArrowButton()创建 BasicArrowButton带有一些施工时的颜色。这些颜色会更改按钮的三角形颜色,例如背景颜色。

这是为了创建 BasicArrowButton 。不过,如果您想更好地控制按钮(例如,您需要按钮上的图标和/或一些文本而不是三角形),那么您可以简单地创建一个简单的 JButton里面createArrowButton()而不是BasicArrowButton 。然后您可以在相同的方法中初始化它,或者最好在 configureArrowButton() 中修改它。 。就像下面的代码一样:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.LookAndFeel;
import javax.swing.plaf.basic.BasicComboBoxUI;

public class MainComboBoxUI2 {
public static class MyComboBoxUI extends BasicComboBoxUI {
@Override
protected void installDefaults() {
super.installDefaults();
LookAndFeel.uninstallBorder(comboBox);
}

@Override
protected JButton createArrowButton() {
final JButton button = new JButton("V");
button.setName("ComboBox.arrowButton"); //Mandatory, as per BasicComboBoxUI#createArrowButton().
return button;
}

@Override
public void configureArrowButton() {
super.configureArrowButton(); //Do not forget this!
arrowButton.setBackground(Color.CYAN.darker());
arrowButton.setForeground(Color.BLUE);
}
}

public static void main(final String[] args) {
final JComboBox<String> combo = new JComboBox<>(new String[]{"A string", "B string 2", "C string 3"});
combo.setUI(new MyComboBoxUI());
combo.setPreferredSize(new Dimension(150, 45)); //Needed to be able to show the button's text.

final JPanel panel = new JPanel(new GridBagLayout());
panel.add(combo);
panel.setBackground(Color.RED.darker());

final JFrame frame = new JFrame("MainComboBoxUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

自定义普通的唯一问题JButton方法(即第二种方法)是LayoutManager JComboBox的如果 UI 内的 protected 标志(名为 squareButton)为 true,则不会考虑箭头按钮的首选大小。 。如果该标志是 true ,然后按钮的宽度设置为等于按钮的高度,而按钮的高度又设置为等于组合框的高度。这就是为什么在第二种方法中,我手动设置组合框的首选大小,使其高度为 45 像素,这足以在按钮内显示文本(在这种特定情况下,文本等于“V”)。

您可以通过覆盖 installDefaults() 手动为单个 UI 实例设置此标志的值。 BasicComboBoxUI中的方法并将其设置为 false例如。否则,您可以通过调用 UIManager.put("ComboBox.squareButton", Boolean.FALSE); 为所有组合框设置其值。尽早在您的代码中(即在每个组合框实例化之前)。但事实证明,因为我测试过,当把这个值设置为false时按钮的大小与显示区域的大小重叠(显示区域是组合框的所选值可见的位置)。

经过进一步挖掘,我发现这个问题的原因是UI的getMinimumSize(...)方法不考虑按钮的插入...这意味着 JButton 的默认边框文本/图标周围的内容不予考虑。因此,将 squareButton 标志设置为 false 后,您还需要重写此方法。

将它们放在一起,您将得到以下 final方法/代码:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.LookAndFeel;
import javax.swing.UIManager;
import javax.swing.plaf.basic.BasicComboBoxUI;

public class MainComboBoxUI3 {
public static class MyComboBoxUI extends BasicComboBoxUI {
@Override
protected void installDefaults() {
super.installDefaults();
LookAndFeel.uninstallBorder(comboBox);
}

@Override
protected JButton createArrowButton() {
final JButton button = new JButton("Drop");
button.setName("ComboBox.arrowButton"); //Mandatory, as per BasicComboBoxUI#createArrowButton().
return button;
}

@Override
public void configureArrowButton() {
super.configureArrowButton(); //Do not forget this!
arrowButton.setBackground(Color.CYAN.darker());
arrowButton.setForeground(Color.BLUE);
//arrowButton.setBorder(javax.swing.BorderFactory.createEmptyBorder());
}

//Overrided getMinimumSize to take into account the button's insets for both width and height:
@Override
public Dimension getMinimumSize(final JComponent c) {
final Dimension mindim = super.getMinimumSize(c);
final Insets buttonInsets = arrowButton.getInsets();
return new Dimension(mindim.width + buttonInsets.left + buttonInsets.right, mindim.height + buttonInsets.top + buttonInsets.bottom);
}
}

public static void main(final String[] args) {
UIManager.put("ComboBox.squareButton", Boolean.FALSE); //Set all the combo boxes' button to non-square...
final JComboBox<String> combo = new JComboBox<>(new String[]{"A string", "B string 2", "C string 3"});
combo.setUI(new MyComboBoxUI());

final JPanel panel = new JPanel(new GridBagLayout());
panel.add(combo);
panel.setBackground(Color.RED.darker());

final JFrame frame = new JFrame("MainComboBoxUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

结果如下所示:

Combo Box final approach result

最后,如果你想让下拉按钮的边框变小(即上图中带有“Drop”字样的按钮),可以设置 arrowButton 的边框。到(例如)javax.swing.BorderFactory.createEmptyBorder()里面configureArrowButton()方法...

关于java - Swing - 删除选定的边框并更改 JComboBox 的箭头颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59917708/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com