gpt4 book ai didi

events - JComboBox 焦点和鼠标单击事件不起作用

转载 作者:行者123 更新时间:2023-12-04 14:51:18 25 4
gpt4 key购买 nike

编辑
Downvoter,这是一个糟糕的问题吗?我提供了该问题的可运行示例代码。如果它对您有用,请告诉我或指出不清楚的地方。

你好,
在下面的代码中有一个 JComboBoxJFrame ,当鼠标进入JComboBox时我没有得到通知或被点击或获得焦点。然而,PopupMenuEvent工作正常。

我究竟做错了什么? (我的目标是在 JComboBox 的文本组件被点击时收到警报)

public class TestJComboBox extends javax.swing.JFrame
{
public TestJComboBox()
{
initComponents();
}

// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jComboBox1 = new javax.swing.JComboBox();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
formMouseClicked(evt);
}
});

jComboBox1.setEditable(true);
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
jComboBox1.setName("jComboBox1"); // NOI18N
jComboBox1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jComboBox1MouseClicked(evt);
}
public void mouseEntered(java.awt.event.MouseEvent evt) {
jComboBox1MouseEntered(evt);
}
});
jComboBox1.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
}
public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
}
public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
jComboBox1PopupMenuWillBecomeVisible(evt);
}
});
jComboBox1.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
jComboBox1FocusGained(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(70, 70, 70)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 226, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(104, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(90, 90, 90)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(164, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void jComboBox1FocusGained(java.awt.event.FocusEvent evt)
{
System.out.println("JComboBox Focus gained");
}

private void formMouseClicked(java.awt.event.MouseEvent evt)
{
System.out.println("Form clicked");
jComboBox1.setFocusable(false);
jComboBox1.setFocusable(true);
}

private void jComboBox1MouseClicked(java.awt.event.MouseEvent evt)
{
System.out.println("JComboBox Click");
}

private void jComboBox1PopupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt)
{
System.out.println("JComboBox Visible menu");
}

private void jComboBox1MouseEntered(java.awt.event.MouseEvent evt)
{
System.out.println("Entered JComboBox");
}

public static void main(String args[])
{
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
new TestJComboBox().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JComboBox jComboBox1;
// End of variables declaration
}

谢谢!

最佳答案

可能downvoter 对您使用Netbeans GUI 编辑器感到冒犯。我自己不喜欢它,但如果你发现你实际上可以用它维护一个复杂的 gui,欢迎你使用它。我个人讨厌它,因为各种极其烦人的错误只会在您尝试编辑表单时显示出来,并且它会悄悄地丢失您的布局和组件设置。但这不是重点。

无论如何,你需要像这样添加你的 ActionListener :

jComboBox1.getEditor().getEditorComponent().addMouseListener(...);

JComboBox 实际上是一个复合组件,其中包含 JTextField、JButton 和 JList,因此当鼠标事件真正进入内部 JTextField 时,您将 ActionListener 添加到包装组件。

关于events - JComboBox 焦点和鼠标单击事件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6205433/

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