英文:
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
类,但这绝不是一项容易的任务,因为许多绘制方法将是私有的。但是,请查看该类的代码,看看是否可以做到。
您代码中的其他问题:
-
不要使用
updateUI()
。当Swing内部更改外观和感觉(LAF)时,该方法会在内部调用。您没有在更改LAF。 -
组件应该在窗口可见之前添加到窗口中。这将消除对
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:
-
Don't use
updateUI()
. The method is invoked internally in Swing when the LAF is changed. You are not changing the LAF -
Components should be added to the frame BEFORE the frame is visible. This will eliminate the need for the updateUI().
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论