简体   繁体   English

如何创建一个JComboBox,其元素各自具有两种不同的样式?

[英]How to create a JComboBox whose elements each have two different styles?

I'm creating a Java application by using Swing. 我正在使用Swing创建Java应用程序。 It contains a JTextField with a JComboBox which contains the last entered words with the time when these words were entered. 它包含一个带有JComboBox的JTextField,其中包含最后输入的单词以及这些单词的输入时间。 The String with the time should be in another size and color (smaller and in light grey) as the last entered words (standard style). 带时间的字符串应采用其他大小和颜色(较小且为浅灰色)作为最后输入的单词(标准样式)。 So in each element of the JComboBox should be used two different styles. 因此,在JComboBox的每个元素中应使用两种不同的样式。 How can I do that? 我怎样才能做到这一点?

Here is an example: 这是一个例子:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.util.Date;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public class ComboSample implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new ComboSample());
    }

    @Override
    public void run() {
        JFrame frm = new JFrame("Combo example");
        final JTextField fld = new JTextField(20);
        TextData[] data = new TextData[]{new TextData("First", new Date(System.currentTimeMillis() - 100000)), 
                new TextData("Second", new Date(System.currentTimeMillis() - 200000)),
                new TextData("Third", new Date(System.currentTimeMillis() - 300000)),
                new TextData("Fourth", new Date(System.currentTimeMillis() - 400000))};
        JComboBox<TextData> cb = new JComboBox<>(data);
        cb.setSelectedItem(null);
        cb.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JComboBox cb = (JComboBox) e.getSource();
                TextData td = (TextData) cb.getSelectedItem();
                if (td != null) {
                    fld.setText(td.getText());
                }
            }
        });
        frm.add(fld);
        frm.add(cb, BorderLayout.EAST);
        frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frm.pack();
        frm.setLocationRelativeTo(null);
        frm.setVisible(true);
    }

    private static class TextData {
        private static final DateFormat FORMAT = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
        private final String text;
        private final Date timestamp;
        public TextData(String text, Date timestamp) {
            this.text = text;
            this.timestamp = timestamp;
        }
        public String getText() {
            return text;
        }
        @Override
        public String toString() {
            return "<html>" + text + " <span style=\"color:#D3D3D3\"><i>" +  FORMAT.format(timestamp) + "</i></span></html>";
        }
    }
}

An easy approach is to choose a Look and Feel for that allow to achieve that. 一种简单的方法是选择允许实现该目标的外观。 Another way can be to create a different JLabel with the background color that you need for every JComboBox element and add them with: 另一种方法是使用每个JComboBox元素所需的背景色创建一个不同的JLabel,并添加以下内容:

JLabel label = new JLabel("Some text");
label.setBackground(Color.grey);
label.setOpaque(true);

jComboBox.addItem(label);

JLabel anotherLabel = new JLabel("Another text");
anotherLabel.setBackground(Color.white);
anotherLabel.setOpaque(true);

jComboBox.addItem(anotherLabel);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM