java swing JCombox上的箭头事件怎么触发

zhangshenqiu 2009-11-12 05:44:33
java swing JCombox上的向下箭头事件怎么触发,用什么监听事件 的?我只要单击那个箭头就想处理一个动作。各位帮帮忙
...全文
444 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
睡觉谁叫~~~ 2009-11-12
  • 打赏
  • 举报
回复
学习了!
qingzhe2008 2009-11-12
  • 打赏
  • 举报
回复

comboBox.addPopupMenuListener(new PopupMenuListener(){

public void popupMenuCanceled(PopupMenuEvent e) {
// 下拉选项弹出时,按ESC键会触发
System.out.println("popupMenuCanceled");

}

public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
// 点击箭头,下拉选项消失时触发
System.out.println("popupMenuWillBecomeInvisible");
}

public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
// 点击箭头,下拉选项弹出时触发
System.out.println("popupMenuWillBecomeVisible");

}

});
littlemonster 2009-11-12
  • 打赏
  • 举报
回复
import java.awt.BorderLayout;
import java.awt.ItemSelectable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;
import javax.swing.JFrame;

public class MainClass {

public static void main(final String args[]) {
final String labels[] = { "A", "B", "C", "D", "E" };
JFrame frame = new JFrame("Selecting JComboBox");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JComboBox comboBox = new JComboBox(labels);
frame.add(comboBox, BorderLayout.SOUTH);

ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
System.out.println("Command: " + actionEvent.getActionCommand());
ItemSelectable is = (ItemSelectable)actionEvent.getSource();
System.out.println(", Selected: " + selectedString(is));
}
};
comboBox.addActionListener(actionListener);
frame.setSize(400, 200);
frame.setVisible(true);

}
static private String selectedString(ItemSelectable is) {
Object selected[] = is.getSelectedObjects();
return ((selected.length == 0) ? "null" : (String)selected[0]);
}
}
plplum 2009-11-12
  • 打赏
  • 举报
回复


import javax.swing.JOptionPane;


public class NewJFrame2 extends javax.swing.JFrame {

/** Creates new form NewJFrame2 */
public NewJFrame2() {
initComponents();
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jComboBox1 = new javax.swing.JComboBox();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
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);
}
});

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(100, 100, 100)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(144, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(84, 84, 84)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(246, Short.MAX_VALUE))
);

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

private void jComboBox1PopupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
JOptionPane.showMessageDialog(null, "事件触发!");
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame2().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JComboBox jComboBox1;
// End of variables declaration

}
zhangshenqiu 2009-11-12
  • 打赏
  • 举报
回复
单击箭头,是触发不了那个ActionListener吧?我试过了。不行啊
swandragon 2009-11-12
  • 打赏
  • 举报
回复

ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {

}
};

public static void main(String args[]) {
String labels[] = { "A", "B", "C", "D","E", "F", "G", "H","I", "J" };
JFrame frame = new JFrame("Editable JComboBox");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = frame.getContentPane();

final JComboBox comboBox = new JComboBox(labels);
comboBox.setMaximumRowCount(5);
comboBox.setEditable(true);
contentPane.add(comboBox, BorderLayout.NORTH);

final JTextArea textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textArea);
contentPane.add(scrollPane, BorderLayout.CENTER);

ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
textArea.append("Selected: " + comboBox.getSelectedItem());
textArea.append(", Position: " + comboBox.getSelectedIndex());
textArea.append(System.getProperty("line.separator"));
}
};
comboBox.addActionListener(actionListener);

frame.setSize(300, 200);
frame.setVisible(true);
}
一洽客服系统 2009-11-12
  • 打赏
  • 举报
回复
应该actionlistener就能满足你的需求吧

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧