gpt4 book ai didi

java - 单击两次时自定义 JComboBox 未关闭

转载 作者:行者123 更新时间:2023-12-01 10:51:34 25 4
gpt4 key购买 nike

正如标题所示,我正在创建一个自定义的JComboBox,并且当我编写popup.hide()时,自定义的BasicComboPopup不会隐藏;另外,如果你们知道制作 JComboBox 的更简单方法,该方法在主组件中显示缩写值,但在弹出窗口中显示未缩写的内容,我洗耳恭听。这是我的代码:

     import javax.swing.JComboBox;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import javax.swing.JFrame;
import java.awt.GridBagLayout;
import java.awt.Component;
import java.awt.Container;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPopupMenu;
import javax.swing.plaf.basic.BasicComboPopup;

class ChangingCombos extends JFrame
{
String[] abbrStr = new String[]{"--","TF","FF","SP"};
String[] longStr = new String[]{"-Select One-","Thermo Film","Fashion Film","Specialty"};
AbbreviatedComboBox<String> box = new AbbreviatedComboBox<String>(longStr,abbrStr);

public ChangingCombos()
{
setLayout(new GridBagLayout());
add(box);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocation(300,300);
pack();
setVisible(true);
}

public static void main(String[]args)
{
new ChangingCombos();
}

class AbbreviatedComboBox<E> extends JComboBox<E>
{
E[] items = null;
E[] abbreviations = null;
BasicComboPopup popup = null;
JComboBox self = this;
JComboBox<E> abstractBox;

public AbbreviatedComboBox(E[] items, E[] abbreviations)
{
super(abbreviations);
this.items = items;
this.abbreviations = abbreviations;
abstractBox = new JComboBox<E>(items);
popup = new BasicComboPopup(abstractBox);
addCustomMouseListener(this);
for(Component c: GUIUtilities.getAllSubcomponentsOf(this))addCustomMouseListener(c);

}

private void addCustomMouseListener(Component c)
{
c.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
System.out.println("ohp");
if(!popup.isVisible())
{
popup.show(self,0,self.getHeight());
}
else popup.hide();
}
});
}
}
}

...另外,为了解释 GUIUtilities,它是另一个自定义类,并且方法 getAllSubcomponentsOf(Container c) 递归地返回所述容器中包含的所有组件。这是我访问 JComboBox 上的小箭头按钮的快捷方法。向同一个对象添加多个监听器似乎会产生问题,但它只迭代一次操作,正如仅使用 System.out.println("ohp") 验证的那样每次点击打印一次。

编辑:setVisible(false) 不起作用。我指的隐藏方法是ComboPopup.hide();它并没有被弃用。当我执行 abstractBox.hidePopup() 时,它不会执行任何操作,因为 abstractBox 没有显示在屏幕上,而且从未打算这样做。只是它的弹出窗口。继续努力。

最佳答案

a simpler way of making a JComboBox that shows abbreviated values in the main component, but the un-abbreviated content in the popup

您将自定义对象存储在包含两个属性的 ComboBoxModel 中:

  1. 值 - 用于在程序中进行处理
  2. 描述 - 用于在组合框中显示

然后您可以使用组合框的自定义渲染器来显示描述值。

查看ComboBox With Custom Renderer了解此方法的更多信息和代码示例。

您还可以查看Combo Box Prompt它允许您提示输入值,而无需在 ComboBoxModel 中存储项目。您需要将此解决方案合并到上面示例中的渲染器中。

编辑:

也许我误解了你的要求。下面是一个简单的示例,其呈现下拉列表的方式与组合框中的显示方式不同:

import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.basic.*;

public class ComboBoxRenderer extends JPanel
{
public ComboBoxRenderer()
{
String[] items = { "Red", "Green", "Blue" };
JComboBox<String> comboBox = new JComboBox<String>( items );
comboBox.setSelectedIndex(-1);
comboBox.setPrototypeDisplayValue( "Select Color" );
comboBox.setRenderer( new MyRenderer() );

add(comboBox, BorderLayout.NORTH );
}

class MyRenderer extends BasicComboBoxRenderer
{
public Component getListCellRendererComponent(
JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
{
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

if (value == null)
value = "Select Color";

if (index == -1 && value != null)
{
setText(value.toString());
}
else
setText(index + " : " + value);

return this;
}
}

private static void createAndShowGUI()
{

JFrame frame = new JFrame("SSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new ComboBoxRenderer());
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );
}

public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}

关于java - 单击两次时自定义 JComboBox 未关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33849897/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com