简体   繁体   English

我怎样才能摆脱 JCombobox 内的蓝色高光?

[英]How can i get rid of the blue highlight inside JCombobox?

The JCombobox's "content" window has this blue highlight that i don't know how to get rid of, please help. JCombobox 的“内容”窗口有这个蓝色突出显示,我不知道如何摆脱,请帮忙。

Here is an example of the problem:下面是问题的一个例子:

package example;

import java.awt.CardLayout;
import java.awt.Color;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Example {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(1);
        frame.setSize(500, 500);
        frame.setLayout(new CardLayout());
        frame.setVisible(true);
        
        JPanel panel=new JPanel();
        panel.setLayout(null);
        frame.add(panel);

        String[] model = {"pres.", "PPS.", "P. inp.", "P. mais q."};

        JComboBox combox;
        combox = new JComboBox(model);
        combox.setBounds(100, 100, 145, 30);
        combox.setBackground(new Color(215, 211, 165));
        combox.setFocusable(false);
        panel.add(combox);
        
        panel.updateUI();
    }

}

The combo box is a complex components that uses multiple components internally.组合框是一个内部使用多个组件的复杂组件。 The UI determines how the components interact with one another. UI 确定组件如何相互交互。

For example change your code to the following:例如,将您的代码更改为以下内容:

JComboBox combox;
combox = new JComboBox(model);
combox.setBorder( new LineBorder(Color.YELLOW) );
BasicComboBoxRenderer renderer = new BasicComboBoxRenderer();
renderer.setBorder( new LineBorder(Color.RED) );
combox.setRenderer(renderer);

And you will notice that the blue highlight is not a border of the combo box or its render, implying there is another internal container we don't have access to.您会注意到蓝色高光不是组合框或其渲染的边框,这意味着我们无法访问另一个内部容器。

If you really want to solve the problem then you will need to customize the MetalComboBoxUI class, which is never an easy task because many of the painting methods will be private.如果您真的想解决问题,那么您将需要自定义MetalComboBoxUI类,这绝非易事,因为许多绘画方法将是私有的。 But take a look at the code of the class to see if it can be done.但是看一下类的代码,看看能不能做到。

Other issues with your code:您的代码的其他问题:

  1. Don't use updateUI() .不要使用updateUI() The method is invoked internally in Swing when the LAF is changed.当 LAF 更改时,该方法在 Swing 中内部调用。 You are not changing the LAF你不是在改变 LAF

  2. Components should be added to the frame BEFORE the frame is visible.组件应该在框架可见之前添加到框架中。 This will eliminate the need for the updateUI().这将消除对 updateUI() 的需要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM