如何去掉 JCombobox 内部的蓝色高亮显示?

huangapple go评论118阅读模式
英文:

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

问题

JComboBox的“content”窗口有这个蓝色的高亮,我不知道如何去掉,请帮忙。

以下是问题的示例:

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 JCombobox's "content" window has this blue highlight that i don't know how to get rid of, please help.

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();
    }

}

答案1

得分: 1

组合框是一个复杂的组件,它在内部使用多个组件。界面确定了这些组件之间的交互方式。

例如,将您的代码更改如下:

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);

您会注意到蓝色的高亮不是组合框或其渲染器的边框,这暗示着还有另一个我们无法访问的内部容器。

如果您真的想解决这个问题,您需要自定义MetalComboBoxUI类,但这绝不是一项容易的任务,因为许多绘制方法将是私有的。但是,请查看该类的代码,看看是否可以做到。

您代码中的其他问题:

  1. 不要使用updateUI()。当Swing内部更改外观和感觉(LAF)时,该方法会在内部调用。您没有在更改LAF。

  2. 组件应该在窗口可见之前添加到窗口中。这将消除对updateUI()的需要。

英文:

The combo box is a complex components that uses multiple components internally. The UI determines how the components interact with one another.

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. 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(). The method is invoked internally in Swing when the LAF is changed. You are not changing the LAF

  2. Components should be added to the frame BEFORE the frame is visible. This will eliminate the need for the updateUI().

huangapple
  • 本文由 发表于 2020年9月15日 00:00:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/63887897.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定