Java: Swing: Nimbus: JComboBox的可编辑框与不可编辑框的样式不同

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

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

不,这与Nimbus的设置无关。

英文:

The first JComboBox is editable and looks fine with its TitledBorder. But the second not editable JComboBox looks strange.
All the JComboBoxes contain Enums, therefore they should not be editable but they should have the nice look of the editable JComboBox. How can I achieve that? I am using Nimbus.

Java: Swing: Nimbus: JComboBox的可编辑框与不可编辑框的样式不同

EDIT

Maybe it has to do with the settings I choose for Nimbus? These are the settings:

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

EDIT2

No, it does not have to do with the Nimbus settings:

Java: Swing: Nimbus: JComboBox的可编辑框与不可编辑框的样式不同

答案1

得分: 1

这是在我的 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());
    }
}

Java: Swing: Nimbus: JComboBox的可编辑框与不可编辑框的样式不同

英文:

This is how it looks on my Windows 10 (64 bit) machine with Oracle's JDK 15

Java: Swing: Nimbus: JComboBox的可编辑框与不可编辑框的样式不同

Here is the code.

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(),
                                                         &quot;Editable&quot;,
                                                         TitledBorder.LEADING,
                                                         TitledBorder.BOTTOM));
        Object[] items = new Object[]{&quot;One&quot;,
                                      &quot;Two&quot;,
                                      &quot;Three&quot;,
                                      &quot;Four&quot;,
                                      &quot;Five&quot;,
                                      &quot;Six&quot;,
                                      &quot;Seven&quot;,
                                      &quot;Eight&quot;,
                                      &quot;Nine&quot;,
                                      &quot;Ten&quot;};
        JComboBox&lt;Object&gt; combo = new JComboBox&lt;&gt;(items);
        combo.setEditable(true);
        panel.add(combo);
        return panel;
    }

    private JPanel createNonEditableCombo() {
        JPanel panel = new JPanel();
        panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(),
                                                         &quot;Regular&quot;,
                                                         TitledBorder.LEADING,
                                                         TitledBorder.BOTTOM));
        Object[] items = new Object[]{&quot;First&quot;,
                                      &quot;Second&quot;,
                                      &quot;Third&quot;,
                                      &quot;Fourth&quot;,
                                      &quot;Fifth&quot;,
                                      &quot;Sixth&quot;,
                                      &quot;Seventh&quot;,
                                      &quot;Eighth&quot;,
                                      &quot;Ninth&quot;,
                                      &quot;Last&quot;};
        JComboBox&lt;Object&gt; combo = new JComboBox&lt;&gt;(items);
        combo.setPrototypeDisplayValue(&quot;WWWWWWWWWW&quot;);
        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(&quot;javax.swing.plaf.nimbus.NimbusLookAndFeel&quot;);
        }
        catch (ClassNotFoundException |
               IllegalAccessException |
               InstantiationException |
               UnsupportedLookAndFeelException x) {
            x.printStackTrace();
        }
        EventQueue.invokeLater(() -&gt; new Nimbus00().showGui());
    }
}

答案2

得分: 0

原因是我直接在JComboBox上设置了边框,而没有在包围它的JPanel上设置。

Java: Swing: Nimbus: JComboBox的可编辑框与不可编辑框的样式不同

英文:

The reason is, I set the border directly on the JComboBox and not on the JPanel surrounding it.

Java: Swing: Nimbus: JComboBox的可编辑框与不可编辑框的样式不同

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

发表评论

匿名网友

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

确定