JComboBox的行为与JTextField不同.我怎么能看起来相似? [英] JComboBox does not behave same as JTextField. How can i have it look similar?
本文介绍了JComboBox的行为与JTextField不同.我怎么能看起来相似?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个下面的组合框,在其中可以创建带有项目等的组合框,但外观与JTextField不同.如何使JCombobox看起来像JTextField?
I have this following combo box, where i am able to create my combo box with items etc, but the look is not same as JTextField. How can i make JCombobox look same like JTextField?
MyComboBox.java:
MyComboBox.java:
import java.awt.Color;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import javax.swing.plaf.basic.BasicComboBoxUI;
public class MyComboBox extends JComboBox {
public MyComboBox(String[] name) {
Border border = BorderFactory.createEmptyBorder(11, 11, 11, 11);
JComboBox cb = new JComboBox(name);
cb.setUI(new BasicComboBoxUI() {
@Override
protected JButton createArrowButton() {
return new JButton() {
@Override
public int getWidth() {
return 0;
}
};
}
});
setModel(cb.getModel());
setOpaque(false);
setBorder(new LineBorder(Color.black, 1, true));
//setBackground(Color.white);
setVisible(true);
}
// public void paintComponent(Graphics g) {
// super.paintComponent(g);
// g.setColor(new Color(red, green, blue) );
// g.fillOval(125, 125, 50, 50);
// }
}
推荐答案
-
最好使用外观和感觉
JComboBox
有两种状态,JComboBox
可以是Editable
和Non_Editable
(默认)JComboBox
has two states,JComboBox
can beEditable
andNon_Editable
(default)没有
CustomUI
(例如@aterai),但对Look and Feel
敏感,仅对Metal Look and Feel
without
CustomUI
(as example by @aterai), butLook and Feel
sensitive, only forMetal Look and Feel
import java.awt.*; import java.util.Vector; import javax.swing.*; import javax.swing.UIManager; import javax.swing.plaf.ColorUIResource; import javax.swing.plaf.metal.MetalComboBoxButton; public class MyComboBox { private Vector<String> listSomeString = new Vector<String>(); private JComboBox someComboBox = new JComboBox(listSomeString); private JComboBox editableComboBox = new JComboBox(listSomeString); private JComboBox non_EditableComboBox = new JComboBox(listSomeString); private JFrame frame; public MyComboBox() { listSomeString.add("-"); listSomeString.add("Snowboarding"); listSomeString.add("Rowing"); listSomeString.add("Knitting"); listSomeString.add("Speed reading"); // someComboBox.setPrototypeDisplayValue("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); someComboBox.setFont(new Font("Serif", Font.BOLD, 16)); someComboBox.setEditable(true); someComboBox.getEditor().getEditorComponent().setBackground(Color.YELLOW); ((JTextField) someComboBox.getEditor().getEditorComponent()).setBackground(Color.YELLOW); // editableComboBox.setPrototypeDisplayValue("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); editableComboBox.setFont(new Font("Serif", Font.BOLD, 16)); editableComboBox.setEditable(true); JTextField text = ((JTextField) editableComboBox.getEditor().getEditorComponent()); text.setBackground(Color.YELLOW); JComboBox coloredArrowsCombo = editableComboBox; Component[] comp = coloredArrowsCombo.getComponents(); for (int i = 0; i < comp.length; i++) { if (comp[i] instanceof MetalComboBoxButton) { MetalComboBoxButton coloredArrowsButton = (MetalComboBoxButton) comp[i]; coloredArrowsButton.setBackground(null); break; } } // non_EditableComboBox.setPrototypeDisplayValue("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); non_EditableComboBox.setFont(new Font("Serif", Font.BOLD, 16)); // frame = new JFrame(); frame.setLayout(new GridLayout(0, 1, 10, 10)); frame.add(someComboBox); frame.add(editableComboBox); frame.add(non_EditableComboBox); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocation(100, 100); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { UIManager.put("ComboBox.background", new ColorUIResource(Color.yellow)); UIManager.put("JTextField.background", new ColorUIResource(Color.yellow)); UIManager.put("ComboBox.selectionBackground", new ColorUIResource(Color.magenta)); UIManager.put("ComboBox.selectionForeground", new ColorUIResource(Color.blue)); SwingUtilities.invokeLater(new Runnable() { @Override public void run() { MyComboBox aCTF = new MyComboBox(); } }); } }
这篇关于JComboBox的行为与JTextField不同.我怎么能看起来相似?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文