首页 > 解决方案 > Swing 组合框的相当烦人的问题

问题描述

https://youtu.be/8djixdHNoEQ - 该视频显示了我所面临的情况,因为它JComboBox似乎多次显示/留下残像。

这是我设置组合框的方式:

private String[] list = { "Inches/Centimeters", "Miles/Kilometres", "Pounds/Kilograms", "Gallons/Litres", "Feet/Metres", "Celcius/Kelvin", "Acres/Hectare" }; //the String array the ComboBox uses

private JComboBox<String> conversionCombo; //defining the JComboBox.
conversionCombo = new JComboBox<String>(list); // creating JComboBox

除了定义它使用的列表、组合框本身和创建组合框之外,没有其他代码与该组件交互。(除了设置组合框并将其添加到屏幕等的代码之外)

下面的示例程序。

thing1(驱动类,设置 JFrame):

import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JLabel;


public class thing1 {

    public static void main(String[] args) throws IOException {

        JFrame frame = new JFrame("Thing");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        thing2 thing2 = new thing2();

        frame.getContentPane().add(thing2);

        frame.pack();
        frame.setVisible(true);
    }
}

thing2(设置 UI 元素 - 在本例中只是 JComboBox):

import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JComboBox;
import javax.swing.JPanel;



@SuppressWarnings("serial")
public class thing2 extends JPanel {

    /**
     *  Declaring all variables and components to be used within the GUI
     */

    private String[] list = { "Inches/Centimeters", "Miles/Kilometres", "Pounds/Kilograms", "Gallons/Litres", "Feet/Metres", "Celcius/Kelvin", "Acres/Hectare" };



    thing2() {
        JComboBox<String> conversionCombo = new JComboBox<String>(list); // creating JComboBox
        add(conversionCombo);
        setPreferredSize(new Dimension(800, 80));
        setBackground(Color.WHITE);
    }

}

应该能够编译这个并看到问题。在这种情况下,如果您要从下拉列表中选择任何内容并将鼠标移动到组合框的右侧,而您的鼠标位于其中(例如:https ://i.imgur.com/Z2Slrl3.gifv ),“之后-图像”将出现。

标签: javaswingjcombobox

解决方案


推荐阅读