Java:Swing:Nimbus:JComboBox 可编辑框与不可编辑框看起来不同
Java:Swing:Nimbus:JComboBox editable box looks different from not editable box
第一个 JComboBox 是可编辑的,它的 TitledBorder 看起来不错。但是第二个不可编辑的 JComboBox 看起来很奇怪。
所有 JComboBox 都包含枚举,因此它们不应是可编辑的,但它们应该具有可编辑 JComboBox 的漂亮外观。我怎样才能做到这一点?我正在使用 Nimbus。
编辑
可能与我为 Nimbus 选择的设置有关?这些是设置:
NimbusLookAndFeel nimbus = new NimbusLookAndFeel();
UIManager.setLookAndFeel(nimbus);
UIManager.put("control", Settings.getTexturedBackgroundColor());
UIManager.put("nimbusBlueGrey", Settings.getLightGrayGold());
UIManager.put("nimbusBase", Settings.getDarkGold());
UIManager.put("textForeground", Color.BLACK);
UIManager.put("nimbusFocus", new Color(255, 220, 35));
UIManager.put("ToolBar:Button.contentMargins",
new Insets(5, 15, 5, 15));
UIManager.put("TextField.background", Settings.getLightYellow());
UIManager.put("ComboBox.forceOpaque", false);
UIManager.put("TitledBorder.border", new Insets(10, 10, 10, 10));
UIManager.put("TitledBorder.position", TitledBorder.ABOVE_BOTTOM);
UIManager.put("TitledBorder.font", getGermanFont(16F));
UIManager.put("TitledBorder.titleColor", Color.GRAY);
UIManager.put("Table.opaque", false);
UIManager.put("List.opaque", false);
UIManager.put("Table.cellRenderer", false);
UIManager.put("OptionPane.buttonFont", Main.getGermanFont(16F));
编辑2
不,这与 Nimbus 设置无关:
这就是它在我的 Windows 10(64 位)机器上与 Oracle JDK 15
的样子
这是代码。
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.WindowConstants;
import javax.swing.border.TitledBorder;
public class Nimbus00 {
private JFrame frame;
private JPanel createEditableCombo() {
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(),
"Editable",
TitledBorder.LEADING,
TitledBorder.BOTTOM));
Object[] items = new Object[]{"One",
"Two",
"Three",
"Four",
"Five",
"Six",
"Seven",
"Eight",
"Nine",
"Ten"};
JComboBox<Object> combo = new JComboBox<>(items);
combo.setEditable(true);
panel.add(combo);
return panel;
}
private JPanel createNonEditableCombo() {
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(),
"Regular",
TitledBorder.LEADING,
TitledBorder.BOTTOM));
Object[] items = new Object[]{"First",
"Second",
"Third",
"Fourth",
"Fifth",
"Sixth",
"Seventh",
"Eighth",
"Ninth",
"Last"};
JComboBox<Object> combo = new JComboBox<>(items);
combo.setPrototypeDisplayValue("WWWWWWWWWW");
panel.add(combo);
return panel;
}
private void showGui() {
frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(createEditableCombo(), BorderLayout.PAGE_START);
frame.add(createNonEditableCombo(), BorderLayout.PAGE_END);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
}
catch (ClassNotFoundException |
IllegalAccessException |
InstantiationException |
UnsupportedLookAndFeelException x) {
x.printStackTrace();
}
EventQueue.invokeLater(() -> new Nimbus00().showGui());
}
}
原因是,我直接在JComboBox上设置边框,而不是在它周围的JPanel上。
第一个 JComboBox 是可编辑的,它的 TitledBorder 看起来不错。但是第二个不可编辑的 JComboBox 看起来很奇怪。 所有 JComboBox 都包含枚举,因此它们不应是可编辑的,但它们应该具有可编辑 JComboBox 的漂亮外观。我怎样才能做到这一点?我正在使用 Nimbus。
编辑
可能与我为 Nimbus 选择的设置有关?这些是设置:
NimbusLookAndFeel nimbus = new NimbusLookAndFeel();
UIManager.setLookAndFeel(nimbus);
UIManager.put("control", Settings.getTexturedBackgroundColor());
UIManager.put("nimbusBlueGrey", Settings.getLightGrayGold());
UIManager.put("nimbusBase", Settings.getDarkGold());
UIManager.put("textForeground", Color.BLACK);
UIManager.put("nimbusFocus", new Color(255, 220, 35));
UIManager.put("ToolBar:Button.contentMargins",
new Insets(5, 15, 5, 15));
UIManager.put("TextField.background", Settings.getLightYellow());
UIManager.put("ComboBox.forceOpaque", false);
UIManager.put("TitledBorder.border", new Insets(10, 10, 10, 10));
UIManager.put("TitledBorder.position", TitledBorder.ABOVE_BOTTOM);
UIManager.put("TitledBorder.font", getGermanFont(16F));
UIManager.put("TitledBorder.titleColor", Color.GRAY);
UIManager.put("Table.opaque", false);
UIManager.put("List.opaque", false);
UIManager.put("Table.cellRenderer", false);
UIManager.put("OptionPane.buttonFont", Main.getGermanFont(16F));
编辑2
不,这与 Nimbus 设置无关:
这就是它在我的 Windows 10(64 位)机器上与 Oracle JDK 15
的样子这是代码。
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.WindowConstants;
import javax.swing.border.TitledBorder;
public class Nimbus00 {
private JFrame frame;
private JPanel createEditableCombo() {
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(),
"Editable",
TitledBorder.LEADING,
TitledBorder.BOTTOM));
Object[] items = new Object[]{"One",
"Two",
"Three",
"Four",
"Five",
"Six",
"Seven",
"Eight",
"Nine",
"Ten"};
JComboBox<Object> combo = new JComboBox<>(items);
combo.setEditable(true);
panel.add(combo);
return panel;
}
private JPanel createNonEditableCombo() {
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(),
"Regular",
TitledBorder.LEADING,
TitledBorder.BOTTOM));
Object[] items = new Object[]{"First",
"Second",
"Third",
"Fourth",
"Fifth",
"Sixth",
"Seventh",
"Eighth",
"Ninth",
"Last"};
JComboBox<Object> combo = new JComboBox<>(items);
combo.setPrototypeDisplayValue("WWWWWWWWWW");
panel.add(combo);
return panel;
}
private void showGui() {
frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(createEditableCombo(), BorderLayout.PAGE_START);
frame.add(createNonEditableCombo(), BorderLayout.PAGE_END);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
}
catch (ClassNotFoundException |
IllegalAccessException |
InstantiationException |
UnsupportedLookAndFeelException x) {
x.printStackTrace();
}
EventQueue.invokeLater(() -> new Nimbus00().showGui());
}
}
原因是,我直接在JComboBox上设置边框,而不是在它周围的JPanel上。