【问题标题】:Why can't I edit the appearance of this editable jComboBox?为什么我不能编辑这个可编辑的 jComboBox 的外观?
【发布时间】:2021-03-06 07:43:04
【问题描述】:

我有这段代码,但 MyComboBoxRenderer() 似乎无法使用它。它在下面写的注释行中有错误。

此代码用于自动建议。因此,当用户在文本字段上键入内容时,它会在组合框中显示建议。

public test2() {
    initComponents();
    
     jComboBox1.setRenderer(new MyComboBoxRenderer1());
     jComboBox1.setBackground(new Color(0,0,0,0));
    
    final JTextField textfield = (JTextField)     jComboBox1.getEditor().getEditorComponent(); //it has error in this line
    textfield.addKeyListener(new KeyAdapter() {
    public void keyReleased(KeyEvent ke) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                comboFilter(textfield.getText());
            }
        });
    }
    });
    }

也许它与文本字段有关。我的问题是我想编辑组合框的外观或设计。我希望它继承框架的背景。像透明的。示例在图片中。

这是图片。请点击以下链接查看。

It should be something like this

Rather than this one. This is the output of the codes above.

这是我的组合框渲染器中的代码。

 public MyComboBoxRenderer1(){
    setOpaque(true);
    setFont(new Font ("Segoe UI Semibold", Font.PLAIN ,14));
    setForeground(Color.WHITE);
}


@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
    setText(value.toString());
   if (isSelected)
    {
      setBackground(Color.WHITE);
      setForeground(Color.BLACK);
    }
    else {
        setBackground(Color.GRAY);
        setForeground(Color.WHITE);
    }
    
    return this;
}
}

为什么渲染器不能用这个?我应该怎么做才能让它发挥作用?任何人都可以帮助我吗?先感谢您。 :)


已编辑...

我已经将背景设置为透明。我只需要声明texfield的背景。 XD耶。 textfield.setBackground(新颜色(0,0,0,0)); textfield.setForeground(new Color(255,255,255));

但它留下了仍然不透明的一小部分。

我尝试在我的框架上添加一个额外的组合框。但它没有文本字段。而且效果很好!

上面是带有文本字段的组合框,我遇到的问题。较低的是没有文本字段的那个,我只是尝试了代码是否可以与普通的组合框一起使用。我需要让它看起来像较低的。

     jComboBox1.setRenderer(new MyComboBoxRenderer1());
     jComboBox1.setBackground(new Color(0,0,0,0)); 
    
     jComboBox2.setRenderer(new MyComboBoxRenderer1());
     jComboBox2.setBackground(new Color(0,0,0,0)); 

它具有相同的代码。但它不适用于另一个。也许又是因为文本字段? :(((

【问题讨论】:

    标签: java swing jtextfield jcombobox renderer


    【解决方案1】:
    jComboBox1.setBackground(new Color(0,0,0,0));
    

    不要尝试使用透明色。 Swing 不知道如何正确绘制透明颜色。请参阅:Background With Transparency 了解更多信息。

    改为更改组件的不透明度:

    component.setOpaque( false );
    

    在组合框的情况下,您需要担心组件和渲染器,因此您可以使用:

    comboBox.setOpaque(false);
    ((JLabel)comboBox.getRenderer()).setOpaque(false);
    

    但是,这会导致下拉列表出现问题。由于渲染现在是透明的,您将看不到行选择。

    尚未对此进行测试,但如果根据正在渲染的项目来改变渲染器的不透明度,这是一个可能的解决方案。

    代码可能是:

    public Component getListCellRendererComponent(
        JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
    {
        super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
    
        setOpaque(index == -1 ? false : true);
    
        // add custom painting here
    
        return this;
    }
    

    -1 表示组合框中的项目与列表中的项目相对呈现。

    【讨论】:

    • setOpaque(index = -1 ? false : true);这条线有错误。它说条件语句是多余的。
    • @RicaJacutina 这是一个错字。那应该是平等测试。如果您要了解更改的概念,那么这一点。如果您不喜欢将代码编写为单个语句,请将其更改为 if/else 语句。
    • @RicaJacutina 该代码未经测试。那是一个错字。那应该是平等测试。重点是让您了解更改的“概念”并根据需要进行更改。如果您不喜欢将代码编写为单个语句,请将其更改为 if/else 语句
    • @RicaJacutina 很高兴它有帮助。不要忘记“接受”答案。如果没有帮助,请发布正确的 minimal reproducible example 来展示问题。
    猜你喜欢
    • 1970-01-01
    • 2010-12-19
    • 2015-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-28
    相关资源
    最近更新 更多